Sunday, July 14, 2013

Building Androids with Emacs.


title: Building for Androids (With GNU/Linux and Emacs), Part 1.

date: 2013-06-26, 17:32:38

in_menu: false


A screenshot of my phone's OS info

In the most recent holy war to rise from the geek community, I have finally decided which side I stand with: Android is my preferred Mobile OS environment, and iOS is now something I want little to do with.

In the old holy war, the Editor War, I wavered quite a bit. Originally, I actually preferred the simplicity of nano, if You can believe it. However, as I tried out (and I mean, seriously, this is the editor I will be using for the next month tried out) each editor I came across, I eventually found that I had to side with one of the two giants: vi(m) and emacs.

(I thought I held with vi for quite some time… then, I gave emacs an actual, fair go, and I have resigned to using it almost exclusively. I mean, for everything. These Blog Posts, My Web Sites, Music Composition, Graph/Flowchart Making, Organizing my thoughts, Note taking, Writing Novels, Writing Email, GPG, Directory Maintanence… and, of course, for Programming, as was intended.)

But, I was unhappy to find out, Android usually comes bundled with its own editor: Eclipse. I do not like being unable to use my editor of choice, and I ESPECIALLY do not want to use a graphical IDE. Let’s leave it at that, for now, and get to my solution: Setting up A GNU/Linux Installation to be a complete and functional IDE for Android, with Emacs as the editor of choice.

  1. Download and Install the Android SDK for Linux

The tarball, at the time of this writing, is named android-sdk_r22.0.1-linux.tgz. It can be found here.

To install, simply extract it (as root) to somewhere in Your tree. I chose the /opt directory.

#: cd /opt && tar -xvvzf ~/Downloads/android-sdk_r22.0.1-linux.tgz
#: chmod -Rv a+x android-sdk-linux/tools/

I also took the liberty of adding some setup for step 2 here: that last command allows anyone on the system to run any of the tools provided by the android sdk. If this is being installed in a user’s home directory, obviously change that to saner permissions.

  1. Add the tools to the system’s PATH

As I am installing this for the entire system, I wanted to allow the tools to be executed by their names, not their full paths. Therefore, I went to /etc/profile and added this line at the bottom:

export PATH=$PATH:/opt/android-sdk-linux/tools

I added it here, and not at the top of the file with the other exports, in case I ever need to remove it or revert to a stock /etc/profile. It is just safer.

Now, open a new terminal and try typing the following command:

$: android

If it works, You can move on to Step 3. However, if not, then You will have to add a line to the top of each user’s .bashrc file (this should really already be there, because without it the /etc/profile is fairly useless… but for completions sake…)

. /etc/profile

Having added that to Your ~/.bashrc, close and reopen a terminal, and you should be able to run the above command, and proceed to the next step.

  1. Update and finish installing Android’s SDK

After running the android tool, a box should have popped up to have You install a bunch (17 in my case) of packages. For me, 15 of these installed the first time I hit okay, then I had to rerun the install twice for the differing kernels (because they have different licenses).

After all that is done, You can verify everything worked (and check out the digs) by running the following command, which starts the emulator:

$: emulator

Easy to remember, right?

But wait, it didn’t start!

That’s because we need to create a new machine first. You can look up the rules and syntax later, I’ll just say to run the following:

$: android create avd -n test -t 1 --abi armeabi-v7a

And then:

$: emulator @test

Et, voila.

  1. Emacs Mode

Lots of contradictory (and downright incorrect) information exists out there. For me, the easiest solution I could find was to dowload the android-mode file into my .emacs.d directory, add it to be loaded in my init file, and call it a day. All of those tales of snake oil and complete integration are nothing but tomfoolery and bupkus.

$: cd ~/.emacs.d/ && git clone git://github.com/remvee/android-mode.git
$: emacs init.el

And with my setup, it was simply a matter of adding “android-mode” to the load list. For others, it may be something more complex. YMMV.

  1. Test it Out!

Creating a new project from the CLI is an esoteric mess, but not one which is undocumented. Here You Go. For those uninterested at the moment, just type this:

$: cd ~/work/android/projects
$: android create project \
--target 1 \
--name MyAndroidApp \
--path ./MyAndroidAppProject \
--activity MyAndroidAppActivity \
--package com.example.myandroid

After that is finished running, open the file res/layout/main.xml in emacs. And here we go!

Command Test:

Compile! (really, android-ant-debug)

C-c C-c c

Run the Emulator! (android-emulator)

C-c C-c e

Install! (android-ant-installd)

C-c C-c i

And now, You should have an App installed on that Virtual Phone, which is named something like “MyAndroidAppActivity” or “MyAndroidApp.” Run it, and it will display a message. Find that message in the sourcefile which is STILL OPEN AND ACCESSIBLE in the terminal beneath that window, change it, and do a quick recompile and reinstall (C-c C-c c and C-c C-c i) and there You go. It should read whatever You type there.

Anyway, that is as far as I have gotten. I got most of this from an old, no-longer-existing-but-archived-in-the-wayback-machine blog post from 2012. Other sources were the Android Docs, Super User, and Google. I wanted to record my experience here, in the hopes that it might help others interested in setting up Emacs to make Androids.

Time for me to get back to work. Comments are always appreciated!

No comments:

Post a Comment