Java Library in an Android library referenced by a

2019-02-27 01:26发布

问题:

I have an Android library project (written by me and I have the sources) which I use in my Android project - so far so good and no problems. But in that library project I am referencing a foreign .jar file (the SDK for the Bixolon Mobile Printer) and it builds fine and I can reference the SDK's method in my code. However at runtime I get this:

   02-16 09:44:43.620: W/dalvikvm(25402): VFY: unable to resolve virtual method 2433: Lcom/bixolon/android/library/BxlService;.GetStatus ()I
    02-16 09:44:43.625: W/dalvikvm(25402): VFY: unable to resolve virtual method 2433: Lcom/bixolon/android/library/BxlService;.GetStatus ()I
    02-16 09:44:43.625: W/dalvikvm(25402): VFY: unable to resolve virtual method 2436: Lcom/bixolon/android/library/BxlService;.MarkFeed (I)I
    02-16 09:44:43.625: W/dalvikvm(25402): VFY: unable to resolve virtual method 2428: Lcom/bixolon/android/library/BxlService;.Directio ([BI[B[I)I
    02-16 09:44:43.630: W/dalvikvm(25402): VFY: unable to resolve virtual method 2437: Lcom/bixolon/android/library/BxlService;.PrintImage (Ljava/lang/String;III)I
    02-16 09:44:43.630: W/dalvikvm(25402): VFY: unable to resolve virtual method 2433: Lcom/bixolon/android/library/BxlService;.GetStatus ()I
    02-16 09:44:43.630: W/dalvikvm(25402): VFY: unable to resolve virtual method 2433: Lcom/bixolon/android/library/BxlService;.GetStatus ()I
    02-16 09:44:43.630: W/dalvikvm(25402): VFY: unable to resolve virtual method 2433: Lcom/bixolon/android/library/BxlService;.GetStatus ()I
    02-16 09:44:43.790: W/dalvikvm(25402): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
    02-16 09:44:43.795: E/AndroidRuntime(25402): FATAL EXCEPTION: main
    02-16 09:44:43.795: E/AndroidRuntime(25402): java.lang.NoClassDefFoundError: com.bixolon.android.library.BxlService
    02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.core.bluetooth.printer.bixolon.BixolonPrinter.connectToPrinter(BixolonPrinter.java:71)
    02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.core.bluetooth.printer.PrintManager.findPrinter(PrintManager.java:101)
    02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.core.bluetooth.printer.PrintManager.getPrinter(PrintManager.java:77)

02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.core.bluetooth.printer.PrintManager.printTestReceipt(PrintManager.java:53)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileoffences.ui.Printer$1.execute(Printer.java:40)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.ui.Dialog.executeActions(Dialog.java:271)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.ui.widgets.DynamicWidget.executeActions(DynamicWidget.java:152)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.ui.widgets.DynamicWidget.executeActions(DynamicWidget.java:138)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at de.softcon.mobileapp.framework.ui.widgets.Button$1.onClick(Button.java:67)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at android.view.View.performClick(View.java:2538)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at android.view.View$PerformClick.run(View.java:9152)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at android.os.Handler.handleCallback(Handler.java:587)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at android.os.Looper.loop(Looper.java:130)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at android.app.ActivityThread.main(ActivityThread.java:3691)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at java.lang.reflect.Method.invokeNative(Native Method)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at java.lang.reflect.Method.invoke(Method.java:507)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
02-16 09:44:43.795: E/AndroidRuntime(25402):    at dalvik.system.NativeStart.main(Native Method)

Which makes me wonder, if the .jar I am referencing in my Android Library project gets deployed correctly... I have a sample Project from Bixolon which directly includes their .jar file (same that I am referencing in my Android Library project) and it works fine. I already made sure the is the same in my own Android Project AND my Android Library Project (as it is defined in the sample Project provided by Bixolon). So I am a bit at a loss now as to what the problem could be. Unfortunately I don't have the source files of that referenced Bixolon SDK .jar - just the .jar.

回答1:

Check out the official dev guide here:

A library project can include a JAR library
You can develop a library project that itself includes a JAR library, however you need to manually edit the dependent application project's build path and add a path to the JAR file.

Besides the library project, you need manually add the jar to your main project build path as well, hope this help.

Update from SDK r17:

This is automatically handled by ADT now, check out new feature for ADT 17.0.0 release here:

Added feature to automatically setup JAR dependencies. Any .jar files in the /libs folder are added to the build configuration (similar to how the Ant build system works). Also, .jar files needed by library projects are also automatically added to projects that depend on those library projects. (more info)