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 outputCreated project directory: /home/myuser/android_projects/textEditorHelloWorldCreated directory /home/myuser/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehelloAdded file /home/myuser/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello/tehello.javaCreated directory /home/myuser/android_projects/textEditorHelloWorld/resCreated directory /home/myuser/android_projects/textEditorHelloWorld/binCreated directory /home/myuser/android_projects/textEditorHelloWorld/libsCreated directory /home/myuser/android_projects/textEditorHelloWorld/res/valuesAdded file /home/myuser/android_projects/textEditorHelloWorld/res/values/strings.xmlCreated directory /home/myuser/android_projects/textEditorHelloWorld/res/layoutAdded file /home/myuser/android_projects/textEditorHelloWorld/res/layout/main.xmlAdded file /home/myuser/android_projects/textEditorHelloWorld/AndroidManifest.xmlAdded 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
# 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.$> lstehello.java# It looks like this$> cat tehello.javapackage 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. */@Overridepublic 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.javapackage 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. */@Overridepublic 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# from http://java.sun.com/javase/downloads/widget/jdk6.jsp. That path# 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 likePATH=$PATH:/usr/java/latest/binexport PATHexport 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.apk88 KB/s (4544 bytes in 0.050s)pkg: /data/local/tmp/textEditorHelloWorld-debug.apkSuccess# 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.
No comments:
Post a Comment