Home / Community
Home / Community

Android applications with Ruby

B07554b05ba6bf78ffed784fef5c496dby Paul Estrada|

Dec 19, 2012


I have been coding on the Android platform since it was first released back in 2008. As you probably know, the programming language of choice for Android development is Java.

However, I have been using Ruby for my everyday coding activities for a while now. Thus, I have been looking for a feasible way to use Ruby with Android.

After some research, I found this great Gem called Ruboto.

Ruboto makes use of JRuby to compile Ruby scripts into an Android application. I built a sample application and now I am going to show you how to develop with Ruboto.

System requirements

You must have installed on your system:

  • Java JDK (I used the Oracle Java 7 JDK)
  • JRuby, which you can easily install using RVM: $ rvm install jruby
  • Android SDK Tools
  • Add the Android SDK’s /tools and /platform-tools directories to your $PATH. Run in your terminal:

$ echo 'export PATH=${PATH}:~/android-sdk-linux/tools:~/android-sdk-linux/platform-tools' >> .bashrc

Then, to load these changes, run: $ source .bashrc

Creating the application

Note: For all the commands of Ruboto to work properly, make sure you are using JRuby as your current Ruby VM.

To create a Ruboto application, you have to run:

$ ruboto gen app --package your.package.name --target android-version

This will create a new directory named with the last part of your 'package name' and the android version specified with '--target'

For instance: $ ruboto gen app --package org.sample.rubotoapp --target android-17

This will create a directory named 'rubotoapp' targeted only to 'android-17' (i.e., API level 17, Android 4.2 Jelly Bean). Now just switch to your application directory:

$ cd your-ruboto-app-directory

The main activity of the application resides on your application ‘/src’ directory.

This is my main activity file '/src/rubotoapp_activity.rb' (The file is named with your application name):

require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :Button, :LinearLayout, :TextView

class RubotoappActivity
  def on_create(bundle)
    set_title 'Ruboto App Sample'
    self.content_view = linear_layout :orientation => :vertical do
      @text_view = text_view :text => 'Ruby on Android rocks!', :id => 42,
                             :width => :match_parent, :gravity => :center, 
                             :text_size => 48.0
      button :text => 'Touch here', :width => :match_parent, id => 43,
             :on_click_listener => proc { awesome_method }
    puts "Exception creating activity: #{$!}"
    puts $!.backtrace.join("\n")

  def awesome_method
    @text_view.text = 'Doing something awesome'
    toast 'Start hacking with Ruboto'

You need to make sure ANT is installed on your system. If not, then install it this way:

$ sudo apt-get install ant

Start your Android Emulator, passing the name of your AVD as follows (replacing ‘android4’ with your AVD name):

$ emulator -avd android4

If you can not start the emulator this way, try to use the command directly from your Android SDK Tools path:

/your_android_sdk_path/tools $ android emulator -avd android4

You have to wait until the emulator is totally loaded before proceeding with the next steps, that is, when the system icons are visible on the emulator’s screen.

Installing on the emulator/device

Note: The first time you install a Ruboto application on the device/emulator, Ruboto will ask for you to install the Ruboto Core Platform. Just follow the download link and install the Ruboto platform.

With the emulator running, you can install and start your shiny new Ruboto application with:

your_app_directory $ rake install start

And now, brace yourself to behold your first Ruby App on Android: \(ºoº)/

image alt

From now on, you can edit the main activity of your application and start developing something awesome.

After you make changes to the source file, you can update and restart the application on your device/emulator, using:

your_app_directory $ rake install:clean start

And that is it. You can start hacking with Ruby on Android. I suggest you to visit the Ruboto repository for more information about specific Android API functionality. Finally, don’t forget to say:

Domo Arigato Mr. Ruboto !

blog comments powered byDisqus