NoClassDefFoundError for OkHttpClient

前端 未结 4 2065
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 17:16

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

     compile \'com.facebook.android:facebook-android-sdk:4.6.0\'
相关标签:
4条回答
  • 2020-12-01 17:20

    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 .

    0 讨论(0)
  • 2020-12-01 17:27

    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");
    }
    
    0 讨论(0)
  • 2020-12-01 17:35

    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.

    0 讨论(0)
  • 2020-12-01 17:41

    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.

    0 讨论(0)
提交回复
热议问题