Ruby on Android — Part I

Of course, now that I have a new G1, I want to start writing apps for it. Android apps are written in Java, but not having touched that since college, I started looking at ways to get ruby onto it instead. There was plenty of chatter about the possibility of running jruby on the device, but there are also frameworks that claim to allow you to write apps in ruby and push them to a variety of mobile platforms (android, iPhone, etc). I’ll be hearing a talk about Appcelerator Titanium at Ruby Hoedown in August, but I haven’t gotten into their closed beta, so I’ll have to wait to try that out. The other (similar) solution I came across was Rhodes, which is available now (so that’s what I’ll start with).

For Android, Rhodes compiles your ruby project to an APK (Android package), which you can push to the Android emulator to test (and eventually to the marketplace). So, before I get to Rhodes, the first step is to set up a local Android development environment. I can skip a lot of the stuff Google suggests, since I won’t actually be doing java work — for this first step, all I want is to be able to take an APK and get it running in a local Android emulator. It took me a while to get this figured out, so hopefully my notes will be helpful to others.

The first step is to download the Android SDK from Google. The installation instructions include a lot of good stuff, but it most of it isn’t needed for what I plan on doing. I already had Java 6 (RE) installed, which seems to work ok. Since I’m on linux, I followed the linux directions (all the way at the bottom), but the general directions for any OS amount to:

  1. Make sure you have Java 1.5 or 1.6 installed
  2. Install (apt-get) ia32-libs if you’re on a 64-bit processor (linux only)
  3. Set up your PATH to include the android tools. (Mac/linux: in your home dir’s .bash_profile, add export PATH=${PATH}:/tools"; Windows: right-click on My Computer, and select Properties. Under the Advanced tab, hit the Environment Variables button, and in the dialog that comes up, double-click on Path (under System Variables). Add the full path to the tools/ directory to the path.)

You can ignore all the stuff about Eclipse.

Once you have all that done, you should be able to type ‘android‘ and see some help text (you may need to ‘source ~/.bash_profile‘, or just open a new terminal to reread your profile and get your new PATH).

The first step is to create a new virtual device. There are currently three versions of the Android software in the SDK: Version 1.1 (the original), Version 1.5 (the current), and Version 1.5 + Google API. You can see your available targets using:

$ android list targets
Available Android targets:
id: 1
Name: Android 1.1
Type: Platform
API level: 2
Skins: HVGA (default), QVGA-P, HVGA-P, HVGA-L, QVGA-L
id: 2
Name: Android 1.5
Type: Platform
API level: 3
Skins: HVGA (default), QVGA-P, HVGA-P, HVGA-L, QVGA-L
id: 3
Name: Google APIs
Type: Add-On
Vendor: Google Inc.
Description: Android + Google APIs
Based on Android 1.5 (API level 3)
* (maps.jar)
API for Google Maps
Skins: QVGA-P, HVGA-L, HVGA (default), QVGA-L, HVGA-P

You can find the details about various options when creating an Android Virtual Device (AVD) here, but the following is good enough to get you started:

$ android create avd -n first_avd15 -t 2
Android 1.5 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
Created AVD 'first_avd15' based on Android 1.5

To start up your virtual device:

$ emulator @first_avd15

(I get an error here about audio input missing, but that may be because I don’t have a mic on this computer.)

Now you should see a red generic Android device, with just the word “ANDROID” in the middle. On my system it takes a while, but eventually you see the “Press menu to unlock” screen, and then you can get to the home screen, where you can do normal Androidy things. To finish the test, grab a simple Android program in APK form (I used this simple app). Download it, and run:

$ adb -e install Constitution_1.0.0.apk (or whichever APK you got)
738 KB/s (534155 bytes in 0.706s)
pkg: /data/local/tmp/Constitution_1.0.0.apk

On your emulator, go back to the home screen, and you’ll now the the app in the lower tray, with everything else, and it should run without incident.

If you have any problems duplicating this process, feel free to ping me (comment on this thread). I worked this out over two nights, and had many false starts, so I could have forgotten to put something that I did to get past a problem.

My next step will be try and write a simple ruby app and get it to an APK, so I can put it on my emulator.

Part 2

Published by

Patrick Schless

I'm a (mostly) ruby/rails developer, living in Chicago. I work at Braintree, and I have lots of side-projects.

6 thoughts on “Ruby on Android — Part I”

  1. kaiiserni — Glad you found it helpful! I've added the next section, though it's more about my difficulty getting Rhodes set up than my successes. Hopefully Part 3 ("It works!") will come along shortly 🙂

  2. We are a group of volunteers and opening a new scheme in our community.
    Your site offered us with valuable information to work on. You’ve done an impressive
    job and our whole community will be thankful to you.

  3. It’s really a great and helpful piece of information.
    I am happy that you just shared this useful info with us.
    Please keep us up to date like this. Thanks for sharing.

    my webpage – vps hosting minecraft (Willis)

  4. I do not know whether it’s just me or if perhaps
    everyone else encountering problems with your site.
    It seems like some of the written text in your posts are running off the
    screen. Can someone else please provide feedback and let me know if this is happening to
    them too? This may be a problem with my web browser because I’ve
    had this happen before. Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *