NoClassDefFoundError for OkHttpClient

匿名 (未验证) 提交于 2019-12-03 03:08:02

问题:

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.(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.

回答1:

You are getting

       java.lang.NoClassDefFoundError: com.squareup.okhttp.internal.Util         at com.squareup.okhttp.OkHttpClient.(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 {  compile 'com.android.support:multidex:1.0.0' }
  1. Add multiDexEnabled true

  2. Call compile 'com.android.support:multidex:1.0.0' // or 1.0.1

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

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

Finally

compile 'com.squareup.okhttp3:okhttp:3.0.1' or // compile 'com.squareup.okhttp:okhttp:2.5.0' compile 'com.facebook.android:facebook-android-sdk:4.10.0'

Then

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



回答2:

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"); }


回答3:

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.

         ...     

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.



回答4:

You should enable multidex to resolve this problem.

In build.gradle

apply plugin: 'com.android.application'  android {     compileSdkVersion 23     buildToolsVersion '23.0.2'     dexOptions {         incremental true         javaMaxHeapSize "4g"     }  defaultConfig {         applicationId "com.example.app"         minSdkVersion 17         targetSdkVersion 22         versionCode 1         versionName "1.0"         // Enabling multidex support.         multiDexEnabled true     }      buildTypes {         debug {             shrinkResources true         }         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } }  dependencies {      // Enabling multidex support.     compile 'com.android.support:multidex:1.0.1'     //Other Libraries }

In your project Application class

public class SampleApp extends Application {     @Override         protected void attachBaseContext(Context base) {             super.attachBaseContext(base);             MultiDex.install(base);         } }

Here is the official link [http://developer.android.com/tools/building/multidex.html].



回答5:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!