{"id":614,"date":"2013-05-10T12:48:20","date_gmt":"2013-05-10T10:48:20","guid":{"rendered":"https:\/\/mihosoft.eu\/?p=614"},"modified":"2013-08-08T22:45:26","modified_gmt":"2013-08-08T20:45:26","slug":"java-gradle-for-ios-via-robovm","status":"publish","type":"post","link":"https:\/\/mihosoft.eu\/?p=614","title":{"rendered":"Java &#038; Gradle for iOS via RoboVM"},"content":{"rendered":"<p>Today I was browsing Github for JVMs that run on iOS. I&#8217;d love to eventually be able to run JavaFX applications on iOS.<\/p>\n\n<h2>JVM replacement for iOS<\/h2>\n\n<p>Among others I found <a href=\"http:\/\/www.robovm.org\/\">RoboVM<\/a> 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 <strong>iOS<\/strong>. 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. <\/p>\n\n<h2>Example (taken from RoboVM webpage)<\/h2>\n\n<p><strong>Update:<\/strong> <a href=\"https:\/\/github.com\/miho\/RoboVM-Gradle01\">Github repository<\/a><\/p>\n\n<p>This is an iOS application completely developed in Java:<\/p>\n\n<div id=\"attachment_619\" style=\"width: 710px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/mihosoft.eu\/wp-content\/uploads\/2013\/05\/Bildschirmfoto-2013-05-10-um-12.22.36.png\"><img aria-describedby=\"caption-attachment-619\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/mihosoft.eu\/wp-content\/uploads\/2013\/05\/Bildschirmfoto-2013-05-10-um-12.22.36-1024x750.png\" alt=\"RoboVM iOS Sample App\" width=\"700\" height=\"512\" class=\"size-large wp-image-619\" srcset=\"https:\/\/mihosoft.eu\/wp-content\/uploads\/2013\/05\/Bildschirmfoto-2013-05-10-um-12.22.36-1024x750.png 1024w, https:\/\/mihosoft.eu\/wp-content\/uploads\/2013\/05\/Bildschirmfoto-2013-05-10-um-12.22.36-300x219.png 300w, https:\/\/mihosoft.eu\/wp-content\/uploads\/2013\/05\/Bildschirmfoto-2013-05-10-um-12.22.36-700x513.png 700w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><p id=\"caption-attachment-619\" class=\"wp-caption-text\">RoboVM iOS Sample App<\/p><\/div>\n\n<pre><code>package eu.mihosoft.robovm.gradle01;\n\nimport org.robovm.cocoatouch.coregraphics.*;\nimport org.robovm.cocoatouch.foundation.*;\nimport org.robovm.cocoatouch.uikit.*;\n\npublic class IOSDemo extends UIApplicationDelegate.Adapter {\n\n    private UIWindow window = null;\n    private int clickCount = 0;\n\n    @Override\n    public boolean didFinishLaunching(UIApplication application,\n            NSDictionary launchOptions) {\n\n        final UIButton button = UIButton.fromType(UIButtonType.RoundedRect);\n        button.setFrame(new CGRect(115.0f, 121.0f, 91.0f, 37.0f));\n        button.setTitle(\"Click me!\", UIControlState.Normal);\n\n        button.addOnTouchUpInsideListener(new UIControl.OnTouchUpInsideListener() {\n            @Override\n            public void onTouchUpInside(UIControl control, UIEvent event) {\n                button.setTitle(\"Click #\" + (++clickCount), UIControlState.Normal);\n            }\n        });\n\n        window = new UIWindow(UIScreen.getMainScreen().getBounds());\n        window.setBackgroundColor(UIColor.lightGrayColor());\n        window.addSubview(button);\n        window.makeKeyAndVisible();\n\n        return true;\n    }\n\n    public static void main(String[] args) {\n        NSAutoreleasePool pool = new NSAutoreleasePool();\n        UIApplication.main(args, null, IOSDemo.class);\n        pool.drain();\n    }\n}\n<\/code><\/pre>\n\n<h2>Gradle &amp; NetBeans<\/h2>\n\n<p>I&#8217;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! <\/p>\n\n<p>Here is the build script:<\/p>\n\n<pre><code>apply plugin: 'java'\n\ntask wrapper(type: Wrapper) {\n    gradleVersion = '1.5'\n}\n\n\/\/ robovm home (usually \/opt\/robovm\/)\nproject.ext.set(\"robovm_home\", \"\/opt\/robovm\/\")\n\n\/\/ project dependencies\ndependencies {    \n    compile fileTree (dir: \"$project.robovm_home\/lib\/\", includes: ['*.jar'])\n}\n\n\/\/ source is located in src\nsourceSets {\n    main {\n        java {\n            srcDirs = ['src\/']\n        }\n\n        resources {\n            srcDirs = ['src\/']\n        }\n    }\n\n    test {\n        java {\n            srcDirs = ['test\/']\n        }\n    }\n}\n\n\/\/ main class \nString mainClass = \"eu.mihosoft.robovm.gradle01.IOSDemo\"\n\n\/\/ compile bytecode to llvm and run in the ios simulator\ntask run (dependsOn: compileJava){\n\n    doFirst {\n        println(\"&gt;&gt; Running RoboVM\")\n        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\"\n        def proc = cmd.execute()\n\n        proc.in.eachLine {line -&gt; println line}\n        proc.err.eachLine {line -&gt; System.err.println( 'ERROR: ' + line)}\n        proc.waitFor()\n    }\n}\n<\/code><\/pre>\n\n<h2>Conclusion<\/h2>\n\n<p>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.<\/p>\n\n<p><a href=\"https:\/\/twitter.com\/intent\/user?screen_name=mihosoft\">Follow me on Twitter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I was browsing Github for JVMs that run on iOS. I&#8217;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 &#8230;<\/p>","protected":false},"author":1,"featured_media":619,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[13,129,21,52,1,5],"tags":[48,22,124,59,60,49,58],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/mihosoft.eu\/wp-content\/uploads\/2013\/05\/Bildschirmfoto-2013-05-10-um-12.22.36.png","jetpack_shortlink":"https:\/\/wp.me\/p2P2yR-9U","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts\/614"}],"collection":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=614"}],"version-history":[{"count":13,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts\/614\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/posts\/614\/revisions\/863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=\/wp\/v2\/media\/619"}],"wp:attachment":[{"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mihosoft.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}