NoClassDefFoundError for OkHttpClient
Asked Answered
M

4

14

After adding the facebook dependency in gradle I'm getting this runtime error:

     compile 'com.facebook.android:facebook-android-sdk:4.6.0'

Please notice that I'm also using okhttp:

    compile 'com.squareup.okhttp:okhttp:2.5.0'

and error log is:

 E/AndroidRuntime: FATAL EXCEPTION: Thread-109754
     Process: com.venkat.project, PID: 4453
            java.lang.NoClassDefFoundError: com.squareup.okhttp.internal.Util
            at com.squareup.okhttp.OkHttpClient.<clinit>(OkHttpClient.java:57)
            at com.venkat.project.http.MyHTTPThread.run(MyHTTPThread.java:127)
            at com.venkat.project.http.MyHTTPThread.run(MyHTTPThread.java:61)
            at java.lang.Thread.run(Thread.java:841)
02-23 18:11:02.729 4453-4573/com.venkat.project I/dalvikvm: Rejecting re-init on previously-failed class Lcom/squareup/okhttp/OkHttpClient; v=0x0

Note: I'm getting this error on Samsung mobile 4.4 but on the emulator and on moto g 5.0 it works.

Monolatry answered 23/2, 2016 at 12:53 Comment(5)
Clean and Build your projectMoussaka
@MD no use. getting same error.Monolatry
Try to open the apk and undex the classes. Check if the Util class was included in dexing. As a temporary workaround, you could copy the class from sources and include it in your code. That way it will be in your classpath. (Here I stress the work "workaround" and "temporary)"Kutaisi
@Kutaisi if i remove the facebook dependency in gradle the app is woking ... but just if i add it give the errorMonolatry
@Venu Facing same problem yet ?Notwithstanding
N
5

You are getting

       java.lang.NoClassDefFoundError: com.squareup.okhttp.internal.Util
        at com.squareup.okhttp.OkHttpClient.<clinit>(OkHttpClient.java:57)
        at com.venkat.project.http.MyHTTPThread.run(MyHTTPThread.java:127)
        at com.venkat.project.http.MyHTTPThread.run(MyHTTPThread.java:61)

NoClassDefFoundError for OkHttpClient

public class NoClassDefFoundError extends LinkageError

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

Quote from NoClassDefFoundError

You should use

compile 'com.facebook.android:facebook-android-sdk:4.10.0'

After that you can get this error finished with non-zero exit value 2

Then

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

    // Enabling multidex support.
    multiDexEnabled true
}

dependencies {
 implementation 'com.android.support:multidex:1.0.0'
}
  1. Add multiDexEnabled true

  2. Call implementation'com.android.support:multidex:1.0.2'

OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers.

You can call latest version

implementation'com.squareup.okhttp3:okhttp:3.2.0'

Then

Clean and Re-Build & Sync Your Project . Hope this helps .

Notwithstanding answered 29/2, 2016 at 6:5 Comment(0)
P
15

The latest version of Piccasso use an older version of Okhttp, you need to use a new dependency

compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.picasso:picasso:2.5.2'    
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.0.2'

Example:

File httpCacheDirectory = new File(getCacheDir(), "picasso-cache");
Cache cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().cache(cache);
Picasso.Builder picassoBuilder = new Picasso.Builder(getApplicationContext());
picassoBuilder.downloader(new OkHttp3Downloader(clientBuilder.build()));
Picasso picasso = picassoBuilder.build();
try {
   Picasso.setSingletonInstance(picasso);
} catch (IllegalStateException ignored) {
  Log.e(LOG_TAG, "Picasso instance already used");
}
Ptah answered 2/8, 2016 at 8:11 Comment(0)
N
5

You are getting

       java.lang.NoClassDefFoundError: com.squareup.okhttp.internal.Util
        at com.squareup.okhttp.OkHttpClient.<clinit>(OkHttpClient.java:57)
        at com.venkat.project.http.MyHTTPThread.run(MyHTTPThread.java:127)
        at com.venkat.project.http.MyHTTPThread.run(MyHTTPThread.java:61)

NoClassDefFoundError for OkHttpClient

public class NoClassDefFoundError extends LinkageError

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

Quote from NoClassDefFoundError

You should use

compile 'com.facebook.android:facebook-android-sdk:4.10.0'

After that you can get this error finished with non-zero exit value 2

Then

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

    // Enabling multidex support.
    multiDexEnabled true
}

dependencies {
 implementation 'com.android.support:multidex:1.0.0'
}
  1. Add multiDexEnabled true

  2. Call implementation'com.android.support:multidex:1.0.2'

OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers.

You can call latest version

implementation'com.squareup.okhttp3:okhttp:3.2.0'

Then

Clean and Re-Build & Sync Your Project . Hope this helps .

Notwithstanding answered 29/2, 2016 at 6:5 Comment(0)
T
0

https://developer.android.com/tools/building/multidex.html

Multidex support for Android 5.0 and higher

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART. That means your app would working fine on API level 21 or above.

Multidex support prior to Android 5.0

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.

Try adding this

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

In your manifest add the MultiDexApplication class from the multidex support library to the application element.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

Alternative to that, If your app extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex.

public void onCreate(Bundle arguments) {
    MultiDex.install(getTargetContext());
    super.onCreate(arguments);
    ...
}

Finally, you will need to update your build.gradle file as below by adding multiDexEnabled true :

defaultConfig {  
        applicationId '{Project Name}'  
        minSdkVersion 15  
        targetSdkVersion 23  
        versionCode 1  
        versionName "1.0"  
        multiDexEnabled true  
    }  

I hope it will help you out.

Tantalate answered 29/2, 2016 at 5:20 Comment(2)
Attribute “name” was already specified for element “activity”Diophantus
@Diophantus I didn't get your point? and why did you down vote please enlighten me . What mistake did I made.Tantalate
A
0

Looks like you have enabled proguard. If you do not need proguard in your application then you can disable it in your build.gradle(app)

debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

And if you need proguard in your application then follow this link where Mr. Jake Wharton have solved this.

Ariannaarianne answered 29/2, 2016 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.