Sunday 6 February 2011

Part 2: That Sample Application

Index:
1. First dabble with Google Android
2. That Sample
3. TipCalc Sample

Summarising my previous post, I decided to try out this sample as an introduction to the Android SDK. Let me first give some credit to this sample blog, it gave some very good pointers and I greatly appreciate the help it provided. My blog is not meant to be taking any glory from anyone else, but instead document my journey so that others can learn alongside me. So, see this entry into my blog as "additional help" to the sample blog. Hopefully once I get the hang of the Android SDK, I will be able to contribute my own original ideas.

I started by downloading the Android SDK, Apache Ant, Java JDK and DroidDraw. So far so good. It then became clear that things has changed since sample blog was posted. A few deviations I had to take from the scripted sample:

1) I had some trouble running the "emulator.exe" application, but after some playing around, I found that the emulator can be started from within the SDK Manager (c:\tools\android\). From there, you can select the top most menu item (Virtual Devices) and create a new AVD (Android Virtual Device) using the version of the SDK you specify. I used 2.1 because my version of Android on my Galaxy S was still running Eclair. Once created, simply select the new AVD and click Start.

2) The next place I hit a wall was running "activitycreator.bat", it simply did not exist in the directory specified in the blog. Reading through the comments, I found that activitycreator.bat has been replaced by android.bat. So, as stated in the comments, I ran "android create project --activity tipcalc --target 2 --path ./tipcalc --package com.andriod.tipcalc". One thing to note here is the value of "target", it is 1 in my case, but it refers to the ordinal that your version of the AVD is pointing to (2,1 in my case). This can be found by typing "android list" in a command prompt.

3) Another issue was with the path of java (aka JAVA_HOME). The blog says to put java in c:\tools\java, problem is that Java automatically installs itself in c:\program files\java. Because of this, I didn't want to move the Java directory anywhere else, in case another application relies on it being in Program files. This meant that I simply had to change my environment variable for JAVA_HOME to "c:\ Program Filed\Java\jdk1.6.0_23", which I did. But for some reason I kept on getting this error, when running ant. Turns out that the issue wasn't the path, but the fact that the command prompt does not refresh it's environment variables automatically, so simply opening up another cmd refreshed the environment variables for that cmd session. The simplest way to check this is to simply type "set" in the command prompt and see what the value for JAVA_HOME is. There is probably a way to force cmd to refresh it's environment variables without restarting cmd, but since this was not a too important issue, I am not going to try and figure that out. I am trying to write an Android app, after all.

4) After all that, ant didn't really compile the project as expected (I am starting to think this this sample is not the best place to start). So, instead of just running "ant", I ran "ant compile". The ant compiler seemed to compile the project, even though it gave some compile errors. I am going to assume that these errors were actually warnings, but may revise that opinion soon. (Update: looks there are easier ways to do this, see my next blog for more details)

5) Next issue was that the location of adb has changed from \android\tools to \android\platform-tools. This is specified in a text document called "adb_has_moved.txt" in the \android\tools directory. I followed the instructions in that text file and added "c:\tools\android\platform-tools" to my PATH variable so that it is easier to execute it in the future. This once again required a cmd restart :(. But after a cmd restart, I was able to run "adb install \bin\tipcalc-debug.apk". This installed the application on the emulator. (Update: looks like there are easier ways to do this, see my next blog for more details)

So, finally I have my Hello World application on the emulator. Now to run it. So I open the menu on the emulator and Eureka, the application is on the device. Running it returns the default "Hello World" application. Mission accomplished!!

So, in conclusion, a lot seems to have changed in the SDK since this sample blog was written. the blog does not have a created date, but the comments on the blog indicates it was written in late 2008. Maybe in hindsight I should have checked how "out of date" the blog was before I started this blog. But at least I learned a little bit more than I would have if the blog was up to date.

In my next post I will focus and break down the actual code used in the TipCalc sample

3 comments:

  1. Thanks for this update. I ran across the sample code too and realized that it was a couple of years old. I have not tried the code yet, but was thinking about it. You have saved me some time by making corrections (and I don't have your qualifications, so I may not have been able to overcome the problems). I look forward to following your next post.

    ReplyDelete
  2. Hi David,

    Thanks for the feedback. I have completed the second half of the sample (the actual TipCalc application) and it went a lot smoother than this section. I will post a blog entry on it in the comming days.

    Thanks
    Paul

    ReplyDelete