The Decaffeinated Robot

Size: px
Start display at page:

Download "The Decaffeinated Robot"

Transcription

1 Developing on without Java Texas Linux Fest 2 April 2011

2 Overview for

3 Why? architecture Decaffeinating for Why? architecture Decaffeinating for

4 Why choose? Why? architecture Decaffeinating for

5 Why choose? is the pragmatic choice for open source mobile. Why? architecture Decaffeinating for

6 Architectural overview Why? architecture Decaffeinating for

7 Architectural overview Why? architecture Decaffeinating for

8 Architectural overview Why? architecture Decaffeinating for

9 Architectural overview Why? architecture Decaffeinating for

10 Architectural overview Why? architecture Decaffeinating for

11 All apps run in a Dalvik VM What does this mean? Why? architecture Decaffeinating for

12 All apps run in a Dalvik VM What does this mean? In order to write apps for, you have to do it within the Java-based environment. Why? architecture Decaffeinating for

13 All apps run in a Dalvik VM What if you do not want to work with Java? Why? architecture Decaffeinating for

14 All apps run in a Dalvik VM What if you do not want to work with Java? Good question. Why? architecture Decaffeinating for

15 Alternatives to Java Build a web app Use a cross-platform tool Use an alternative JVM language Use the for Use the native Why? architecture Decaffeinating for

16 Creating web apps Choosing to write a web app for Creating web apps Choosing to write a web app for

17 What is it? An web app is an application designed to run in a web browser using HTML, CSS, and Javascript. Creating web apps Choosing to write a web app for

18 What is it? An web app is an application designed to run in a web browser using HTML, CSS, and Javascript. Can run in the browser Creating web apps Choosing to write a web app for

19 What is it? An web app is an application designed to run in a web browser using HTML, CSS, and Javascript. Can run in the browser Can run in a browser widget Creating web apps Choosing to write a web app for

20 Leverage HTML5 Cache for off-line use Persistent web storage Video Geolocation Creating web apps Choosing to write a web app for

21 Advantages Use familiar and technologies As close to truly cross-platform as possible Instant updates for all users Creating web apps Choosing to write a web app for

22 Disadvantages Performance No deep integration with the platform No market visibility Not a native experience Creating web apps Choosing to write a web app for

23 Resources -specific web app documentation at guide/webapps/index.html Mobile Boilerplate, a template for creating rich and performant mobile web apps, at http: //html5boilerplate.com/mobile/ Creating web apps Choosing to write a web app for

24 PhoneGap Appcelerator Titanium Rhomobile Rhodes Choosing to use a cross-platform tool for PhoneGap Appcelerator Titanium Rhomobile Rhodes Choosing to use a cross-platform tool for

25 PhoneGap Creates packaged web apps Allows with JavaScript, HTML5, and CSS3 Provides a JavaScript library to expose platform APIs Builds packages for all major platforms MIT license PhoneGap Appcelerator Titanium Rhomobile Rhodes Choosing to use a cross-platform tool for

26 Appcelerator Titanium Provides a JavaScript library to expose platform APIs Builds as a native app for and ios Interprets app logic at runtime Apache license PhoneGap Appcelerator Titanium Rhomobile Rhodes Choosing to use a cross-platform tool for

27 Rhomobile Rhodes Allows in Ruby with an MVC-style framework Builds native apps for all major platforms Integrates with enterprise features from other Rhomobile products MIT license PhoneGap Appcelerator Titanium Rhomobile Rhodes Choosing to use a cross-platform tool for

28 Things to consider Is being dependent on the tool acceptable? What features does your app need? What platforms do you need to support? How important is performance? Is the license compatible with your app? PhoneGap Appcelerator Titanium Rhomobile Rhodes Choosing to use a cross-platform tool for

29 Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

30 good news Java JVM Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

31 (somewhat) bad news JVM Dalvik VM Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

32 Dalvik VM and JVM Incompatible byte codes means no dynamic compilation (no Jython) Staticly compiled and interpreted still work Beware of resource limits Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

33 Ruboto (JRuby) Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

34 Ruboto (JRuby) ruboto-irb Provides IRB console and runs scripts Allows API exploration Available from Market Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

35 Ruboto (JRuby) ruboto-irb Provides IRB console and runs scripts Allows API exploration Available from Market ruboto-core Provides a framework for apps Includes UI setup domain-specific language Uses familiar Ruby MIT license Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

36 Hello, world! require ruboto. rb ruboto_import_widgets : TextView $activity. handle_create do bundle setup_content do text_view : text => Hello, world! end end Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

37 Ruboto impressions Starts slowly Hogs memory Some JRuby features don t work Weird redeploy cycle Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

38 Mirah Ruby-like syntax No runtime library No performance penalty Metaprogrammming and macros Apache license Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

39 Hello, world! import android. app. Activity import android. widget. TextView class HelloActivity < Activity def oncreate ( state ) super state text = TextView.new s e l f text. settext Hello, world! setcontentview text end end Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

40 Mirah impressions Excellent performance Using Pindah makes it easy to get started Approachable for Ruby programmers Young language, little documentation Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

41 Scala Combines functional and object-oriented paradigms Concise syntax Good type inference Used by LinkedIn, Twitter, FourSquare BSD license Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

42 Hello, world! import _root_. android. app. Activity import _root_. android. os. Bundle import _root_. android. widget. TextView class GameChooserActivity extends Activity { override def oncreate ( savedinstancestate : Bundle) { super. oncreate ( savedinstancestate ) val text = new TextView ( this ) text. settext ( Hello, world! ) setcontentview ( text ) } } Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

43 Scala impressions Using simple build tool (sbt) with plugin makes a breeze Very good performance Powerful Approachable to Java programmers Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

