RubyMotion Blog

Avatar

This is the official blog of RubyMotion, a toolchain for iOS, OS X and Android development that lets you do iPhone, iPad, Mac and Android apps in Ruby.

Follow us on Twitter to stay tuned with everything that's happening in the community!

Not a RubyMotion user yet? Give it a spin today!

RubyMotion 3.0 Sneak Peek: Android Support

It’s very sunny here in San Francisco, California, and we are super excited to give you a first sneak peek at the next major version of RubyMotion, numbered 3.0, which will be released later this year!

Android Support

RubyMotion 3.0 features support for a new mobile platform: Android. You can now write full-fledged Android apps in Ruby instead of Java. If you have a RubyMotion app for iOS or OS X and want to port it to Android, you can even share some of the code! One language, three platforms.

RubyMotion Android projects are created and managed exactly the same way as RubyMotion iOS and OS X projects. The —template=android argument can be passed to motion create to create a new Android project.

$ motion create --template=android Hello
    Create Hello
    Create Hello/.gitignore
    Create Hello/app/main_activity.rb
    Create Hello/Rakefile
    Create Hello/resources
    Create Hello/assets
$ cd Hello
$ rake

The entire project configuration lives in the Rakefile. RubyMotion for Android features a build system driven by Rake tasks, similar to what we already do for the other platforms. You can build and run apps in the Android emulator (rake emulator), on your USB-connected Android device (rake device), and prepare and sign builds suitable for a Google Play submission (rake release).

The AndroidManifest.xml file (similar to iOS’ Info.plist file) is also generated based on the values in the Rakefile.

You can keep using your favorite editor when working on an Android project in RubyMotion: Eclipse is not required nor used in any part of the process. In addition, ctags-based editors will have auto-completion for Java APIs.

Android projects can also vendor 3rd-party Java libraries, similar to how iOS and OS X projects can vendor 3rd-party Objective-C libraries. If you need a MapView in your app, integrating the Google Play Services library is just one app.vendor_project call away.

In a RubyMotion Android projects, raw resource files can be added to the assets directory, while special application-level resources can exist in the resources directory. The build system will make sure these are properly packaged and identifiable via the R class.

Runtime

RubyMotion for Android features a completely new Ruby runtime specifically designed and implemented for Android development. This is a new implementation of the Ruby language, it does not share code with RubyMotion for the Objective-C runtime. We are using the RubySpec project to make sure the runtime behaves as expected.

class MainActivity < Android::App::Activity
  def onCreate(savedInstanceState)
    super

    text = Android::Widget::TextView.new(self)
    text.text = 'Hello World!'
    self.contentView = text
  end
end

The object model of RubyMotion for Android is based on Java. Ruby classes, objects, methods and exceptions are Java classes, objects, methods and exceptions, and vice-versa. No bridge is involved.

The Ruby builtin classes are also based on core Java classes:

This unified runtime approach provides an excellent integration with Java APIs as well as good performance, since objects do not have to be bridged. The runtime uses the Java Native Interface (JNI) in order to integrate with Java, and the entire set of Android APIs is available in Ruby, out of the box.

The memory management is delegated to Dalvik’s generational and concurrent garbage collector. The runtime creates and destroys global references when needed (such as when an instance variable is set), and the destruction of local references is determined at compile time.

The garbage collector is very efficient; it features a per-thread allocation pool for fast allocations, finalizations can happen on dedicated threads, and cycles are properly handled.

Compiler

RubyMotion Android apps are fully compiled into optimized machine code, exactly like their iOS and OS X counterparts.

We feature an LLVM-based static compiler that will transform Ruby source files into ARM machine code. The generated machine code contains functions that conform to JNI so that they can be inserted into the Java runtime as is.

Since Android does not allow classes to be created at runtime, the compiler will emit DEX byte-code for class interfaces, but mark all methods as defined in native code.

The compiler also emits DWARF metadata, which lets us properly symbolicate exceptions, and also allows us to set source-level breakpoints when attaching a debugger.

The build system links all object files with the RubyMotion runtime in order to create an NDK shared library.

RubyMotion Android apps are packaged as .apk archives, exactly like Java-written apps. They weight about 500KB by default and start as fast as Java-written apps. All Android API levels are supported.

Samples

We are extremely excited about RubyMotion for Android. We can finally use the same language to develop an application for multiple platforms, and even if platform-specific code (ex. user interface) will have to be written, we do believe that a significant part of the app, such as the backend, can be shared.

We spent the last days working on sample code apps.

We also created an application for our conference, which has been submitted and is available in the Google Play store. If you have an Android device, check it out! The source code will be published on GitHub right after the conference.

We Need Your Help

RubyMotion 3.0 is not ready for prime-time yet. We are working hard on improving and polishing it so that it can be delivered to everyone.

The reason we are giving you a sneak peek today is because we can’t achieve that without you. RubyMotion for Android features a brand-new implementation of Ruby that will require significant testing before it can be as mature as the existing RubyMotion platforms support.

We are looking for volunteers interested in accessing early builds of RubyMotion 3.0, testing the new features and giving us continuous feedback so that we can quickly iterate.

Are you the author of a RubyMotion gem that you think would make sense to exist for Android? Do you have an app in the App Store that you would like to port and submit to the Google Play Store? Are you willing to test early builds of RubyMotion for Android, report bugs and work with us? We need your help.

If you are interested, please fill out this form. We will only approve a small amount of developers for the beta, and we will start seeding the first builds in early July. Thanks a lot!


Interested in giving RubyMotion a try? We offer a 30 days money back guarantee. Download it today!
Follow us on Twitter to stay tuned with everything that is happening in the community.

By Laurent Sansonetti.