NetBeans and Android Tip

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.

2 thoughts on “NetBeans and Android Tip

  1. 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

  2. I have found the problem.

    System.loadLibrary requires the full path to the library. So I use ./lib/armeabi/mylib.so

    Daniil

Leave a Reply to Daniil Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.