NoClassDefFoundError for .jar library at runtime in Android Studio NoClassDefFoundError for .jar library at runtime in Android Studio android android

NoClassDefFoundError for .jar library at runtime in Android Studio


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 limitRefer to this document: https://developer.android.com/tools/building/multidex.htmlBasically 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.htmlhttps://developer.android.com/reference/android/support/multidex/MultiDexApplication.htmlFor 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"}