AIR 3 Native Extensions for Android - Can I/How to include 3rd party libraries?
Asked Answered
G

2

11

With all the new hype surrounding native extension support in AIR 3, I haven't found a single thing that confirms or denies it is possible to include and use an external JAR inside the native Android implementation.

All of the examples basically showcase the ability to hook into the built-in Android APIs. But what if someone wants to use one of hundreds of libraries that make it easier? Certainly it seems like this should be possible. I'll try to outline what I've done and maybe someone will spot a flaw:

  1. Successfully created native Android library, using compiled-in imports from 3rd party Android library XYZ.
  2. Exported Android project as JAR file. Note: The referenced 3rd party JAR is in /lib inside the jar.
  3. Successfully created ActionScript library (SWC) to interface with native Android library.
  4. Created ANE package from SWC, extension.xml, Android JAR, and library.swc (for platform Android-ARM, extracted from SWC).
    Note: Have also tried putting the 3rd party library in file structure outlined here: http://help.adobe.com/en_US/air/extensions/WSf268776665d7970d-24823354 12ffea65006-8000.html#WSf268776665d7970d-6be13ace1308aaebeca-7fff. I think this is more geared to runtime type execution with JNI or whatever, so that article has left me a little confused.
  5. Used ANE in example Flex Mobile project.
  6. Built and installed APK file on test DroidX device.
  7. Can successfully see AIR application loading (and trace statements) in LogCat "ActivityManager: Displayed com.me.androidapp/.AppEntry"
  8. Can successfully see native Android Java code being invoked via runtime by AIR app to instantiate extension & context. "mye_native: MyExtension.initialize", "mye_native: MyExtensionContext.createContext"
  9. As soon as AIR app tries to call native function, it blows up, not able to find the classes from 3rd party Android library: E dalvikvm: Could not find class 'com.thirdparty.SomeClass', referenced from method com.me.nativeExtentions.MyExtensionFunction.call

Any ideas? It seems like it should work this way.

One thing I have noticed is that when I blow apart the APK and Dex from a pure Java Android app of mine (that also has 3rd party android libraries), it appears that the classes from the 3rd party have been included inside it (not just a jar reference). When I do the same thing with my Flex APK, I find only my own Java classes plus Adobe's, and the 3rd party ones are nowhere to be found that I can see.

I posted this on the Adobe forums, but I thought I'd give the SO think-tank a try. Thank you.

Genny answered 11/10, 2011 at 21:28 Comment(1)
I'm having the same issue as well, hope someone who knows the answer will take notice to this question!Somewhere
R
5

You have to combine all your jars into one. Something like http://code.google.com/p/jarjar/ or your own Ant script will help.

Edited to add example: Suppose your main extension jar file is extension.jar and you are using code in external.jar. Then you can put the classes from external.jar into extension.jar using the Java jar tool:

jar -xf external.jar

This will extract the .class files into package folders. If the top-level package is "com", then you can add those to extension.jar with:

jar -uf extension.jar com

(Repeat the second command for each top-level package in the external jar.)

Rugg answered 1/11, 2011 at 20:37 Comment(4)
I have found this to be true also. I am not sure if the EULAs for 3rd party JARs all support doing this.Genny
Another alternative to combining the JARs is to extract the 3rd party JAR somewhere, and then create a "classes" folder reference to it in the main Android project.Genny
@DerekBromenshenkel can you expand on that alternative? I've extracted the jar and I've added the the folder with the classes to the ANE's native project (with "add External Class folder") instead of the jar file itself; but then the project does not compile at all, it doesn't find the classes..........Lifton
Seems like this should work. I tried it and made the final flex apk with both the JARs combined into the one extension jar file, and yet my app crashes because it cannot find the third party classes. When I extract the combined JAR file, I see that the third party folder is where it should be. Any ideas on what I might be missing? My guess is that the classes are not somehow being converted to dex code.Perpend
O
3

You could also combine the library jars manually by turning them into zip-files (just rename them to .zip) and copy the classes from the 3rd party library into the main one and rename that one back to .jar

Okay answered 8/8, 2012 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.