5 Quick Android Dev Tips
Posted by ch0wda on Sep 05, 2008
I’ve recently been doing some development with Google Android, and I must say that I’m really enjoying it. It really seems like there are a lot of untapped possibilities. I’m currently using Eclipse for development, and the official Android plugin for it. It’s the path of least resistance. Here’s a collection of 5 things that I’ve found helpful to get up and running.
Add The Android SDK To Your Path
The SDK is a minimal collection of tools and jar files. I normally keep all my various code in a directory directly under $HOME called /projects, I’ll refer to it as $PROJECTS from now on. I’m a total n00b when it comes to Java development, so I don’t know what other people do for these types of things, but I’ve added a folder to keep all things like the Android SDK called $PROJECTS/.java.
bash-3.2$ wget http://dl.google.com/android/android-sdk-mac_x86-0.9_beta.zip
Unzip this file and symlink it to @$PROJECTS/.java/android, you’ll see why in a minute. Please note that you’ll need a different file unless you’re using a Mac.
bash-3.2$ ln -s $PROJECTS/.java/android-sdk-mac_x86-0.9_beta $PROJECTS/.java/android
The final step is to add the SDK to your $PATH so you can access the command line goodies. If you remember, I symlink’d the current release of the Android SDK to $PROJECTS/.java/android. When the next version of Android is released I can simply download it and change the symlink and I won’t have to change my paths. This will make testing my software against various releases extremely simple. Your platform might be different, but on mine, I can change $PATH by editing $HOME/.profile. The actual files that you will need accessible from your path are at $PROJECTS/.java/android/tools, so that’s the value I’m adding to the end of my path.
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/mysql/bin::$HOME/.java/android/tools:$PATH
You should know be able to access any of the pre-bundled command line tools for the Android SDK in a simple and flexible manner.
Creating An SDCard For Your Emulator
There are times that you want to store some data or media that can act in the same way as an SDCard. The SDK has a tool that you can use called mksdcard which can be used to create an SDCard iso, which can then be mounted on the emulator. Creating one is simple. I recommend setting up a common folder for assets that you might want to use across your various projects. In my case, that would be at $PROJECTS/.java/shared. Here’s a listing of the switches for this tool:
mksdcard [-l label] <size>[K|M] <file>
In my case, I wanted a simple 1GB card that I could use for various purposes. I’m not going to give the volume a label, although I certainly could in the future.
mksdcard 1024M $PROJECTS/.java/shared/sdcard1.iso
Now I just need to make sure that this SDCard gets loaded onto the emulator when my Activity gets run. In my case,
-sdcard $PROJECTS/.java/shared/sdcard1.iso
One really useful thing is the ability to preload data onto the SDCard file, which can be done by mounting the file using Finder and drag-and-drop. If you’re into command-line 1337-ness, push files using the adb tool included with the SDK or something like mtools. I tried to do it that way but couldn’t find get it to work after 5 minutes, so I moved on. I loaded a simple mp3 file onto my SDCard image and was immediately able to play it.
Create Emulator Data Profiles
By default, the emulator stores data in 2 places, a user-specific directory that will keep track of your settings and preferences in-between emulator sessions, and an optional SDCard that you pass in. ( See previous tip for SDCard emulation. ) The first of these is normally stored at $HOME/.android, this file will include various configurations, but also your contact list and various debugging files. If you don’t like this location, you can pass an option to the compiler that will use a different directory. One thing that I’ve done is tried to create a couple of different user data sets, one a little more complete than the other to get a better feel for how my applications will look with a blank slate, but also with a little more data. I’m going to store these in $PROJECTS/.java/shared/min.img and $PROJECTS/.java/shared/max_data.img, respectively. To pass one or the other to the compiler, do this:
-data $PROJECTS/.java/shared/max_data.img
Using this in combination with the tip above should allow you to maintain maximum flexibility, but maintain fairly complex data sets for testing our software. For a full list of emulator options, check out the Android Emulator Reference.
Update Other Application Onto An Emulator
There are already some really great application available for Android, perhaps you want to build on top of some existing applications or you’re trying to see how resource intensive things are going to be in anticipation of the release of the first Android-enabled phone. One of the very first things that I’m going to want for my phone will be an SSH-client. Connectbot is a secure shell client for Android. I’m going to download the .apk file so I can use it. I could also check out the source from subversion and build it, but I don’t necessarily want to do Connectbot development at this point, just test it out.
bash-3.2$ cd $PROJECTS/.java/shared && wget http://connectbot.googlecode.com/files/SSH.apk
Now I need to start my emulator, I’m going to be loading the application while it’s running. Once running, it’s as simple as using the adb tool.
bash-3.2$ adb install SSH.apk
1650 KB/s (190790 bytes in 0.112s)
pkg: /data/local/tmp/SSH.apk
Success
Now, I can go to the Menu on the emulator and I should be able to use Connectbot. It worked the first time for me. Note that you do not have to do this each time, the application will persist until you remove it with adb. Also with your emulator running, you will need to start the adb shell and navigate to data/app and remove the application.
bash-3.2$ adb shell
The adb shell is similar to the concept of SSH’ing into a server, except it’s your phone. You can take a look at the filesystem and browse around. I highly recommend familiarizing yourself with the layout of an Android device.
Good Android Resources
I’ve found that solid Android resources online are fairly hard to come by, so here’s a list to get you started. I’ve also found that Google code is a much better place to find projects than Github, for the moment.
- Apps for Android – a great collection of sample Android applications
- Android Standard Reference
- Common Android Howtos
- Hello, Android: Introducing Google’s Mobile Development Platform by Ed Burnette, a Pragmatic Programmer book currently in beta
- Android Developers Mailing List
- The Android Wiki
- Emdroid.el – a nice starting point to transition from Eclipse to Emacs, aka my next step.
- HelloAndroid.com – a fairly active community site full of tutorials, and lists of applications
- AndroidGuys.com – supposedly the site for the best Android news, including a podcast that I have yet to listen to
- TalkAndroid.com – another active community site
- Run Scala on Android
Hopefully, this provides you enough incentive to get going on your own. Remember, you are only limited by your imagination. The next area that I’m going to investigate it how to write tests for Android applications, I’m hoping it’s not too difficult.
