itt 2014 - niklas therning - truly native java apps on ios with robovm

46
Truly Native Java Apps on iOS with RoboVM Niklas Therning / @ntherning Trillian Mobile AB

Upload: istanbul-tech-talks

Post on 15-Apr-2017

148 views

Category:

Engineering


0 download

TRANSCRIPT

Truly Native Java Apps on

iOS with RoboVM

Niklas Therning / @ntherning Trillian Mobile AB

BackgroundMobile app developers face a big problem

Language API Tools Platform

Building apps for multiple platforms is expensive!

BackgroundiOS development according to Apple:

Xcode

Objective-C

Background

C heritage • NULL dereference from disaster:

int *values = NULL; ... *values = 10; // Crash!

• No array bounds checking int array[10]; ... array[100] = 10; // Danger!

Problems with Objective-C

Background

Extremely dynamic • Objects exchange messages:

Java: obj.method(arg); ObjC: [obj method:arg];

Problems with Objective-C

Number number = Double.valueOf(3.14);for (int i = 0; i< 100000000; i++) { number.doubleValue();}

NSNumber *num = [NSNumber numberWithDouble:3.14];for (int i = 0; i < 100000000; i++) { [num doubleValue];}

Time: 0.54 s

Time: 2.05 s

3.8 x slower!!!

Background

No managed memory • malloc() / free() • CFRetain() / CFRelease() • ARC is great

but doesn’t prevent cycles

Problems with Objective-C

C

A

B

retainCount=1

retainCount=1 retainCount=1

– Bjarne Stroustrup

“There are only two kinds of languages: the ones

people complain about and the ones nobody uses”

The RoboVM project• Goal: Java on iOS

• + other JVM languages: Scala, Clojure, …

• Started in 2010 - open-sourced in Jan 2013

• github.com/robovm

• License allows closed source apps

• www.robovm.com / @robovm

Requirements• A Mac

• Mac OS X 10.9+

• Xcode 5.x

• Oracle’s Java 7 JRE

FeaturesCompiles bytecode into machine code AOT

Main.class RoboVM compiler Main.class.o

FeaturesRuntime classes based on Android

java.lang.Object.class

java.lang.String.class

java.lang.Class.class

RoboVM compiler

java.lang.Object.class.o

java.lang.String.class.o

java.lang.Class.class.o

FeaturesSupports Java Native Interface (JNI)

Linked statically

java.lang.Object.class.o

java.lang.String.class.o

java.lang.Class.class.o

Main.class.o libmyjni.a

Linker MyApp

FeaturesJava to native bridge — Bro

Call native code without JNI@Library("c")public class Abs { static { Bro.bind(); } @Bridge static native int abs(int i); public static void main(String[] args) { System.out.println(abs(-100)); }}

Supports structs, enums, callbacks

FeaturesBindings for CocoaTouch APIs (WIP)

Features• Integrates with popular Java tools

Leiningensbt

Demo #1

Demo #2

Limitations• No runtime bytecode loading

• No dynamic JNI (use static)

• No debugging (yet)

• ~Java6 APIs (Java7 syntax)

• Mac required for iOS apps

Current statusUseable

50+ apps in App Store

RoboVM+libGDX — Java game development framework libgdx.badlogicgames.com

Future• Documentation

• Debugging

• Complete Cocoa Touch Bindings

• Toolchain for Windows/Linux

• Java8 APIs using OpenJDK

• Java8 lambdas

• Interface Builder integration

Questions?