I now [have]{into-android} a [sim-unlocked]{sim-unlocking-g1} [rooted]{rooting-g1} T-mobile G1 phone running Android. I’ve installed the Cyanogen v3.6.8.1 firmware on it, and upgraded the recovery image to Cyanogen’s pimped out version.

I am really impressed with the cell phone and MID aspects of the device. But I bought it so I could do some hacking.

All right… so what else can I do with this thing? It sounds logical that I now write a Hello world application.

Setting up build environment

Android apps are written in Java and run on a davlik virtual machine. I am still learning, but from what I understand it’s a VM akin to the Java VM but more optimized for embedded devices and using completely incompatible bytecode.

After installing the SDK you will have an emulator and some development tools available in your PATH:

  • android - create and manage AVD (virtual devices) and projects
  • apkbuilder - build a package
  • ddms - a debugger for davlik VMs on emulated or real
  • dmtracedump - parser for trace files
  • draw9patch - tool for converting images into 9-region scalable ones
  • emulator - qemu based emulator
  • hierarchyviewer - interface design viewer tool
  • hprof-conv - don’t know yet
  • mksdcard - create a disk image for the emulator
  • sqlite3 - everyone ships their own sqlilte3 binary :)
  • traceview - log viewer for debugging apps

Before building on Debian, I needed to install the following additional tools:

# apt-get install sun-java6-bin sun-java6-jdk ant ia32-libs

(the ia32-libs is only needed on 64 bit systems)

For whatever reason, I also had to set the JAVA_HOME environment variable like so:

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.14/

(without it, it complained that it was set to /usr/lib/jvm/ia32-java-6-sun-1.6.0.14/jre … note that I just had to remove the jre from what it self detected)

Along with the tools the SDK comes with some activities and tutorials under platforms/android-1.5/samples/, but it’s probably better to follow along with the online on line versions.

Building Hello World

I started by creating a new project:

# android create project --target 1 --path ./test1 --activity Test1 --package net.jukie.test1
# cd test1

I then made some changes to the src/net/jukie/test1/Test1.java app:

  • added import android.widget.TextView; at the top.

  • modified onCreate() to read:

     super.onCreate(savedInstanceState);
     TextView tv = new TextView(this);
     tv.setText("Hello, Android");
     setContentView(tv);
    

And, now to build the sucker:

# ant debug

That gives me a bin/Test1-debug.apk.

Next, I installed the app on my device using:

# adb install bin/Test1-debug.apk
87 KB/s (4468 bytes in 0.049s)
    pkg: /data/local/tmp/Test1-debug.apk
Success

Weee! My first useless app is now running on my Tmobile G1 displaying Hello, Android.

Debugging

Now that my app is running, let’s connect a debugger to it. We will use the ddms tool to redirect a connection to the davlik VM running our process to a local socket. The ddms program assigns a unique port number for a debugger to connect to to each app (or each VM that runs the app). You can see that port in the top left pane of the ddms window. Selecting any application will two numbers in the right pane, in my case it’s 8610 / 8700; the latter is assigned to the currently selected app. Let’s use that.

From the directory in which your project was built, run:

# jdb -attach localhost:8700

You’ll be greeted with a Java debugger prompt. You can now do things like manage breakpoints, inspect objects and thread state, and do other standard debugging things.

Being a C guy at heart, I always found it strange how thread-demanding Java is. The davlik VM seems to be no better:

> threads
Group system:
  (java.lang.Thread)0xc1436ec1e8 <7> Signal Catcher    cond. waiting
  (java.lang.Thread)0xc1427a7240 <5> HeapWorker        cond. waiting
Group main:
  (java.lang.Thread)0xc140019b48 <3> main              cond. waiting
  (java.lang.Thread)0xc1436ee610 <15> Binder Thread #3 running
  (java.lang.Thread)0xc1436ed8b8 <13> Binder Thread #2 running
  (java.lang.Thread)0xc1436ed400 <11> Binder Thread #1 running

… note that this is a Hello world program that doesn’t actually do anything.

Anyway… that’s enough for today.

Resources