Getting Started with RubyMotion for Android

Thank you for downloading RubyMotion. This guide will help you get started with RubyMotion for Android.

1. Overview

RubyMotion is a toolchain that permits the development of iOS, OS X and Android applications using the Ruby programming language.

iOS is Apple’s mobile operating system, powering a variety of devices such as the iPhone, iPod touch and iPad. OS X is Apple’s desktop operating system, powering Mac computers such as the iMac or the MacBook Air. Android is Google’s mobile operating system, powering a huge array of devices from different manufacturers.

Developers can write applications for iOS and OS X and submit them to the App Store, Apple’s application distribution system. Alternatively, RubyMotion can be used to conceive applications for Android that can be submitted to the Google Play store.

Conceptually, RubyMotion is a combination of two major components:

  • Runtimes: Brand-new implementations of the Ruby language, tightly integrated with the native Apple or Google runtime and optimized for embedded device constraints.

  • Project Management: A command-line interface to create, manage, and interactively develop RubyMotion projects. It also includes a static compiler that compiles Ruby into optimized machine code and an interactive console where you can evaluate expressions on the fly and change the way your app behaves in real-time.

RubyMotion installs itself into '/Library/RubyMotion'. A symbolic link to the command-line interface is created as '/usr/bin/motion'.

This document focuses on RubyMotion for Android. For iOS and OS X, check out the Getting Started With iOS and OS X document instead.

2. Prerequisites

RubyMotion requires a 64-bit Mac running OS X 10.8.4 or higher. OS X 10.12 Sierra or higher is however recommended.

A proper Android development environment is required in order to use RubyMotion for Android.

2.1. Installing Java

The RubyMotion build system requires a Java compiler to be installed. By default, OS X does not come with Java.

Follow the steps on this page to get Java installed on your environment.

Install Java 1.8 also.

After doing that you can verify that Java has been properly installed:

$ javac -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

2.2. Setup the Android Environment

RubyMotion comes with a single command that will set up a proper Android environment for development.

$ motion android-setup

This command will download both the Android SDK and NDK under the ~/.rubymotion-android directory then run the Android UI tool that will let you chose the versions of Android you want to target.

2.3. Installing a Faster Emulator (Optional)

The built-in Android emulator is too slow to be used for development, but there exists 3rd-party alternatives.

At the time of this writing we recommend the use of Genymotion. RubyMotion is known to work fine with it. Please refer to our Project Management guide to know how to build for an Intel-based emulator.

2.4. Configuring Your Device For Development

You will need a functional Android device configured for development.

In case you haven’t done it already, make sure your device is connected to your Mac via a USB cable, then perform the following steps:

  1. Open the Settings app.

  2. Scroll down and tap on the About phone or About tablet item, depending of the type of the device you are using. This will open a new screen.

  3. Scroll down again and tap 7 times on the Build number item. A message should appear after that, clarifying that you are now in developer mode.

  4. Go back to the previous screen and scroll down again, a Developer options item is now available. Tap on it.

  5. Check the USB debugging item. This will allow your Mac to communicate with the device for development tasks. A window will appear on the device, asking you to authorize the Mac. Make sure to confirm that.

You should now be good to go!

If you are running an older version of Android these steps might not work for you. We recommend that you do a Google search for enable developer mode with your Android version number.

2.5. Text Editor

After having installed RubyMotion you need to set up a text editor for development if you don’t already have a favorite. RubyMotion does not come with a proprietary IDE and lets you use the editor of your choice. Here is a list of Common Editors

3. Software Updates

Software updates can be applied via the command-line.

The following command will grab the latest version of RubyMotion from the network and install it. You must be connected to the Internet to successfully update RubyMotion.

$ sudo motion update

You can run the following command to check the version of RubyMotion installed on your computer.

$ motion --version
5.8

Once a day, the RubyMotion build system pings the software update server in order to see if a new version of RubyMotion is available to install.

If a new version is available RubyMotion prints a message to your terminal suggesting that you to upgrade. The build system will also print a message if your license is about to expire.

Support ~~-

3.1. All Developers

Everyone can find additional help with these resourses:

3.2. Professional and Enterprise Customers

If you are experiencing an issue, would like to request a feature, or simply have a question, you can file a support ticket from the command-line too.

$ motion support

This will open a new window in your browser where you can fill up a support ticket. Your license key and some useful information regarding your environment will be added automatically.

4. Hello World

Let’s create a Hello World app for Android.

Go to a directory of your choice, then type the following command to create a new Android project.

$ motion create --template=android Hello
$ cd Hello
Note
Note that we pass --template=android to the toolchain. By default, RubyMotion will create iOS projects, so we have to select the Android template.

A new Hello directory has been created. Check out the Rakefile and the app/main_activity.rb files.

You can try to run the app on your device. Make sure it’s connected via USB, then run the following command.

$ rake device

The app should start right away (make sure you unlocked the device screen first). The window should be empty, which is expected given that we have not written any code yet.

Now, edit the app/main_activity.rb file and change its content to the following code.

class MainActivity < Android::App::Activity
  def onCreate(savedInstanceState)
    puts "Hello World!"
    super
    view = Android::Widget::TextView.new(self)
    view.text = "Hello World!"
    self.contentView = view
  end
end

If you run rake device again, you should see the hello world message appearing on your device. Also, the same message will be printed on your terminal, due to the #puts call.

5. And Now?

Congratulations, you successfully created your first RubyMotion Android app. That wasn’t too hard, was it?

To continue, we recommend that you check the Samples page as well as the Sample Code Repository on GitHub. Each of the sub-folders contains a RubyMotion project as introduced above. You can type rake in each directory to build and run them and check their source code by reading the files in the 'app' directory.