Hacking by Walkingice: Exclude dependency jar file from apk when using Ant for Android

2013/05/29

Exclude dependency jar file from apk when using Ant for Android

To put it simply, Android ant packages each jar files under directory libs to the destination apk file, but I don't wanna it. This is just workaround. (I am using Android SDK Tools 22.0.1)

I am writing a simple Bluetooth 4.0 application which base on Samsung series mobile phone. Android is going to support Bluetooth 4.0 but it does not happen so far. That means I still need using Samsung BLE SDK to use Bluetooth 4.0, that's ok.

When I started learning Samsung BLE SDK via attached samples, it works well. It is build-able under Eclipse, it works. When I used Ant to build the apk (yes, I used to use command-line tool to do developing.), it failed.

I got a run-time error:  <A Class> had used a different <B class>; during pre-verification

W/dalvikvm(22070): Class resolved by unexpected DEX: Lcom/samsung/ble/pxpmonitor/ProximityService$3;(0x430e85d0):0x40086000 ref [Lcom/samsung/android/sdk/bt/gatt/BluetoothGattCallback;] Lcom/samsung/android/sdk/bt/gatt/BluetoothGattCallback;(0x430e85d0):0x40038000
W/dalvikvm(22070): (Lcom/samsung/ble/pxpmonitor/ProximityService$3; had used a different Lcom/samsung/android/sdk/bt/gatt/BluetoothGattCallback; during pre-verification)
W/dalvikvm(22070): Unable to resolve superclass of Lcom/samsung/ble/pxpmonitor/ProximityService$3; (60)
W/dalvikvm(22070): Link of class 'Lcom/samsung/ble/pxpmonitor/ProximityService$3;' failed
D/AndroidRuntime(22070): Shutting down VM
W/dalvikvm(22070): threadid=1: thread exiting with uncaught exception (group=0x41f83930)
E/AndroidRuntime(22070): FATAL EXCEPTION: main
E/AndroidRuntime(22070): java.lang.NoClassDefFoundError: com/samsung/ble/pxpmonitor/ProximityService$3
E/AndroidRuntime(22070):        at com.samsung.ble.pxpmonitor.ProximityService.(ProximityService.java:195)
E/AndroidRuntime(22070):        at java.lang.Class.newInstanceImpl(Native Method)
.....(skip)
E/AndroidRuntime(22070):        at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 

Because the apk file I built contains ble_sdk.jar, and Samsung Galaxy S4 has the same (at least, the same name) implementation under /system/framework.

The problem is that I have to build some classes which depend on ble_sdk.jar, but I cannot package the ble_sdk.jar into apk file since it makes duplication.

In general, we put dependency libraries to directory libs, but the dex-helper macro in Android Ant build system merges EVERY JAR FILES INTO classes.dex. Cool, that bites me.


To put the ble_sdk.jar outside directory libs and asks ant-building-system refer to it? it doesn't work, we even cannot override the property jar.libs.dir. (issue #33194)

I tried to figure it out but quit with no luck. If you know a good way match my requirement, please tell me. This is my workaround. (workaround means --- Someday I will bite myself.)

Workaround: Do not package each jar under libs unless I ask you to do that.

Add a custom_rules.xml to your project with these statements.

<project>
<target name="-post-compile">
        <path id="project.all.jars.path">
                <!-- un-comment below line if you want it-->
                <!-- <pathelement path="libs/please_package_this.jar"> -->
        </path>
        <echo level="info">To be packaged jar: ${toString:project.all.jars.path}
</project>


It cleans project.all.jars.path that defines Android ant-building-system's merge targets. I don't like this idea but it basically works.

14 comments:

  1. If you are using "android update project ..." to establish your ant build environment, you may consider to specify the command line option as

    ant -Djava.compiler.classpath=${path-of-the-jar-file} ...

    This just triggers the compile time validation without including the jar into the apk.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information.

    rpa training in chennai | best rpa training in chennai | rpa training institute in chennai | rpa courses in chennai | rpa training in pune | rpa online training | rpa training in bangalore

    ReplyDelete
  4. Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.
    Data Science Training in Chennai | Data Science training in anna nagar
    Data Science training in chennai | Data science training in Bangalore
    Data Science training in marathahalli | Data Science training in btm

    ReplyDelete
  5. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  6. I'm here representing the visitors and readers of your own website say many thanks for many remarkable
    python training Course in chennai
    python training in Bangalore
    Python training institute in kalyan nagar

    ReplyDelete
  7. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
    Best Devops Training in pune

    ReplyDelete
  8. it is vey nice and usefull guide. I alllways fallowe your tips and i inspried with your work and knowedge.if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.https://www.udtapaisa.com/mutual-funds

    ReplyDelete
  9. This is extremely fascinating substance! I have completely delighted in perusing your focuses and have reached the conclusion that you are right about a hefty portion of them. You are extraordinary.  cursos de ti online

    ReplyDelete