Java & Gradle for iOS via RoboVM
Today I was browsing Github for JVMs that run on iOS. I’d love to eventually be able to run JavaFX applications on iOS.
JVM replacement for iOS
Among others I found RoboVM which looks very promising. It takes Java bytecode and translates it to native code. This enables applications that have been compiled with RoboVM to run as native applications on Linux/OS X and iOS. RoboVM comes with bindings to the Cocoa API. Therefore, it is possible to create iOS user interfaces. This is really nice. RoboVM allows me to share Java code between Android and iOS.
Example (taken from RoboVM webpage)
Update: Github repository
This is an iOS application completely developed in Java:
package eu.mihosoft.robovm.gradle01;
import org.robovm.cocoatouch.coregraphics.*;
import org.robovm.cocoatouch.foundation.*;
import org.robovm.cocoatouch.uikit.*;
public class IOSDemo extends UIApplicationDelegate.Adapter {
private UIWindow window = null;
private int clickCount = 0;
@Override
public boolean didFinishLaunching(UIApplication application,
NSDictionary launchOptions) {
final UIButton button = UIButton.fromType(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.lightGrayColor());
window.addSubview(button);
window.makeKeyAndVisible();
return true;
}
public static void main(String[] args) {
NSAutoreleasePool pool = new NSAutoreleasePool();
UIApplication.main(args, null, IOSDemo.class);
pool.drain();
}
}
Gradle & NetBeans
I’m a NetBeans fan and recently fell in love with Gradle. I created a simple gradle build script that normally compiles the Java code. But when it runs the application it does it with RoboVM, i.e., in the iOS Simulator!
Here is the build script:
apply plugin: 'java'
task wrapper(type: Wrapper) {
gradleVersion = '1.5'
}
// robovm home (usually /opt/robovm/)
project.ext.set("robovm_home", "/opt/robovm/")
// project dependencies
dependencies {
compile fileTree (dir: "$project.robovm_home/lib/", includes: ['*.jar'])
}
// source is located in src
sourceSets {
main {
java {
srcDirs = ['src/']
}
resources {
srcDirs = ['src/']
}
}
test {
java {
srcDirs = ['test/']
}
}
}
// main class
String mainClass = "eu.mihosoft.robovm.gradle01.IOSDemo"
// compile bytecode to llvm and run in the ios simulator
task run (dependsOn: compileJava){
doFirst {
println(">> Running RoboVM")
String cmd = "$project.robovm_home/bin/robovm -verbose -arch x86 -os ios -cp $project.robovm_home/lib/robovm-objc.jar:$project.robovm_home/lib/robovm-cocoatouch.jar:$projectDir/build/classes/main/ -run $mainClass"
def proc = cmd.execute()
proc.in.eachLine {line -> println line}
proc.err.eachLine {line -> System.err.println( 'ERROR: ' + line)}
proc.waitFor()
}
}
Conclusion
The project is still at the beginning. But I think it has a huge potential. And maybe we can use it for JavaFX applications as well.
Leave a Reply