Wednesday, May 5, 2010

Adndrod development from any text editor in Lunux

I prefer to learn how to create a simple android app bare-bones, from scratch. Eclipse and the ADT plugin are easy but don't give you a good sense of the tools necessary to compile and deploy an android app. By building an app manually, at least once, you become better acquainted with what is happening behind the ADT plugin and will be better prepared to trouble shoot problems.

Luckily, all the tools you need to build an android app with a simple text editor come with the SDK and reside in the tools directory.

For this sample application, I am on a Linux box. You can probably follow these steps, almost exactly on a windows box but you will have to account for path differences and put '.exe' on the end of calls to executables.

You will first need to download the SDK and set up the emulator. I covered that in the privious post. Let get going...


Step 1. Create an android project
# I am in the tools directory of the SDK and I run the following command. The target
# was chosen from the output of 'android list targets' discussed in the previous post.

$> ./android create project --target 3 --name textEditorHelloWorld --path ~/android_projects/textEditorHelloWorld --activity tehello --package com.osilabs.android.projects.tehello

# The following is the output

Created project directory: /home/myuser/android_projects/textEditorHelloWorld
Created directory /home/myuser/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello
Added file /home/myuser/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello/tehello.java
Created directory /home/myuser/android_projects/textEditorHelloWorld/res
Created directory /home/myuser/android_projects/textEditorHelloWorld/bin
Created directory /home/myuser/android_projects/textEditorHelloWorld/libs
Created directory /home/myuser/android_projects/textEditorHelloWorld/res/values
Added file /home/myuser/android_projects/textEditorHelloWorld/res/values/strings.xml
Created directory /home/myuser/android_projects/textEditorHelloWorld/res/layout
Added file /home/myuser/android_projects/textEditorHelloWorld/res/layout/main.xml
Added file /home/myuser/android_projects/textEditorHelloWorld/AndroidManifest.xml
Added file /home/myuser/android_projects/textEditorHelloWorld/build.xml

# These directories were created and populated with the following...

$> ls ~/android_projects/textEditorHelloWorld/
AndroidManifest.xml bin build.properties build.xml default.properties libs local.properties res src

Step 2. Create your hello world code
# Move to the newly created directory which has the sourcecode.

$> cd ~/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello

# Here we see that creating the project also created the java file we will simply modify
# to create our hello world.

$> ls
tehello.java

# It looks like this

$> cat tehello.java
package com.osilabs.android.projects.tehello;

import android.app.Activity;
import android.os.Bundle;

public class tehello extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

# Let's edit it so our hello world app says something. I use gedit, you use
# whatever you want.

$> gedit tehello.java

# I altered the code to the following. I have highlighted in red
# the lines I changed.

$> cat tehello.java
package com.osilabs.android.projects.tehello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class tehello 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("Any program is only as good as it is useful. -L. Torvalds");
setContentView(tv);
}
}

# IMPORTANT: open jdk doesn't work. You must get the sun jdk (or oracle
# jdk, or whoever owns it by the time you read this). I downloaded it
# will probably change with new versions so search for it, find it,
# install it. If you don't, and try to use openjdk, you may be getting errors
# about "java.lang.NoClassDefFoundError: com.android.jarutils.DebugKeyProvider

# After you install the sun jdk, you need to set JAVA_HOME.

$> export JAVA_HOME="/usr/java/latest"

# Better yet, put this in your login script. I use bash so I put it in my ~/.bash_profile
# along with putting it in my path. Something like

PATH=$PATH:/usr/java/latest/bin
export PATH
export JAVA_HOME="/usr/java/latest"

# Yes, /usr/java/latest exists after the install, it is a symbolic link to the most
# recent jdk you have ... I think. I am not a big java guy so this is probably
# true ... probably. :)

# Now compile it in debug mode. This is easier to start with because
# we don't have to worry about signing it yet. Move to the root of the
# project and use ant to compile it. If you don't have ant, get it first.

$> cd ~/android_projects/textEditorHelloWorld
$> ant debug

# Stuff scrolls by, but in the end it says "BUILD SUCCESSFUL", you hope. This
# has created the file you will install, the apk file. I found it here:
# ~/android_projects/textEditorHelloWorld/bin/textEditorHelloWorld-debug.apk


Step 3. Launch the emulator
# If you don't still have it open from previously getting it working, launch it. Give
# time to boot up. First cd back to the root of the android sdk directory, where
# the tools directory is.

$> tools/emulator -avd tools/emulator -avd avd_v1.6_id3 &

# The '&' at the end is optional, I put it there so I could get my terminal back.

Step 4. Install your new app
# Install your cool new app. I first tried without the -s emulator flag and it failed.
# I got the emulator identified '5554' from the top window bar of the running
# emulator. Yours may be the same, may be different.

$> tools/adb -s emulator-5554 install ~/android_projects/textEditorHelloWorld/bin/textEditorHelloWorld-debug.apk

88 KB/s (4544 bytes in 0.050s)
pkg: /data/local/tmp/textEditorHelloWorld-debug.apk
Success

# Now, on the emulator, I pull up the tab with all the applications, and there it is,
# at the bottom of the list, I had to scroll. It is called 'tehello'. Click it to run it.
# I won't tell you what happens, I don't want to spoil the surprise.





Tuesday, May 4, 2010

Create an android development ADV (Android Virtual Device) in Linux

1. Download the android SDK.  Install it somewhere like '~/android/'

2. Create virtual device for emulator

 // cd into the root of the sdk you just downloaded after unzipping it
 $> cd android_sdk_directory

 // Runs GUI to install platforms. You need them to set different 
 //  virtual devices for the different android versions.
 $> tools/android

 // A GUI will come up, go to 'Installed Packages' click 'Update All'.
 //  This should start downloading the SDK Platforms you need.

3.  Get the target ID you would like to use for the emulator.
 //  This shows which target id to use to get the emulator
 //  to simulate different android versions. The target ID is the
 //  number by each 'id:'.
 $>tools/android list targets

4. Create your virtual device for the target ID from above
 // For me, id 3 was for android version 1.6. For you, it may be different.
 $> tools/android create avd -n avd_v1.6_id3 -t 3

 // Just for fun, I will create another virtual device for a different version
 $> tools/android create avd -n avd_v2.1_id6 -t 6

 // It asked me to create a custom hardware profile, I said no. if
 //  I need it later, I wall revisit this, for now, probably fine.

5. Fire up the emulator
 $> tools/emulator -avd avd_v1.6_id3
 // or
 $> tools/emulator -avd avd_v2.1_id6

 // The emulator should fire up and you should be able to take
 //  android for a spin...