Google I/O: How do I code thee? Let me count the ways

Dan Morrill presented a great talk this morning on the 3 different ways to code for Android: Managed Code (Dalvik), Ajax, and Native Code. To demonstrate the different ways Dan decided to write the same app , a k-means clustering application, 3 different ways.

Dan Morrill presented a great talk this morning on the 3 different ways to code for Android: Managed Code (Dalvik), Ajax, and Native Code. To demonstrate the different ways Dan decided to write the same app , a k-means clustering application, 3 different ways.

The first way is managed code. Dalvik is a virtual machine optimized for embedded devices. It's designed to run fast without having to be compiled. Dalvik apps are backed where needed by system infrastructure in native code, for example OpenGL, Binder IPC, media, etc.. Dalvik is the richest form of app development on Android.

Web apps/Ajax apps are broken up into declarative layouts and code. JavaScript mutates the DOM to create UI effects. Recently the canvas tag allows JavaScript to do direct painting. Android 1.5 includes a browser based on WebKit + SquirrelFish. It uses WebKit v528.5, which is equivalent to Safari 4 beta, but due to a bug it reports as 3.1.2 in the user-agent string. The Android browser includes support for the canvas tag and the Google Gears plug-in (version 0.5.17.0 (with location).

Native Code is supported in a Native Development toolKit (NDK) which was recently added to the "Donut" branch in source control. NDK apps are launched as a Dalvik applications that load a dynamic library and then make calls into native code via JNI for heavy lifting. Native code is useful for things like physics simulations, efficient/fast loading of large-ish data files, speed-intensive lookups, and custom VMs like SCUMM.

Dan timed the various implementation to discover their strengths and weaknesses:

  • Dalvik: 18ms render, 1000ms calculate
  • Ajax: 334ms render, 698ms calculate
  • Native: 18ms render, 150ms calculate

He also demonstrated what he called "Amplified Ajax", which is involved extending the Ajax DOM in an Android WebView with either the Dalvik or Native implementations.

During the talk Dan revealed some future directions for Dalvik and Ajax on Android. In the future the Android team is planning to enhance Dalvik by introducing:

  • Improved garbage collection
  • Just-in-Time compilation
  • Optimization in core libraries
  • Additional APIs like Bluetooth, P2P networking, and more

Ajax coders on Android can look forward to:

  • Improved HTML5 implementation
  • Faster JavaScript virtual machines