Search code examples
androidnoclassdeffounderrorclassnotfoundexceptionandroid-4.4-kitkatandroid-multidex

Could not find class/NoClassDefFound error on Android running 4.3/ pre L


I have an android application that seems to run fine on Android L+ devices. However, when I try to run it for pre L devices, I seem to get an exception on Could not Find Class or NoClassDefFoundError and these classes seem to be from a different project on which my project is dependent upon.

I tried java.lang.NoClassDefFoundError on Android devices with KitKat and NoClassDefFoundError in 4.4 kitkat but not in 5.0 Lollipop - Could not find class , which recommended me to disable multidex in build.gradle, but it did not find the issue and I started getting transformClasseswithDexforDebug errors listed at UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define, which recommended me to turn back multiDex back to enabled.

Here is a sample stacktrace of the errror:

01-28 17:09:21.214 15125-15125/<app> I/dalvikvm: Could not find method <XXX>, referenced from method <YYY>.findZygotePid 01-28 17:09:21.190 15125-15125/<app> E/dalvikvm: Could not find class '<ZZZ>', referenced from method <AAA>.broadcast 01-28 17:09:21.229 15125-15125/<AAA> E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NoClassDefFoundError: <Method from a dependent project's class>

Both my main Android project and the dependent module have multidex enabled and dependencies is set to compile com.android.support:multidex:1.0.0. Can someone please provide some inputs on how do I go about fixing the exception being thrown on pre L devices? Any inputs would be appreciated.

I also tried clean build and rebuild of the project, but it doesn't help me. Thanks.

UPDATE: Sample Code

class Dummy { 
Dummy(<params>) {
        initialize variables;
    }

}

and this is being invoked as new Dummy(<params>), where it throws an exception of NoClassDefFound package_name.Dummy


Solution

  • Fixed the above problem, by making my base activity extend MultiDexApplication (not Application) and the application does not crash now. Also, modified build.gradle and AndroidManifest.xml to set minSdkVersion to 18.