Android Phone development from the Linux command-line

From Wikiid
Jump to: navigation, search

Here is how I got the Android "HelloWorld" application to build/run from the command-line under Linux.

 cd /home/android/tools
 activitycreator --out helloproject hello.world.packages.helloActivity
 cd helloproject/src/hello/world/packages/
 vi helloActivity.java
 cd ../../../..
 ant
 cd ..
 emulator &
 adb install helloproject/bin/helloActivity-debug.apk

Step-by-Step

ActivityCreator sets up all of the junk that the system needs - it's recommended that you run it from the android tools directory (why?):

 cd /home/android/tools
 activitycreator --out helloproject hello.world.packages.helloActivity

This creates a directory under tools called 'helloproject' - and under that is a 'src' directory containing hello/world/packages - with 'helloActivity.java' pre-created, ready for editing.

So to edit our single source file application:

 cd helloproject/src/hello/world/packages/
 vi helloActivity.java

The simple program it creates doesn't do anything - so change it to read:

 package hello.world.packages;
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.TextView;
 public class helloActivity extends Activity
 {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      TextView tv = new TextView(this);
      tv.setText("Hello, Android");
      setContentView(tv);
   }
 }

Then, to compile it - head back up to the 'helloProject' directory and use 'ant' (which is like 'make' for Java - and uses 'build.xml' as it's 'Makefile'). The activitycreator made use a build.xml - so we just need:

 cd ../../../..
 ant

(That builds a 'debug' version that's "signed" so it'll only run on your system - to make a released version, you have to build with 'ant Release' - and then mess around with digital signature tools and stuff. More on that on another page in the future!)

Assuming all goes well, we have a 'helloActivity-debug.apk' file in the helloproject/bin directory. Next we go up to the 'tools' directory and start the phone emulator running:

 cd ..
 emulator &

...once it's all started up, we use the 'adb' program to install the executable onto the phone:

 adb install helloproject/bin/helloActivity-debug.apk

The program ends up in the main menu - just tap the screen there and it'll say "Hello Android".

Tadaaaaa!