Developing Android applications using NetBeans is usually as easy as stealing candy from a baby. But the last couple of days I have been struggling with an application that uses a couple of external libraries. The other developers (using Eclipse) have a couple of scripts that they run to get the .so files included in the .apk file. When I tried running the same scrips on the .apk generated from NetBeans, the application failed to start in the emulator. I nearly switched to Eclipse (god forbid), but then I saw the light again. As it turns out, NetBeans does not include the java api jar-files in the external libs in the dex-file by default which resulted in a ClassNotFoundException.
The solution is as simple as you would expect when you have used NetBeans for a while. Add the following to the build.xml file in the project root (replace the dummy values for the signjar target):
<target name="-pre-jar">
<copy todir="${build.classes.dir}">
<fileset dir="${external.libs.dir}">
<include name="*.jar"/>
</fileset>
</copy>
</target>
<target name="-post-jar">
<zip update="true" destfile="${dist.apk}">
<zipfileset dir="${external.libs.dir}" includes="*.so" prefix="lib/armeabi"/>
</zip>
<zip destfile="tmp.apk">
<zipfileset src="${dist.apk}">
<exclude name="META-INF/*.*" />
</zipfileset>
</zip>
<move file="tmp.apk" tofile="${dist.apk}" />
<signjar jar="${dist.apk}" alias="alias" storepass="secret" keypass="secret2" keystore="my_keystore"/>
</target>
You also need to add external.libs.dir=<your lib folder> to you <project root>/nbproject/project.properties file.
Now you can install the resulting .apk file using adb install or by running/debugging directly from NetBeans. Remember to follow the tip for asset-files in a previous post if you have that kind of resources.