May 11

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.

8 Responses to “NetBeans and Android Tip”

  1. Daniil Says:

    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. Daniil Says:

    I have found the problem.

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

    Daniil

  3. Daniil Says:

    Sorry my previous post is wrong. I still can’t load library because it is not founded.

  4. Daniil Says:

    If you pleas can you provide the step by step, using netbeans and androids ndk.

    Regards,
    Daniil

  5. Daniil Says:

    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

  6. Ivar Grimstad Says:

    I am glad it worked out for you!

  7. Rod Says:

    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 :-)

  8. geek Says:

    Thanks a lot for the tip.
    This helped me to get the physics example of the AndEngine working:
    http://www.spiff.be/node/329

Leave a Reply