Android Studio中运行时.jar库的NoClassDefFoundError

在堆栈溢出嗨朋友

我最近遇到一个问题,当我的Android应用程序中实施Fiksu SDK。 一个简单的介绍,Fiksu SDK是一个注册安装和事件的营销工具。 所有与Fiksu相关的方法都在一个名为“ASOTracking.jar”的.jar文件中。

问题:

该库设置正确,当项目编译时,Gradle没有报告错误,我甚至可以debuggingapk。 但是,它会在运行时尝试初始化SDK时抛出“java.lang.NoClassDefFoundError”


日志:

01-27 14:06:54.801 31166-31166/com.example.myapp I/dalvikvm﹕ Could not find method com.fiksu.asotracking.FiksuDeviceSettingsManager.getInstance, referenced from method com.fiksu.asotracking.FiksuTrackingManager.getClientId 01-27 14:06:54.801 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve static method 13738: Lcom/fiksu/asotracking/FiksuDeviceSettingsManager;.getInstance ()Lcom/fiksu/asotracking/FiksuDeviceSettingsManager; 01-27 14:06:54.801 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0000 01-27 14:06:54.806 31166-31166/com.example.myapp I/dalvikvm﹕ Could not find method com.fiksu.asotracking.FiksuConfigurationManager.getInstance, referenced from method com.fiksu.asotracking.FiksuTrackingManager.initialize 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve static method 13733: Lcom/fiksu/asotracking/FiksuConfigurationManager;.getInstance ()Lcom/fiksu/asotracking/FiksuConfigurationManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000f 01-27 14:06:54.806 31166-31166/com.example.myapp I/dalvikvm﹕ Could not find method com.fiksu.asotracking.FiksuDeviceSettingsManager.getInstance, referenced from method com.fiksu.asotracking.FiksuTrackingManager.isAppTrackingEnabled 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve static method 13738: Lcom/fiksu/asotracking/FiksuDeviceSettingsManager;.getInstance ()Lcom/fiksu/asotracking/FiksuDeviceSettingsManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0000 01-27 14:06:54.806 31166-31166/com.example.myapp I/dalvikvm﹕ Could not find method com.fiksu.asotracking.FiksuDeviceSettingsManager.getInstance, referenced from method com.fiksu.asotracking.FiksuTrackingManager.setAppTrackingEnabled 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve static method 13738: Lcom/fiksu/asotracking/FiksuDeviceSettingsManager;.getInstance ()Lcom/fiksu/asotracking/FiksuDeviceSettingsManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0010 01-27 14:06:54.806 31166-31166/com.example.myapp I/dalvikvm﹕ Could not find method com.fiksu.asotracking.FiksuDeviceSettingsManager.getInstance, referenced from method com.fiksu.asotracking.FiksuTrackingManager.setClientId 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve static method 13738: Lcom/fiksu/asotracking/FiksuDeviceSettingsManager;.getInstance ()Lcom/fiksu/asotracking/FiksuDeviceSettingsManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0010 01-27 14:06:54.806 31166-31166/com.example.myapp I/dalvikvm﹕ Could not find method com.fiksu.asotracking.FiksuConfigurationManager.getInstance, referenced from method com.fiksu.asotracking.FiksuTrackingManager.setDebugModeEnabled 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve static method 13733: Lcom/fiksu/asotracking/FiksuConfigurationManager;.getInstance ()Lcom/fiksu/asotracking/FiksuConfigurationManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0001 01-27 14:06:54.806 31166-31166/com.example.myapp E/dalvikvm﹕ Could not find class 'com.fiksu.asotracking.CustomEventTracker', referenced from method com.fiksu.asotracking.FiksuTrackingManager.uploadCustomEvent 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve new-instance 2112 (Lcom/fiksu/asotracking/CustomEventTracker;) in Lcom/fiksu/asotracking/FiksuTrackingManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x000f 01-27 14:06:54.806 31166-31166/com.example.myapp E/dalvikvm﹕ Could not find class 'com.fiksu.asotracking.PurchaseEventTracker', referenced from method com.fiksu.asotracking.FiksuTrackingManager.uploadPurchase 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve new-instance 2125 (Lcom/fiksu/asotracking/PurchaseEventTracker;) in Lcom/fiksu/asotracking/FiksuTrackingManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0022 01-27 14:06:54.806 31166-31166/com.example.myapp E/dalvikvm﹕ Could not find class 'com.fiksu.asotracking.PurchaseEventTracker', referenced from method com.fiksu.asotracking.FiksuTrackingManager.uploadPurchaseEvent 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve new-instance 2125 (Lcom/fiksu/asotracking/PurchaseEventTracker;) in Lcom/fiksu/asotracking/FiksuTrackingManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0014 01-27 14:06:54.806 31166-31166/com.example.myapp E/dalvikvm﹕ Could not find class 'com.fiksu.asotracking.RegistrationEventTracker', referenced from method com.fiksu.asotracking.FiksuTrackingManager.uploadRegistration 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve new-instance 2126 (Lcom/fiksu/asotracking/RegistrationEventTracker;) in Lcom/fiksu/asotracking/FiksuTrackingManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x001e 01-27 14:06:54.806 31166-31166/com.example.myapp E/dalvikvm﹕ Could not find class 'com.fiksu.asotracking.RegistrationEventTracker', referenced from method com.fiksu.asotracking.FiksuTrackingManager.uploadRegistrationEvent 01-27 14:06:54.806 31166-31166/com.example.myapp W/dalvikvm﹕ VFY: unable to resolve new-instance 2126 (Lcom/fiksu/asotracking/RegistrationEventTracker;) in Lcom/fiksu/asotracking/FiksuTrackingManager; 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0010 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x35ce at 0x2b in Lcom/fiksu/asotracking/FiksuTrackingManager;.initialize 01-27 14:06:54.806 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x35c5 at 0x2e in Lcom/fiksu/asotracking/FiksuTrackingManager;.initialize 01-27 14:06:54.811 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x359b at 0x15 in Lcom/fiksu/asotracking/FiksuTrackingManager;.uploadCustomEvent 01-27 14:06:54.811 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x35cf at 0x30 in Lcom/fiksu/asotracking/FiksuTrackingManager;.uploadPurchase 01-27 14:06:54.811 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x35cf at 0x23 in Lcom/fiksu/asotracking/FiksuTrackingManager;.uploadPurchaseEvent 01-27 14:06:54.811 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x35d1 at 0x26 in Lcom/fiksu/asotracking/FiksuTrackingManager;.uploadRegistration 01-27 14:06:54.811 31166-31166/com.example.myapp I/dalvikvm﹕ DexOpt: unable to optimize static field ref 0x1507 at 0x16 in Lcom/fiksu/asotracking/FiksuTrackingManager;.uploadRegistrationEvent 01-27 14:06:54.811 31166-31166/com.example.myapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x35d1 at 0x19 in Lcom/fiksu/asotracking/FiksuTrackingManager;.uploadRegistrationEvent 01-27 14:06:54.811 31166-31166/com.example.myapp D/AndroidRuntime﹕ Shutting down VM 01-27 14:06:54.811 31166-31166/com.example.myapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41ecb700) 01-27 14:06:54.821 31166-31166/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NoClassDefFoundError: com.fiksu.asotracking.FiksuConfigurationManager at com.fiksu.asotracking.FiksuTrackingManager.initialize(FiksuTrackingManager.java:83) at com.example.userinterface.MyApplication.onCreate(MyApplication.java:56) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4684) at android.app.ActivityThread.access$1400(ActivityThread.java:159) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) at dalvik.system.NativeStart.main(Native Method) 

