Alex White
Alex White

Reputation: 103

java.lang.noClassDefFoundError on Android 4.4.4, works on 5.0+

I am getting this error:

12-17 03:59:18.767 7516-8610/io.ustube.ustube E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                            Process: io.ustube.ustube, PID: 7516
                                                            java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                at java.lang.Thread.run(Thread.java:841)
                                                             Caused by: java.lang.NoClassDefFoundError: com.google.gson.internal.$Gson$Preconditions
                                                                at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
                                                                at com.google.gson.reflect.TypeToken.get(TypeToken.java:296)
                                                                at com.google.gson.Gson.toJson(Gson.java:644)
                                                                at com.google.gson.Gson.toJson(Gson.java:631)
                                                                at com.google.gson.Gson.toJson(Gson.java:586)
                                                                at com.google.gson.Gson.toJson(Gson.java:566)
                                                                at io.ustube.ustube.LoginActivity$VerifyTask.doInBackground(LoginActivity.java:338)
                                                                at io.ustube.ustube.LoginActivity$VerifyTask.doInBackground(LoginActivity.java:310)
                                                                at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                at java.lang.Thread.run(Thread.java:841) 

but I know that the mentioned class is a dependency. I've looked through the jar file myself. Also, I only get this error below 5.0.

My gradle file includes compile 'com.google.code.gson:gson:2.5'

Upvotes: 3

Views: 1633

Answers (3)

Use compile 'com.android.support:multidex:1.0.0' then extends MultiDexApplication in your application class.

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. For example, if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.

You can try with

compile 'com.google.code.gson:gson:2.3'

Then , Modify your app Gradle build file configuration to include the support library and enable multidex output .

    android {
    compileSdkVersion 21 // Set Yours
    buildToolsVersion "21.1.0" // Set Yours

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21 // Set Yours
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

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

More details

DexIndexOverflowException Only When Running Tests

Upvotes: 1

gprathour
gprathour

Reputation: 15333

This thing happens a lot. You should be able to fix by doing the following,

multiDexEnabled = false

in your build.gradle file.

If you already have multiDexEnabled = true then change it to false or add this new line.

Upvotes: 0

Related Questions