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.
June 14th, 2010 at 3:56 am
Hello, my name is Daniil.
I tries to build the android application using netbeans 6.9 RC2. My application using jni.
I have successfully build the so library and successfully build but when I tries to run I get the exception that library not found?
Can you give some suggest?
How can I see that *.so file is inside apk?
Regards
Daniil
June 14th, 2010 at 4:20 am
I have found the problem.
System.loadLibrary requires the full path to the library. So I use ./lib/armeabi/mylib.so
Daniil
June 14th, 2010 at 6:26 am
Sorry my previous post is wrong. I still can’t load library because it is not founded.
June 14th, 2010 at 6:29 am
If you pleas can you provide the step by step, using netbeans and androids ndk.
Regards,
Daniil
June 14th, 2010 at 6:36 am
Sorry I am too annoying. I fixed the problem. It was my fault.
It should be System.loadLibrary(“utopia”) instead of System.loadLibrary(“./lib/armeabi/utopia”);
Daniil
June 14th, 2010 at 8:13 am
I am glad it worked out for you!
October 24th, 2010 at 9:26 pm
Nice! Thanks, mate. After a week suffering with Eclipse I was about to write my own build file but now I don’t need to
February 28th, 2011 at 7:16 pm
Thanks a lot for the tip.
This helped me to get the physics example of the AndEngine working:
http://www.spiff.be/node/329