Search code examples
javaandroidandroid-4.4-kitkat

NoClassDefFound from Application class


I have been struck on at a point for hours now, i was initializing some code in my Controller.java class that i called from my application class, it was working earlier on all devices but since yesterday i am getting NoClassDefNotFound exception on devices below lolipop..i have not changed anything in this file that may have triggered it

 my ApplicationClass code:

 .. onCreate(..){
      ..
      Controller.init(this);
      ..
    }

it is imported correctly has no error and it has the following code:

  public class Controller {

      ..
      public static final void init(Context context) {
             mRequestQueue = Volley.newRequestQueue(context);
      }
      ..
  }

Package name is correct , i have cleaned,rebuild,invalidated cache and restarted,restarted studios,restated computer.. i have tried everything nothing seems to work.

i am struck for hours now, nothing is working..

code works fine on lollipop (Android 5.0) but crashes below lollipop (Android 5.0)

please help me out


Solution

  • i found the problem myself, android studios has this problem of not showing the exact error when code exceeds 65k methods, like eclipse does with this:

     Conversion to Dalvik format failed:
     Unable to execute dex: method ID not in [0, 0xffff]: 65536
    

    i had to pull off my hair for 2 days to figure out that NoClassDefFound was being shown to me because i had not enabled multidex.

    if you get this problem then write the following code in build.gradle file:

    ...
    android {
        ...
        defaultConfig {
            multiDexEnabled true
        }
        ..
    }
    

    if you are targeting Android 5+

    for below versions add:

     dependencies {
          compile 'com.android.support:multidex:1.0.0'
     }