novorobo

Apr 01, 2013

Baby's First Android Program

This is a relatively comprehensive walkthrough of setting up an Android development environment without Eclipse. I had never done this before, and I wanted to map out any rough spots that tripped me up. (The entire process was actually much smoother than I expected. I found very little to be missing from the official documentation). For the record, I'm using Xubuntu 12.10, 64-bit.

Prep the Environment

Okay, so first I install the SDK. I figure I'll be using it quite a bit, so I just put it in ~/android, so I won't have to go far. Fire things up, just to see what happens:

$ android/tools/android --help
android/tools/android: 1: android/tools/android: java: not found
android/tools/android: 1: android/tools/android: java: not found
android/tools/android: 110: exec: java: not found

Probably should have seen that coming. Let's not do anything just yet, though. Head back to the setup instructions and see what's next. Oh, it wants me to install java? Beautiful. (Good to know I'm still on the beaten path).

Because I'm using a 64-bit machine, I need this first:

$ sudo apt-get install ia32-libs

And now, the java development kit:

$ sudo apt-get install sun-java6-jdk
Package sun-java6-jdk is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Oops. Licensing issues with Oracle have kept sun-java6-jdk out of the default ubuntu repositories. There are, of course, alternate versions of the jdk available (ie, openjdk), but I'd really like to stick to the letter of the instructions as closely as I can. Otherwise, things tend to start breaking down after a while, and I'm never quite sure where the problem was.

Here we go. It's not exactly the same package name, but it's from the same provider. Also, it scores bonus points for making explicit mention of android developers. We add a new repository, update our package lists to reflect the addition, and install oracle-java6-installer.

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java6-installer

The installer took a little while to pull everything down, and then did something I've never seen before. My terminal filled with an old-school blue background + grey 'window', like the old BIOS interfaces. Turns out to be a license agreement. Click through and we're done. Give android another run- should work now.

$ android/tools/android --help

Excellent. Next step is to fire up the SDK manager and download a set of platform tools.

$ android/tools/android sdk &

Here, we're going to check off "Tools", "Extras > Android Support Library", and at least one version of Android. Install the packages. I had click on "Install X packages..." a couple of times before they all took. That should just about do it for environmental setup.

Aside:

The next page of the docs suggested adding "android/tools" and "android/platform-tools" directly to PATH. Seems like a good idea. I added the following two lines to the bottom of "~/.profile":

PATH="$HOME/android/tools:$PATH" \n PATH="$HOME/android/platform-tools:$PATH"

Note that changes won't be applied until the next time you log in. (Further discussion of PATH, and the various ways to modify it here and here).

Generate a Project

Up to now, I've just been setting up the environment. Now we'll actually generate a Hello World. First, we enumerate the target platforms we just installed.

$ android list targets

We pick out the one we want and grab the ID for the next command.

$ android create project \
> --target 1 \
> --name HelloWorld \
> --path ~/Dropbox/android-dev/HelloWorld \
> --activity MainActivity \
> --package com.babysteps.helloworld

Let's try to break that down some. --target is a target id, which we got beforehand. --path, obviously, where I'm putting it. --package is a standard java namespace. --activity, I honestly haven't got a clue. Anyways, we run this, and a bunch of stuff happens. Whole new directory is created, with a minor boatload of new files. Whee!

Build HelloWorld-debug.apk

Alright. Next up is to get the default test-program that was just generated for us to actually run. First we'll go the emulator route, and then we'll look into how you put something on a real device. Launch the android virtual device manager:

$ android avd &

Click "New...", and fill out the details. I used these settings, but picked them largely at random. We can always get back here anyways. Select the new virtual device and click "Start...". It takes a while to boot up, but everything seems to be working properly. Awesome. Now to build and install the test project.

$ cd ~/Dropbox/android-dev/HelloWorld
$ ant debug

This promptly fails because I haven't got ant. Great. What even IS ant? Great googly moogly! Turns out 'ant' is 'make' for java. Seems straightforward enough, so let's just install it directly. Should probably work straight out of the box.

$ sudo apt-get install ant

Once that's installed, we'll try to build again.

$ ant debug

Which now works. Excellent.

Install HelloWorld on a Virtual Device

From the project directory, I use the android debug bridge to install our newly built app.

$ adb install bin/HelloWorld-debug.apk
error: device not found
- waiting for device -

Oh, right. I should probably actually have my virtual device running. It looks like it'll wait politely for me to start it up, though. Launch the device, let it to boot up...

rm failed for /data/local/tmp/HelloWorld-debug.apk, No such file or directory

Ah well. It was worth a shot. Let's go again from the top. Close the emulator and start it up again. Once it's fully booted, try to install our app.

$ adb install bin/HelloWorld-debug.apk
83 KB/s (4854 bytes in 0.056s)
    pkg: /data/local/tmp/HelloWorld-debug.apk
Success

Booyah. Rummaging around in the apps, I can see my project is nicely installed. It's called 'MainActivity', presumably after the --activity parameter at project creation.

Install on an Actual Device

Okay, on to phase two. I want to install my program on a real device. My test phone is a Motorola Defy, running android 2.2.1. The project I generated earlier was actually aimed at 4.2. I'm not looking to deal with cross-platform issues right now, so to keep things super-simple I'm going to delete that project, download what I need for android 2.2, and generate a new project.

$ android &

I bring up the SDK manager again and scroll through the packages to "Android 2.2 (API 8)". Check it off, and click "Install 3 Packages". Wait while that downloads, and close the window.

$ android list targets

Get myself a fresh list of the targets. The newest download seems to be ID 1, which is convenient. I delete the project folder I generated before and generate a new one in the same place.

$ android create project \
> --target 1 \
> --name HelloWorld \
> --path ~/Dropbox/android-dev/HelloWorld \
> --activity HelloWorld \
> --package com.babysteps.helloworld

This makes me a new project targetting 2.2. Okay. I make myself a new virtual device using 2.2, start it up, build the project and install it exactly as before, and I see that everything still works. Actually, I feel a bit silly now, because the instructions are gloriously similar to what I just did.

  1. Connect the device.
  2. Enable USB Debugging. (For 2.2, go to Settings > Applications > Development)
  3. Build the application and install it exactly as before.

So I navigate to my project folder and do all that stuff again:

$ cd ~/Dropbox/android-dev/HelloWorld
$ ant debug
$ adb install bin/HelloWorld-debug.apk 
error: insufficient permissions for device
Failure [INSTALL_FAILED_ALREADY_EXISTS]

Hrmm. Well that wasn't quite right. Derp- I've forgotten to turn off my virtual device, which already has HelloWorld installed.Shut that down and try again.

$ adb install bin/HelloWorld-debug.apk
error: insufficient permissions for device
- waiting for device -

Hangs there. Okay, let's Give this a quick google. Check this out. A couple of useful new commands, and a suggested solution.

$ adb devices
List of devices attached 
????????????    no permissions

Exactly like the reference. Alright, this is good. Next, we'll follow the general template suggested and get the server running as root. (Apparently there was a server. I didn't notice it, but it started itself and announced its presence when I ran adb the first time).

$ adb kill-server
$ sudo ~/android/platform-tools/adb start-server

(Notice that sudo doesn't have the handy additions I made to PATH, so we have to point directly to adb).

$ adb devices
List of devices attached 
015F1C6808008015    device

That seems to have worked. Beautiful. Let's go back and try to install HelloWorld.

$ adb install bin/HelloWorld-debug.apk

Works flawlessly. We are officially in business.

Victorious Screenshot

posted to: programming > android