Developing "Hello World" on Android using the command line

This little blog entry is a summary of how to build a 'Hello World' Android application on Linux using a command line interface. This approach does not use Eclipse.

1. The first step is to download the SDK from http://developer.android.com/sdk/index.html.

2. Next, use the SDK to download the platforms and platform-tools that you want to develop for. After you are done, add the following directories to your $PATH (e.g. in ~/.bashrc):

  • [PathToYourSDK]/android-sdk-linux_x86/tools
  • [PathToYourSDK]/android-sdk-linux_x86/platform-tools

Naturally, start a new shell to see these changes.

3. You should be able to list the installed platforms ('targets') now via android list targets. On my machine, this yields:

$ android list targets
Available Android targets:
id: 1 or "android-10"
     Name: Android 2.3.3
     Type: Platform
     API level: 10
     Revision: 2
     Skins: WVGA800 (default), QVGA, WQVGA432, HVGA, WQVGA400, WVGA854
id: 2 or "Google Inc.:Google APIs:10"
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Revision: 2
     Description: Android + Google APIs
     Based on Android 2.3.3 (API level 10)
     Libraries:
      * com.android.future.usb.accessory (usb.jar)
          API for USB Accessories
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: WVGA854, WQVGA400, HVGA, WQVGA432, QVGA, WVGA800 (default)

4. You should create at least one "Android Virtual Device" or AVD. The AVD represents a smartphone/tablet/whatever that you will emulate on your development machine. You will upload your applications to that AVD and watch them run (hopefully) in the emulator running your AVD.

Here are two examples of creating different AVD's:

android create avd -n SamsungGalaxyS2 -t 1 --skin WVGA800 --sdcard 1000M

android create avd -n SamsungGalaxyS2GoogleAPI -t 2 --skin WVGA800 --sdcard 1000M

Note that the key difference on the command line is the target that you choose, e.g. -t 1. You will also be prompted for different parameters if you accept the offer to build a 'custom hardware profile'. The defaults are all reasonable. In my case, I chose a custom hardware profile to increase the default RAM to 1024M (the RAM capacity of my own Android-based phone).

Suppose you make a mistake, and write --sdcard 16000M (I'm not admitting that I did that...) so that a massive 16Gb file begins to be created. You will have to control-C out of that (I'm not admitting that I did that...), and then delete the AVD using:

android delete avd SamsungGalaxyS2

So to delete an AVD just refer to its name with the android delete avd command. (I said I'm not admitting that I did that...).

5. You can now run the emulator with your AVD loaded:

emulator -avd SamsungGalaxyS2 -scale 96dpi -dpi-device 217 -no-boot-anim -wipe-data

  • -avd SamsungGalaxyS2: e.g. specify the AVD you want to load and run
  • -scale 96dpi: e.g. the resolution of the fonts being used on your screen (this is the default on gnome)
  • -dpi-device 217: the dpi for the device you are emulating (you can go to www.gsmarena.com and lookup the device you are trying to emulate... and most likely gawk (no, not GNU awk) a bit at all the pretty devices)
  • -no-boot-anim: get rid of the boot animation although even with this, I noticed the start up time is quite slow... perhaps reducing that SD card size would help!!
  • -wipe-data: wipe the previous emulation's stored data - you may not want this option if you want your testing and debugging session to pick up where you left off last time

6. You can connect to the emulator for some simple but essential commands using telnet. The default port is 5554. (Standard warning on firewalls and all that...)

$ telnet localhost 5554
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
help
Android console command help:

    help|h|?         print a list of commands
    event            simulate hardware events
    geo              Geo-location commands
    gsm              GSM related commands
    cdma             CDMA related commands
    kill             kill the emulator instance
    network          manage network settings
    power            power related commands
    quit|exit        quit control session
    redir            manage port redirections
    sms              SMS related commands
    avd              control virtual device execution
    window           manage emulator window
    qemu             QEMU-specific commands
    sensor           manage emulator sensors

try 'help ' for command-specific help
OK

7. Create a project. Android actually comes with a command to create a default project with all the necessary files, which for an old school type, is just ... weird. But anyway:

android create project --target 1 --name HWCLI --path ~/Projects/android/HWCLI --activity HelloWorldActivity --package com.syscs.android.helloworld

  • --target: specifying the target/platform (see above, in my case I have only two platforms installed)
  • --name: the name of the project, in my case an acronym for Hello World Command Line Interface
  • --path: specifies the root directory where the project will be installed
  • --activity: the classname for the main Activity that will be created by default
  • --package: the package prefix to use for your project

And there you go: your 'Hello World' project is created. Of course, it is just source, you still have to compile and package it, but using ant this is also trivial as the next step shows.

8. Build the binaries, package, sign, and align the project... but don't panic. Just go to the root of the project directory (in my case ~/Projects/android/HWCLI) and type:

ant debug

You will need Ant 8 at minimum to do this. You can find it at http://ant.apache.org, but if you're running Ubuntu Linux, just look for the ant81 package - perhaps just ant without 81 on later versions of Ubuntu. I'm running 10.04 which has both ant (version 1.7.1) and ant81 (version 1.8.1). Android will complain if you don't have the latter at minimum.

If everything goes well, a deployable package will be created in the project's bin directory, in my case: ~/Projects/android/HWCLI/bin/HWCLI-debug.apk

To understand more what is ant doing for you, see the following excellent summary. You really can do all this by hand ... but for me, reading about it was enough.

http://developer.android.com/guide/developing/building/index.html

9. Yet another tool, the Android Debug Bridge (adb) will be used to upload the package to your emulator. adb is similar in name to the venerable gdb - the GNU debugger, one of the most useful pieces of software ever written, and almost universally feared by beginning programmers (and not a few advanced ones).

To list the devices (either emulated or real devices connected to your machines), you can do the following:

$ adb list devices
List of devices attached 
emulator-5554	device

To upload the package for immediate trial in your emulator:

adb -s emulator-5554 install ~/Projects/android/HWCLI/bin/HWCLI-debug.apk

Naturally, replace the path after install with your project's path.

If you have a real device hooked up to your PC/laptop/supercomputer, you can also upload the application to it via (notice the -d):

adb -d install ~/Projects/android/HWCLI/bin/HWCLI-debug.apk

To remove the application from your emulator, do the following:

adb -s emulator-5554 uninstall HWCLI-debug.apk

10. Install Eclipse. :-) However, you'll have a much better grasp of what's going on now that you've done this... and in a real production environment, you will need to have a solid software configuration management system in place with continuous integration... and that's where mastering the command line is absolutely critical.

All the best to you!