Development for Android with Emacs

A tutorial on how to get up and running for Android development using Emacs.

Android test_activity

Like many emacs users I also wanted to be able to use my favorite editor to experiment with developing for Android. Eclipse is an awesome IDE but nothing can beat using the tools you are comfortable with when coding.

Unfortunately there is not much on the web for how to accomplish the Emacs-Android combination. I was lead to this web archive entry of an old blog post by a StackOverflow post which gives some hints as to how to do it. So I am writting this small blog post myself in order to document it for 2014 and so that people don’t need to rely on an archived blog post which may as well dissapear at any minute.

This guide will assume you are using Arch Linux since this is the Linux Distro I use but the packages should be the same for Debian, Ubuntu or what have you.

Assure you have the needed packages


You will need to have the latest java development kit and also ANT, a java-based build tool.

~$ pacman -Su jdk7-openjdk apache-ant

Also if you have a 64-bit system make sure that you have the multilib package repository activated, so that 32-bit applications can be properly compiled. If you are on Debian/Ubuntu then you will need the ia32-libs package.

~$ apt-get install ia32-libs

Download Android SDK for Linux


Create a directory where the Android SDK shall reside. Let’s assume for the purpose of this guide, that this will be ~/opt/android. So, create the directory if it does not already exist and download the Android SDK from here. Note: Replace the actual SDK filenames in the example below with the most recent ones you will have found in the website at the time of your reading this post.

~$ mkdir opt
~$ wget http://dl.google.com/android/android-sdk_r22.3-linux.tgz
~$ unzip android-sdk-linux_x86-1.1_r1.zip -d ~/opt
~$ mv ~/opt/android-sdk-linux_x86-1.1_r1 ~/opt/android

Add Android Tools to System Path


Edit your shell program’s rc file to add the Android tools directory to your system path. If you are using bash add this to the ~/.bashrc file:

PATH=$PATH:~/opt/android/tools

Or if like me you are using zsh add this to your ~/.zshrc.

export PATH="${ZDOTDIR:-$HOME}/opt/android/tools:$PATH"

Note: zsh is mostly compatible with bash so the first command would also have been fine.

Install Android Mode for Emacs


If you are using a version of Emacs older than 24 then you should simply clone the Android mode repository from Github.

~$ cd ~/opt
~$ git clone git://github.com/remvee/android-mode.git

But if on the other hand you have Emacs 24 or greater then you should be using ELPA and download the package from there.

M-x list-packages

Find android-mode, place the cursor on top of it and press ‘i’ to mark for installing. Then press ‘x’ to execute the installation of the marked package.

Configure Emacs to use Android Mode


Open your emacs init file and add the following:

