Tuesday, March 1, 2011

Extracting the AIR for Android Wrapper

When an AIR for Android project is exported as an APK file, the adt tool creates a Java Activity class (air.ProjectName.AppEntry.java) which has all the logic required for bringing up the AIR Runtime and launching the .swf file. It also creates the AndroidManifest.xml file in which the AppEntry class is listed as an Activity with intent filters android.intent.action.MAIN and android.intent.category.LAUNCHER to make it the Activity to be launched on application load.

This is all good if your entire app is summed up within AIR. However, you may want to start up Services that are not exposed through AIR for Android yet (like TextToSpeech) and for this you need to be able to merge the AIR apk with the android apk. Elad Elrom lists the steps involved to do this here.

I used these steps and was able to get a hybrid app going. However there were a few things that I did not like:

  • It was time consuming to unpack, merge, repack, sign, and reinstall the app each time (even with a script).

  • I needed to manually stop the application (if it was running already) before reinstalling it. Killing from the adb shell was not working for me, because, I think, the device was not rooted.



My life would be easier if I could extend the real AppEntry.java directly in my Native Android project. This would let me launch the project directly from the Android Eclipse and it would manage packaging, killing the running process, and re-installing the apk for me. I searched a lot but could not find the source for AppEntry.java anywhere. So here's what I did:

  1. Followed the steps on this page to decompile the classes.dex files into Java code (it got me 80% there).

  2. Edited the generated AppEntry.java code by hand to remove errors and warnings.

  3. Followed the steps on this page to get hold of the correct contents to add to AndroidManifest.xml so that the AIR app renders with the right theme and resolution on the device.



References:
http://kb2.adobe.com/cps/875/cpsid_87562.html
http://blog.webagesolutions.com/archives/223
http://zeaster.blogspot.com/2007/11/how-to-decompile-dex-file-on-android_28.html
http://elromdesign.com/blog/2010/10/29/hacking-native-android-with-air-app-to-allow-air-access-unavailable-apis/