Android apps with emacs

Recently my $gf got her first smartphone and since I don't own one it was the first chance I got messing around with android and its API. I tried some basic java samples and fiddled with the NDK as well. I did all this using AndroidStudio, but since it won't run on my laptop (not enough RAM) and there were no c/c++ plugins (yet) to aid the development of native apps, I dumped it. My first choice as an alternative is Emacs. I found android-mode quite easy to use. I added a small patch from lateau which adds support for the ndk-build command. To get class/function/package… completion for the java part I'm using the fork of ajc-java-complete from whitypig. Creating the tags of the system jars didn't work though. I used the prebuild tags and created the android tags with Tags.jar as described in the install file. I'm still using pidcat as a nice colorful logcat alternative until I'm happy with or used to the implementation which android-mode supplies.

So here are a few steps to get a android development environment powered by Emacs.

1 Get the android SDK and NDK.

> mkdir -p ~/projects/android
> cd ~/projects/android
> wget http://dl.google.com/android/android-sdk_r23.0.2-linux.tgz
> tar xfz android-sdk_r23.0.2-linux.tgz
> wget http://dl.google.com/android/ndk/android-ndk32-r10b-linux-x86_64.tar.bz2
> tar xfj android-ndk32-r10b-linux-x86_64.tar.bz2

2 Add some of those newly created folders to your PATH

ANDROID_SDK=$HOME/projects/android/android-sdk-linux
ANDROID_NDK=$HOME/projects/android/android-ndk-r10b
PATH=$PATH:$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$ANDROID_NDK

Add these lines to your .bashrc or .zshrc or whatever.

3 Install/update the android API

> android sdk

4 Create a virtual device for local testing

> android avd

5 Install android-mode

M-x el-get-install<RET>
android-mode

or

M-x list-packages
(select android-mode and install)

6 Patch android-mode (fork with applied changes follow)

7 Test the whole thing using an NDK sample

> mkdir -p ~/projects/android/workspace
> cp -r ~/projects/android/android-ndk-r10b/samples/hello-jni ~/projects/android/workspace/
> cd ~/projects/android/workspace/hello-jni
> android update project -n hello-jni -p . -t 1

We'll use ant to build our project. The last line creates the necessary files for ant. Now fire up Emacs, and open the file ./src/com/example/hellojni/HelloJni.java. Enable android-mode if not done automatically and try the following.

M-x android-ndk-build

A new buffer should open and you should see something like this:

-*- mode: compilation; default-directory: "~/projects/android/workspace/hello-jni/" -*-
Compilation started at Wed Oct  1 22:49:22

ndk-build
Android NDK: WARNING: APP_PLATFORM android-17 is larger than android:minSdkVersion 3 in ./AndroidManifest.xml    
[armeabi-v7a] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi-v7a/gdbserver
[armeabi-v7a] Gdbsetup       : libs/armeabi-v7a/gdb.setup
[armeabi] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
[armeabi] Gdbsetup       : libs/armeabi/gdb.setup
[x86] Gdbserver      : [x86-4.6] libs/x86/gdbserver
[x86] Gdbsetup       : libs/x86/gdb.setup
[mips] Gdbserver      : [mipsel-linux-android-4.6] libs/mips/gdbserver
[mips] Gdbsetup       : libs/mips/gdb.setup
[armeabi-v7a] Install        : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so
[armeabi] Install        : libhello-jni.so => libs/armeabi/libhello-jni.so
[x86] Install        : libhello-jni.so => libs/x86/libhello-jni.so
[mips] Install        : libhello-jni.so => libs/mips/libhello-jni.so

Compilation finished at Wed Oct  1 22:49:24

The native part was compiled successfully. To build the java part just run:

M-x android-build-debug

A new buffer will open and you will see the verbose output of ant compiling you sources using the previously generated build.xml.

To test the created app, start an emulator and deploy the app using the commands android-mode provides.

M-x android-start-emulator
<tab>TestEmu<ret>
...
the tab key should complete the previously created avd name
...
M-x android-build-install
M-x android-logcat
M-x android-start-app

Your native app should now be running within your emulator. Hooray.

Refs:

October 01, 2014 | android, java, c++, c, emacs
about | blog | archives