Friday, January 25, 2013

AIR Android Native Extensions: Part 4: Using Third Party Libraries (Jar) with your extensions

In my previous post, I explained how to speed up access of resources in an Android ANE.

In this post, I would discuss how to bundle 3rd party JAR library code along with your ANE. There are numerous instances where we need to use JAR files in our code, for example to support an older platform compatible version for an android component, we'd need to use android-support JAR. However, when you export the JAR for your ANE, the code of the other JAR files doesn't go along. And, AIR's ADT command supports only one JAR per ANE? You are stuck because the code will not run without the other JARs.

You can use the source code instead of the JAR. However, not all 3rd party libraries would provide the source code.

Solution: Merge all your JARs into one and package the merged JAR with the ANE. Simple, you can use the following ANT script to achieve it:

<?xml version="1.0" encoding="UTF-8"?>
<project name="OpenPageMobile" default="combine-jars" basedir=".">
<target name="combine-jars">
<mkdir dir="output"/>
<unzip dest="output">
<fileset dir="jars">
<include name="**/*.jar"/>
<delete dir="output/META-INF"/>
<jar destfile="final.jar" basedir="output" />
<delete dir="output"/> 
<!--<delete dir="output"/> -->

So, what does this ANT script do, it takes up all the JARS in the jars directory, extract them, removes the META-INF file and repackage them as a single JAR in the output folder. It's important to remove the META-INF file as it corrupts the merged JAR.

You can now package this merged JAR with your ANE and all would work fine.

Quick solution, but may save you some valuable time.

The next in the blog series is: Exporting xLarge assets with the APK.

No comments:

Post a Comment