PhoneGap/Cordova for Android Developers
Joe Bowser
jbowser@adobe.com
https://github.com/infil00p
@infil00p

About PhoneGap/Cordova

About PhoneGap

  • Started in August 2008
  • First commit on Android Implementation on October 2008
  • Released first code on October 22, 2008 - Same day as the T-Mobile G1
  • Currently supports devices from Android 2.1 to Android 4.1.1+
  • For this talk Apache Cordova == PhoneGap

WTF Adobe???

  • PhoneGap was started by Nitobi
  • Nitobi was acquired by Adobe in October 2011
  • Adobe, IBM and others donated PhoneGap to the Apache Software Foundation

What PhoneGap does

  • Hybrid application framework that allows developers to use HTML, CSS and Javascript to create applications
  • Good for rapid prototyping
  • Lowers the barrier of entry for developers to write apps
  • This is another tool in the toolbox!

WHY?

History

  • In 2008, the iPhone reigned supreme
  • People were already adopting PhoneGap for iOS and didn't care about Android
  • To attract developers to Android, porting had to be easy
  • Android already had WebView and an API
  • PhoneGap 0.1 for Android - October 2008

So, When to use Cordova?

  • Tight timeframe and need to deploy on iOS, Android and Blackberry
  • Common codebase for business logic and UI
  • Easier to implement certain things in JS instead of in Java
  • Allows developers to use existing skills with Mobile Dev
  • No time to redo layout in Android XML

Apps that use Cordova

How does Apache Cordova work?

  • CordovaWebView is a customized WebView
  • Device features can be accessed through a number of bridges
  • Plugins exist on a plugin interface
  • Plugins are written in Java
  • Most projects extend a pre-written activity called DroidGap

DroidGap

  • Horrible Name!
  • This is the activity that everyone inherits
  • This gives users a default layout of just the WebView

CordovaWebView

  • Initially was just a WebView
  • Developers can use Cordova as a component
  • Inherits all the problems with the Android WebView

Thar be code!

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <org.apache.cordova.CordovaWebView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id = "@+id/mainView"/>
</FrameLayout>
            

Plugins

  • Plugins were initially just objects
  • This was tossed because of a desire to use the exec pattern
  • Plugins take in a series of commands, run methods and return the result back to JS
  • Plugins are written in Java

The Bridge

The Bridge

  • Plugins were initially just objects
  • This was tossed because of a desire to use the exec pattern
  • Plugins take in a series of commands, run methods and return the result back to JS
  • Plugins are written in Java
  • We have numerous bridge implementations

Bridge: JS_OBJECT

  • Plugins were initially just objects
  • We used webView.addJavascriptInterface()
  • This has always been prone to bugs
    • Android 1.5 - Foo.bar()
    • Android 2.3.4 emulator - Crashes
  • This is synchronous - Used primarily for JS to Java

Bridge: PROMPT

  • This doesn't crash the emulator
  • This is very slow
  • This is a criticized version of the bridge
  • This is used for JS to Java

Bridge: Polling

  • This is inefficient
  • Check the result from PROMPT or JS_OBJECT on the Interval
  • The old fallback method

Bridge: HANGING_GET

  • This just sucks
  • Set up a Callback Server, and use HANGING_GET to get the results Async
  • This is faster than polling
  • Breaks in Android 4.x
  • Takes up too much memory
  • Deleted in Apache Cordova 2.2

Bridge: LOAD_URL

  • WebView.loadUrl("javascript:foo()") calls foo.
  • This uses the WebKit method stringByEvaluatingJavascriptString
  • This is fast
  • This breaks input methods. No typing on a screen with live Accelerometer data

Bridge: ONLINE_EVENT

  • This is fast
  • Faster than WebView.loadUrl
  • This doesn't break Input
  • This is our current default Java to JS bridge

Bridge: PRIVATE_API

  • This is super fast
  • This isn't supported officially
  • This only works on Android 3.2+
  • This reflects into WebViewCore and talks to WebKit directly
  • This may break in the future

PluginManager and Plugins

  • Plugins were initially just objects
  • This was tossed because of a desire to use the exec pattern
  • Plugins take in a series of commands, run methods and return the result back to JS
  • Plugins are written in Java

Plugin Developers are Android Devs

  • Basic Plugin API
  • Plugins are now CordovaPlugin
  • Plugins must have an execute method
  • Plugins can handle activity results
  • Plugins can create intents

The whole picture

  • WebView runs the app
  • Plugins run on the plugin manager
  • The bridge is used to communicate

Opportunties

  • Contribute to Apache Cordova
    • Work with Adobe, IBM, Google, RIM and others
    • Become an Apache Committer
  • Write kick-ass plugins
  • Use PhoneGap as another tool in the toolbox

Resources