Tips on Android Development Using Emacs

Android using Emacs

For the intrepid Emacs user, here are some tips for doing Android development without Eclipse. I am working on a follow-up post with some general Android SDK tips.

Emacs prerequisites

  1. android-mode
    This mode gives you the following interactive commands:

    • M-x android-start-emulator Start the Android SDK emulator from emacs
    • M-x android-start-ddms Start the Android debugger
    • M-x android-logcat Like syslog for Android
    • M-x android-ant Run any ant task in the current project directory
  2. android.el
    This is a file included in the Android SDK. It duplicates some of the functionality of android-mode, but adds M-x android-jdb, which starts the JDB debugger once you have DDMS running.Load it from {SDK dir}/tools/lib
  3. JDE
    Optional, but it’s great for java code. Required to use beanshell shortcuts like the import statement command below (Generate and insert import statements)
  4. Typical emacs setup cruft
    Here’s what I have in my init file for Android:

    (add-to-list 'load-path "~/emacs/android-mode")
    	    (require 'android-mode)
    	    (setq android-mode-sdk-dir "~/work/android/android")
    	    (add-hook 'gud-mode-hook
                (lambda ()
                (add-to-list 'gud-jdb-classpath "/home/gregj/work/android-sdk-linux_86/platforms/android-7/android.jar")

Launch the emulator

Although you can start the emulator from within emacs using android-mode: M-x android-start-emulator

I prefer to launch from the shell, since this allows me to exit emacs without having to restart the emulator, e.g.:

emulator -avd starter-21 -partition-size 128 >/dev/null &

Create a project

Instead of using the New Project wizard in Eclipse, use these steps to create an Android project using the shell:

cd {project directory}
	mkdir HelloAndroid
	cd HelloAndroid
	android create project --name HelloAndroid --target android-7 --path . --package home.hoochiepep.HelloAndroid --activity HelloAndroid

Change =–target=, =–package=, and =–activity= parameters as appropriate.

To get a list of valid targets for your SDK, use

android list

Specify the application name

To change the name of the application, edit {project root}/res/values/strings.xml:

<string name="app_name">Hello, Android</string>

Specify a minimum SDK level

The Eclipse New Project wizard includes a Min SDK Version field. To set this without the wizard, add a line to AndroidManifest.xml as a child of manifest:

<uses-sdk android:minSdkVersion="2" />

Make an eclipse project ant-ready (do also when moved to a new machine)

Projects you pull from online will have been created using Eclipse, and will not have a build.xml file for ant.

Another common problem is that the file will be missing. Running an update as shown here will fix both problems:

android update project --path . --target android-7 --subprojects

Generate and insert import statements

You will see references to a handy Eclipse command (invoked by Control-Shift-O) which adds import statements for referenced classes.

The emacs equivalent is found in jde-mode: M-x jde-import-find-and-import (C-c C-v C-z). Make sure the classpath is set correctly prior to starting jde-mode

Use the debugger within Emacs

  • Install the app on the emulator using M-x android-ant-install (the ‘install’ target uses the debug apk)
  • Start ddms. There are no cmdline args, so you might as well use M-x android-start-ddms
  • In the emulator, go to Dev Tools -> Development Settings and select the app as the debug app
  • In the emulator, start the app
  • Look in the ddms window for the app’s debug port (usu. 8700 if the Dev Settings step was done)
  • Start jdb by invoking M-x android-jdb from android.elYou can also start jdb directly using the following command line params as a guide
    jdb -sourcepath/home/gregj/work/android/projects/NotepadCodeLab/Notepadv3/src -attach localhost:8700

    Set breakpoints in JDB, like so

    stop in

    If the project directory is set correctly in the jdb command, you will be able to set breakpoints by line using jde-mode’s M-x gud-break (C-x space).

Posted in Android, Development, emacs Tagged with: , , , , ,
4 comments on “Tips on Android Development Using Emacs
  1. Risto says:

    Thanks, these were great tips!
    Compilation error message refer to missing includes. How should I set the classpath? I’m experienced with C and Emacs but new to Java, and trying the Android GoogleMaps tutorial from the developer site.

    BR, Risto

    The emacs equivalent is found in jde-mode: M-x jde-import-find-and-import (C-c C-v C-z). Make sure the classpath is set correctly prior to starting jde-mode

  2. Justinhj says:

    Thanks this saved me a lot of time I think! BTW android-ant-install fails with the message that I have two devices, my phone, and the emulator. How do I tell it which to use?

  3. @Justinhj: I resolve it by directly going to the command line. adb has handy parameters: -d for device, -e for emulator.

    So, for example, I use M-x android-ant-compile, but drop to the shell to install: adb -e bin/MyApp-debug.apk

  4. Justinhj says:

    Perfect thanks!

1 Pings/Trackbacks for "Tips on Android Development Using Emacs"
  1. […] also Gregory Grubbs’s site for more tips on Android development without […]

Leave a Reply

Your email address will not be published. Required fields are marked *