anas p a anas p a - 8 days ago 4
Android Question

java.lang.NoClassDefFoundError in existing class in android

Error log is

java.lang.NoClassDefFoundError: com.test.service.SmartManager$BackgroundHandlerThread
at com.test.service.SmartManager.<init>(SmartManager.java:107)
at com.test.service.LiveUSdkService.onCreate(LiveUSdkService.java:82)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2574)
at android.app.ActivityThread.access$1800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)


But this class defined as inner static class

My class here

public class SmartManager {
/**
Varibale initialization
**/

static class BackgroundHandlerThread extends HandlerThread {
Handler mHandler = null;

BackgroundHandlerThread() {
super("BackgroundHandlerThread");
start();
mHandler = new Handler(getLooper());
}

void runInBackground(Runnable runnable) {
mHandler.post(runnable);
}
}

public SmartManager() {
/**
............
*/
mThread = new BackgroundHandlerThread(); // Here i got error like this
/**
............
*/
}
/**
Rest of the code
**/
}


When i run my application, it gives error like this. But class is defined like shown above. But id dont know why shown like this.

Answer

Just enable multidex to temporarily fix this bug.

Android has a 65k method for libraries that it won't accept large no of files as libraries.

For more refer: http://developer.android.com/tools/building/multidex.html (Building app over 65k method)

So in your Gradle do something like this...

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0"

         defaultConfig {
             minSdkVersion 14 //lower than 14 doesn't support multidex
             targetSdkVersion 22

             // Enabling multidex support.
             multiDexEnabled true
         }
}

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