Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError for .jar library at runtime in Android Studio

Tags:

Hi Friends at Stack Overflow

I recently run into a problem when implementing Fiksu SDK in my Android app. A brief intro, Fiksu SDK is a marketing tool to register installation and events. All the methods related to Fiksu is in a .jar file called "ASOTracking.jar"

The problem:

The library is setup correctly, there is no error reported by Gradle when project compiles and I can even make debug apk. However, it throws "java.lang.NoClassDefFoundError" at runtime when it tries to initialize the SDK.


The log:

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)

My local environment:

  1. Project is developed on Android Studio
  2. I'm using 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. This is a multi-module project. Application module calls "userInterface" library module and this SDK lies in this library module.

What I've tried:

  1. I do make sure the SDK (jar) file is properly setup and there is no compile error reported by Gradle
  2. I make sure there is no syntax error at the places where SDK methods is called
  3. I checked the dependency of the module that calls this SDK to make sure there is no dependency problem. I go so far that I even trim the whole dependency tree (I know it's unnecessary).
  4. I tried deleting the "build" directory of all modules and rebuild the whole project
  5. I performed the "Invalidate Cache/Restart" operation in Android Studio
  6. I performed "./gradlew clean" command via Terminal at both root directory and application module directory.
  7. I consulted the following links in STO with no success:

Android Studio - Importing external Library/Jar

Android Studio: Add jar as library?

Android studio Gradle Could not find method compile()

Getting "Caused by: java.lang.VerifyError:"

Unable to find classes in Android library file

NoClassDefFoundError with libraries in Android Studio


Final thoughts:

I need some serious help because I've tried everything I know. This SDK worked fine before I migrated this project from Eclipse. Now it throws weird exception when running in Android Studio.

There is one last thing that's kinda suspicious --- the classpath file in Android Studio (the ***.imi file), I re-arrange the classpath entries in it and it still doesn't work.

Thank you guys advance for your help!

like image 946
Shawn Lu Avatar asked Feb 02 '15 17:02

Shawn Lu


People also ask

How to fix Java lang NoClassDefFoundError in Java?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

Why I am getting NoClassDefFoundError?

Just like ClassNotFoundException, NoClassDefFoundError occurs at runtime. We get this error when the class is not available in the program at runtime. It is an unchecked exception which a program throws when the requested class is not present at runtime.

Can use Java library in Android Studio?

You can now use your Java library. Remember to create a native bridge file for your Java library if you do not have one yet. For more information, see Using a Custom Set of Java Libraries In Your RAD Studio Android Apps.

What is Java lang NoClassDefFoundError?

java. lang. NoClassDefFoundError is runtime error thrown when a required class is not found in the classpath and hence JVM is unable to load it into memory.


1 Answers

New Findings

My GS4 (Android 4.3) cannot run this app, but HTC One M8 (Android 5.0.1) can run it

Root Cause

65K methods limit Refer to this document: https://developer.android.com/tools/building/multidex.html Basically there is a methods reference limit and the only solution is to use multidex configuration. However there are implementation for platforms prior 5.0 and after 5.0

Solution

The solution is described in both: https://developer.android.com/tools/building/multidex.html https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html For my project, since it supports older version of Android, I go with the "multidex support library" solution.

  1. Set the build tool version of all library modules and app module to the latest 21.1.2
  2. For module that has Application class, add compile 'com.android.support:multidex:1.0.0' as dependency
  3. Refer to this link to modify your application class. https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html
  4. At the same gradle script, under android -> defaultConfig block, add "multiDexEnabled true"
  5. If your system gives "Java Heap Size" error, in the app module's gradle script, add the following

dexOptions {
    incremental true
    javaMaxHeapSize "4g"
}
like image 145
Shawn Lu Avatar answered Sep 18 '22 02:09

Shawn Lu