我的本地环境:

  1. 项目是在Android Studio上开发的
  2. 我正在使用Mac OS X 10.8.5 Mountain Lion
  3. compileSdkVersion 21,buildToolsVersion“21.1.2”,gradleVersion ='2.2.1',com.android.tools.build:gradle:1.0.0
  4. 这是一个多模块项目。 应用程序模块调用“userInterface”库模块,此SDK位于此库模块中。

我试过了:

  1. 我确定SDK(jar)文件已经正确设置,Gradle没有报告编译错误
  2. 我确定在SDK方法被调用的地方没有语法错误
  3. 我检查了调用此SDK的模块的依赖关系,以确保没有依赖关系问题。 我走得这么远,我甚至修剪整个依赖树(我知道这是不必要的)。
  4. 我尝试删除所有模块的“构build”目录并重build整个项目
  5. 我在Android Studio中执行“Invalidate Cache / Restart”操作
  6. 我通过terminal在根目录和应用程序模块目录执行“./gradlew clean”命令。
  7. 我在STO咨询了以下链接,但没有成功:

Android Studio – 导入外部库/ Jar

Android Studio:添加jar作为库?

Android工作室Gradle找不到方法compile()

获取“由:java.lang.VerifyError引起的:”

无法在Android库文件中find类

NoClassDefFoundError与Android Studio中的库


最后的想法:

我需要一些认真的帮助,因为我已经尝试了我所知道的一切 在我从Eclipse迁移这个项目之前,这个SDK工作正常。 现在在Android Studio中运行时会引发奇怪的exception。

最后还有一点是可疑的 – Android Studio中的classpath文件(***。imi文件),我重新安排了它的类path条目,但它仍然不起作用。

谢谢你们的帮助!

Solutions Collecting From Web of "Android Studio中运行时.jar库的NoClassDefFoundError"

新发现

我的GS4(Android 4.3)不能运行这个应用程序,但HTC One M8(Android 5.0.1)可以运行它

根本原因

65K方法限制参考这个文档: https : //developer.android.com/tools/building/multidex.html基本上有一个方法的参考限制,唯一的解决办法是使用multidexconfiguration。 但是,5.0以前和5.0以后的平台都有实现

该解决scheme在两个描述: https : //developer.android.com/tools/building/multidex.html https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html对于我的项目,自从它支持较旧版本的Android,我用“multidex支持库”解决scheme。

  1. 将所有库模块和应用程序模块的构build工具版本设置为最新的21.1.2
  2. 对于具有Application类的模块,添加compile“com.android.support:multidex:1.0.0”作为依赖项
  3. 参考这个链接来修改你的应用程序类。 https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html
  4. 在相同的gradle脚本下,在android – > defaultConfig块下,添加“multiDexEnabled true”
  5. 如果您的系统提供了“Java堆大小”错误,请在应用程序模块的gradle脚本中添加以下内容
 dexOptions { incremental true javaMaxHeapSize "4g" }