44 Clojure A Lisp for the JVM Software transactional memory and concurrency primitives Clojure REPL available on Market Easy interop with Java EPL license Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

45 Hello, world! ( ns HelloWorld ( : gen class : extends android. app. Activity : exposes methods {oncreate superoncreate}) ( : import android. widget. TextView ) ) ( defn oncreate [ this bundle ] ( doto this (. superoncreate bundle ) (. setcontentview ( doto ( TextView. this ) (. settext Hello, world! ) ) ) ) ) Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

46 Clojure impressions Start-up can be slow More memory-intensive than Java, Mirah, and Scala Upcoming leaner runtime should help address performance Neko library should help make faster and more concise Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

47 Kawa Scheme implementation for the JVM Requires no runtime Includes some support built-in MIT license Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

48 Hello, world! ( require android defs ) ( activity HelloKawa ( on create view ( android. widget. TextView ( this ) text : Hello, world! ) ) ) Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

49 Kawa impressions Excellent performance Relatively obscure, small community Not much tool support Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

50 Advantages Gives you access to the full API You can use familiar Some perform as well as Java Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

51 Disadvantages Some have significant overhead May run into interop corner cases Tool support may be lacking for some Ruboto (JRuby) Mirah Scala Clojure Kawa Choosing to use an alternative JVM language for

52 for Developing with SL4A Choosing to use SL4A for Developing with SL4A Choosing to use SL4A

53 for Brings scripting to Loads, edits, and executes scripts Provides interactive consoles Provides access to many APIs Runs interactively, in the background, or via Locale for Developing with SL4A Choosing to use SL4A

54 Supported Beanshell JavaScript (Rhino) JRuby Lua Python Perl PHP Shell Tcl for Developing with SL4A Choosing to use SL4A

55 How SL4A works SL4A Service Contacts Preferences Phone Wifi UI Location Application Perl Battery Camera for Developing with SL4A Choosing to use SL4A

56 How SL4A works Application Application Rhino Python SL4A Service Contacts Preferences Phone Wifi UI Location Application Perl Battery Camera for Developing with SL4A Choosing to use SL4A

57 How SL4A works Application Application Rhino Python JSON RPC SL4A Service Contacts Preferences Phone Wifi UI Location Application Perl Battery Camera for Developing with SL4A Choosing to use SL4A

58 SL4A limitations Generally, access to API is restricted to exposed façades Languages that can invoke Java directly can access full API Designed for ease of use, not performance for Developing with SL4A Choosing to use SL4A

59 Writing scripts Use an interactive terminal and save or the results Use a built-in editor to create scripts Run a server to develop scripts over a network or USB Package script as an app, but still requires separate interpreter installation for Developing with SL4A Choosing to use SL4A

60 Advantages Interactive from a computer is quick and easy Ideal for scripting a device Use a wide variety of for Developing with SL4A Choosing to use SL4A

61 Disadvantages Not ideal for writing : Multistep installation Limited UI options (dialogs and HTML) Cannot access full platform APIs Performance may not be optimal for Developing with SL4A Choosing to use SL4A

62 for Developing with the NDK Choosing to use the NDK for Developing with the NDK Choosing to use the NDK

63 What is it? Designed to allow you to write performance-critical code in C/C++ Supplements the SDK Still runs in the Dalvik VM for Developing with the NDK Choosing to use the NDK

64 Where it fits for Developing with the NDK Choosing to use the NDK

65 Building with the NDK NDK provides: Cross-compiler System libraries and headers Build Debugger NDK creates shared libraries to be included in an app Still use SDK to build packages for Developing with the NDK Choosing to use the NDK

66 Using JNI Java part public class HelloJni extends Activity { public native String stringfromjni ( ) ; static { System. loadlibrary ( hello j n i ) ; } } for Developing with the NDK Choosing to use the NDK

67 Using JNI C part #include<string. h> #include<j n i. h> j s t r i n g Java_com_example_hellojni_HelloJni_stringFromJNI ( JNIEnv* env, jobject thiz ) { return (*env) >NewStringUTF(env, Hello, from JNI! ) ; } for Developing with the NDK Choosing to use the NDK

68 Using JNI Makefile (.mk) LOCAL_PATH := $( c a l l my dir ) include $(CLEAR_VARS) LOCAL_MODULE := hello j n i LOCAL_SRC_FILES := hello j n i. c include $(BUILD_SHARED_LIBRARY) for Developing with the NDK Choosing to use the NDK

69 activities As of Gingerbread (2.3), it is possible to make 100% native activities Interface with the NDK either through callbacks or an event loop UI restricted to OpenGL for Developing with the NDK Choosing to use the NDK

70 Use cases Performance critical code Using an existing library Embed an interpreter (Lua and Gambit Scheme have been done) for Developing with the NDK Choosing to use the NDK

71 for Comparisons for Comparisons

72 About the demo Guess the number with a WarGames twist Code available from GitHub: decafbot Implementations in: Ruboto Mirah Scala Clojure Java with JNI Python with SL4A Not necessarily the absolute best implementations for Comparisons

73 Package size for Comparisons 1. Python size does not include interpreter

74 Installed size for Comparisons 1. Python size does not include interpreter

75 Startup time for Comparisons 1. Ruboto measurement is approximate

76 Heap use for Comparisons 1. portions of Python and JNI are not reported

77 Lines of code for Comparisons

78 for for

79 Is without Java possible? for

80 Is without Java possible? Yes! for

81 Recommendations Mature technologies that have worked well for me or others: apps Scala Technolgies that should run well, but for which may be trickier: Mirah Kawa for

82 Recommendations Technologies that show promise, but aren t entirely ready yet: Clojure Ruboto Technologies that serve a niche: Scripting layer for for

83 Non-free alternatives MonoDroid Adobe AIR for

84 Thank you! Blog for