;;android MODE
;; Omit the next line if installed through ELPA
(add-to-list 'load-path "~/opt/android-mode")
(require 'android-mode)
(custom-set-variables '(android-mode-sdk-dir "~/opt/android"))

Download and Configure the API


Open the android SDK manager from a terminal

android sdk

Android SDK
Android SDK manager

Select the latest API and download it from there. In the screenshot it’s the API version 4.4.2

Now again from a terminal open the Android Virtual Devices manager and create a new Virtual Device.

android avd

An Android Virtual Device is an emulated Android device of your choice. You can have as many as you want and each time you compile and deploy your test applications you do so against one specific device.

Android Virtual Device Manager
Android Virtual Device Manager

From the screenshot you can see that I created a device named test, running on an Intel Atom CPU with the 4.4.2 Android API.

Create your first Android Project


Now you should create an Android project. This can be done in two different ways. Either from the console or from inside emacs. If you are inside emacs it’s as simple as:

M-x android-create-project

Then emacs will prompt you for 3 arguments which are PATH PACKAGE ACTIVITY.

If on the other hand you want to create the project from the console then simply navigate to a directory of your choice and create the project there like so:

~$ cd ~/w
~$ mkdir android_test_project
~$ cd android_test_project
~$ android create project -p test_android_project -n test_project -k com.android.testproject -t 1 -a test_activity

Explanation of arguments:
  -n --name          : Project name.
  -v --gradle-version: Gradle Android plugin version.
  -t --target        : Target ID of the new project. [required]
  -p --path          : The new project's directory. [required]
  -g --gradle        : Use gradle template.
  -k --package       : Android package name for the application. [required]
  -a --activity      : Name of the default Activity that is created.
                       [required]

Compile the project


Here comes the fun part now. Let’s open emacs and start the android emulator by

M-x android-start-emulator

It will prompt you for the Android Virtual Device to run in the emulator. Use the AVD you created in the previous step. Emacs should even provide auto completion for the name of the AVD.

Android Emulator
Android Emulator

Next open the test project’s layout file in Emacs.

C-x C-f ~/w/test_android_project/res/layout/main.xml

You can edit it and add anything you want on the android:text attribute.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Android Emulator says Hello World from Emacs!"
    />
</LinearLayout>

And now to compile the project and install it on the Android Virtual Device currently running on the emulator, you have to invoke ant with the install command and the target argument. For this example let’s consider it a debug target.

M-x android-ant RET debug install

For a succesfull compilation you would get an output similar to the one below:

-*- mode: compilation; default-directory: "~/w/test_android_project/" -*-
Compilation started at Sun Feb  2 16:29:15

ant -e debug install
Buildfile: /home/lefteris/w/test_android_project/build.xml

-set-mode-check:

-set-debug-files:

-check-env:
Android SDK Tools Revision 22.3.0
Installed at /home/lefteris/opt/android

-setup:
Project Name: test_project
Project Type: Application

-set-debug-mode:

-debug-obfuscation-check:

-pre-build:

-build-setup:
Using latest Build Tools: 19.0.1
Resolving Build Target for test_project...
Project Target:   Android 4.4.2
API level:        19
WARNING: No minSdkVersion value set. Application will install on all Android versions.
----------
Creating output directories if needed...
----------
Resolving Dependencies for test_project...
Library dependencies:
No Libraries

------------------
----------
Building Libraries with 'debug'...
No sub-builds to iterate on

-code-gen:
No changes in the AndroidManifest files.
Handling aidl files...
No AIDL files to compile.
----------
Handling RenderScript files...
----------
Handling Resources...
Found modified input file
Generating resource IDs...
----------
Handling BuildConfig class...
No need to generate new BuildConfig.

-pre-compile:

-compile:
Compiling 1 source file to /home/lefteris/w/test_android_project/bin/classes

-post-compile:

-obfuscate:

-dex:
input: /home/lefteris/w/test_android_project/bin/classes
Found modified input file
Converting compiled files and external libraries into /home/lefteris/w/test_android_project/bin/classes.dex...

-crunch:
Crunching PNG Files in source dir: /home/lefteris/w/test_android_project/res
To destination dir: /home/lefteris/w/test_android_project/bin/res
Crunched 0 PNG files to update cache

-package-resources:
Found modified input file
Creating full resource package...

-package:
Found modified input file
Creating test_project-debug-unaligned.apk and signing it with a debug key...

-post-package:

-do-debug:
Running zip align on final apk...
Debug Package: /home/lefteris/w/test_android_project/bin/test_project-debug.apk
Updating property file: /home/lefteris/w/test_android_project/bin/build.prop
Updating property file: /home/lefteris/w/test_android_project/bin/build.prop
Updating property file: /home/lefteris/w/test_android_project/bin/build.prop
Updating property file: /home/lefteris/w/test_android_project/bin/build.prop

-post-build:

debug:

install:
Installing /home/lefteris/w/test_android_project/bin/test_project-debug.apk onto default emulator or device...
947 KB/s (37546 bytes in 0.038s)
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
	pkg: /data/local/tmp/test_project-debug.apk
Success

BUILD SUCCESSFUL
Total time: 7 seconds

Compilation finished at Sun Feb  2 16:29:22

And now if you click on the applications icon inside the emulator and go to the device’s applications you should see one called test_activity with a little Droid as an icon.

Android test_activity
Click on the test_activity application and then you will see the text that you wrote in the main layout xml file

Hello Android from Emacs

That sums up the guide on how to use Emacs for Android development. If you are familiar with the Android SDK you can take it from here and if you are not the web is full of learning material on how to get started with Android development. Android-mode for Emacs is actively developed and will hopefully allows us to develop for Android from inside Emacs for quite some time to come.

Till next time!

7 thoughts on “Development for Android with Emacs”

  1. The most important thing is that there has no api auto tips, will you have any idea for this?

  2. Thank you very much for the step by step guide. I was really itching for a viable way to leverage Emacs in my android development.

  3. How do I test my application on my android phone instead of the emulator?

  4. Thanks as some one that HATES Android Studio, I like this. Up till 2009 I never used an IDE always did Emacs, I did like Eclipse the only IDE that got me to use an IDE. But now that Google has declared Eclipse as “not cool” and unsupported. I have 2 choices 1) use the bloatware Android Studio or go back to Emacs. My choice is Emacs. I wish Google would bring back support for Eclipse as I have come to rely on it, but oh well. I am right now doing some new configuration on Emacs for Android development, I am trying to get autocomplete to work. But thanks for this article on how to get the Android SDK command line tools to integrate into Emacs.

Leave a Reply

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