Thursday, January 24, 2013

AIR Android Native Extensions - Issues and Solutions

In the last few months, I have worked on a lot of native extensions for AIR applications, both Android and iOS. I faced a lot of issues and there was little help available on the web and even if it was available, it was all scattered and not easy to understand.

In this post, I would focus primarily on the issues that I faced when creating Android native extensions. The issues range from trying to bundle resources into a native extension to accessing resources and so on. Some of the issues would have been real show stoppers if they were not resolved. The idea is to share my experience to help others save time.

I assume that the readers are familiar with basics of native extensions and are versed with compiling ANEs.

Let me first list down the issues I am going to talk about in this series of blogs. Series, not to make a single post too lengthy.
  1. Exporting assets / resources with the extension
  2. Accessing R file variables from an extension
  3. Speeding up access of resources in Native extensions
  4. Using third party JARs in your native code
  5. Exporting xLarge assets with the APK
  6. Preventing Android activity from being recreated on orientation change and relaunch
  7. Launching AIR's main activity from other Android activities and passing information


Exporting assets / resources with the extension and common errors
When we create a native extension for Android which contains a view, we generally use layout XMLs to design the layout and some images for buttons and icons and these files are placed in the res folder. By default, you'd tend to export these files along with the JAR and create the ANE. However, the final APK doesn't have any of these assets and your application would crash

To bundle the files along with the APK, we do not need to export the res folder along with the JAR. However, to bundle it with the ANE, we need to copy it separately alongside the JAR and make some change in the way you compile the ANE.

I generally follow the folder structure as follows:
[build]
  extension.xml
  extension.swc
  compile.bat
  [platform]
    [android-arm]
      res
      library.swf
      extension.jar

As you'd notice above, I have created an android-arm folder which has the res folder. The res folder contains the resources for the native extensions. We can name the folder anything, but it's important to keep the three items i.e. res folder, library.swf and extension.jar.
Once we've prepared the folder structure, you can execute a command line script as follows to compile the extension:

adt -package -target ane extension.ane extension.xml -swc extension.swc -platform Android-ARM -C ./platform/Android-ARM/ .

Note: Please note the space and dot (" ." ). It is supposed to be like this only. You can add iPhone platform in the similar way. However, the XIBs in iPhone need to be compiled first.

Doing the above would ensure the resources are bundled with the app. However, please ensure that the you keep unique names for your resource files so that they don't conflict with resources from other extensions. This is essential, otherwise AIR SDK would give errors when compiling when the final app. for example, by default when you create an eclipse project, it creates a file "strings.xml" in res/values folder. Rename the file to "yourproject-strings.xml" or anything unique you can think of. Similarly, the variable names inside the resource files must also be unique. So, if extension 1 defines are resource with id "webview", extension 2 should not use the same name, else you won't be able to compile the final APK. I hope this turns out to be useful for some. Do check out Part 2:Accessing R file variables from an extension.


1 comment:

  1. Gambling Problem with Casino Games - Dr. Martin's Hospital
    Gambling Problem with Casino 제천 출장샵 Games · 2. Gamstop. 1. Online 진주 출장샵 Casino · 3. Casino. 1. Gambling addiction 고양 출장마사지 · 2. The Gambling Problem 용인 출장안마 at a Casino · 3. 순천 출장마사지

    ReplyDelete