Get started

Requirements

A Mac with Mac OS X 10.9.

Get Java SE JDK 7 from Oracle.

Get Xcode 5.x from the Mac App Store

Note! The first time you install Xcode and every time you update to a new version you have to open it once to agree to the Xcode terms.

RoboVM for Eclipse

The RoboVM for Eclipse plugin is the quickest way to get started with RoboVM. The plugin is known to work with Eclipse 4.2 (Juno) and later. The example below demonstrates how to create a simple iOS app from within Eclipse. The next section has some more info on what goes on under the hood when you use the RoboVM compiler.

Install the RoboVM for Eclipse plugin from the Eclipse Marketplace or use the following update site:

http://download.robovm.org/eclipse/

Create a new RoboVM iOS Project project. Enter IOSDemo as project name, app name and main class. Enter org.robovm.IOSDemo as app id.

Create a new class and call it IOSDemo with no package name. Copy and paste the code below into the newly created file replacing whatever Eclipse auto generated for you.

import org.robovm.apple.coregraphics.*;
import org.robovm.apple.foundation.*;
import org.robovm.apple.uikit.*;

public class IOSDemo extends UIApplicationDelegateAdapter {

    private UIWindow window = null;
    private int clickCount = 0;

    @Override
    public boolean didFinishLaunching(UIApplication application,
            NSDictionary launchOptions) {
  
        final UIButton button = UIButton.create(UIButtonType.RoundedRect);
        button.setFrame(new CGRect(115.0f, 121.0f, 91.0f, 37.0f));
        button.setTitle("Click me!", UIControlState.Normal);

        button.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {
            @Override
            public void onTouchUpInside(UIControl control, UIEvent event) {
                button.setTitle("Click #" + (++clickCount), UIControlState.Normal);
            }
        });

        window = new UIWindow(UIScreen.getMainScreen().getBounds());
        window.setBackgroundColor(UIColor.colorLightGray());
        window.addSubview(button);
        window.makeKeyAndVisible();
        
        return true;
    }

    public static void main(String[] args) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(args, null, IOSDemo.class);
        pool.close();
    }
}

Finally, launch the app in the iOS simulator by right clicking the project you created and select Run As... → iOS Simulator App.

Using the command line tool

Download the latest RoboVM release and unpack it somewhere in your home folder.

Create HelloWorld.java in a new folder with the following contents:

public class HelloWorld {
    public static void main(String[] args) {
         System.out.println("Hello world!");
     }
}

Compile HelloWorld.java using javac:

mkdir classes
javac -d classes/ HelloWorld.java

Finally, compile the Java bytecode into native code using:

robovm-0.0.11/bin/robovm -verbose -cp classes/ HelloWorld
Note! When you compile a program using RoboVM it will translate not only the main class into native code but also the transitive dependencies of that main class. So the first time you compile a program for a particular CPU and OS RoboVM also compiles many of the standard runtime classes such as java.lang.Object, java.lang.String, etc. A simple class like HelloWorld references about 1500 classes directly or indirectly. RoboVM keeps a cache of compiled classes and only recompiles a class when it or any of its direct dependencies have changed.

You should now have a native executable called HelloWorld/HelloWorld. Since we didn't specify a CPU or OS to the robovm-0.0.11/bin/robovm command the compiler will produce a binary which can be run on the current system. Running it should print:

Hello world!

Let's take a quick peek at the contents of the HelloWorld folder:

HelloWorld/lib/boot/robovm-rt.jar
HelloWorld/lib/classes0.jar
HelloWorld/HelloWorld

This is the final product for this simple console program. The jar files in HelloWorld/lib/boot/ are the jar files which were specified using the -bootclasspath command line argument. Since we didn't specify anything RoboVM will add robovm-rt.jar for us. The jar files directly in HelloWorld/lib are the jar files specified using -cp argument. If one specifies a path (like we did) rather than a jar file RoboVM will archive the contents of that path in a jar file and call it classes0.jar (subsequent paths will be archived to classes1.jar, classes2.jar, etc). The jar files will be stripped from .class files. The reason why the jar files are included by stripped is to make it possible to store property files, images, etc in the classpath rather than in the file system.

Compiling a simple iOS app

While it's nice to be able to make native console programs like we did above the main goal of RoboVM is to make it possible to develop iOS apps in Java. Let's make a very simple iOS app. Put the following into IOSDemo.java:

import org.robovm.apple.coregraphics.*;
import org.robovm.apple.foundation.*;
import org.robovm.apple.uikit.*;

public class IOSDemo extends UIApplicationDelegateAdapter {

    private UIWindow window = null;
    private int clickCount = 0;

    @Override
    public boolean didFinishLaunching(UIApplication application,
            NSDictionary launchOptions) {
  
        final UIButton button = UIButton.create(UIButtonType.RoundedRect);
        button.setFrame(new CGRect(115.0f, 121.0f, 91.0f, 37.0f));
        button.setTitle("Click me!", UIControlState.Normal);

        button.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {
            @Override
            public void onTouchUpInside(UIControl control, UIEvent event) {
                button.setTitle("Click #" + (++clickCount), UIControlState.Normal);
            }
        });

        window = new UIWindow(UIScreen.getMainScreen().getBounds());
        window.setBackgroundColor(UIColor.colorLightGray());
        window.addSubview(button);
        window.makeKeyAndVisible();
        
        return true;
    }

    public static void main(String[] args) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(args, null, IOSDemo.class);
        pool.close();
    }
}

Compile IOSDemo.java using javac:

mkdir classes
javac -cp robovm-0.0.11/lib/robovm-rt.jar:robovm-0.0.11/lib/robovm-objc.jar:robovm-0.0.11/lib/robovm-cocoatouch.jar -d classes/ IOSDemo.java

Compile the Java bytecode into native code and launch the app using:

robovm-0.0.11/bin/robovm -verbose -arch x86 -os ios -cp robovm-0.0.11/lib/robovm-objc.jar:robovm-0.0.11/lib/robovm-cocoatouch.jar:classes/ -run IOSDemo

Building from source

Please see the GitHub project wiki for build instructions.