OkHttpClient 的 NoClassDefFoundError

NoClassDefFoundError for OkHttpClient

在 gradle 中添加 facebook 依赖项后,我收到此运行时错误:

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

请注意,我也在使用 okhttp:

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

错误日志是:

 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

注意:我在 Samsung Mobile 4.4 上遇到此错误,但在模拟器和 moto g 5.0 上它有效。

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

Multidex 支持 Android 5.0 及更高版本

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.

Android5.0

之前的 Multidex 支持

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.

尝试添加这个

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

在您的清单中,将 MultiDexApplication class 从 multidex 支持库添加到应用程序元素。

<?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>

或者,如果您的应用程序扩展了应用程序 class,您可以覆盖 attachBaseContext() 方法并调用 MultiDex.install(this) 以启用 multidex。

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

最后,您需要通过添加 multiDexEnabled true 更新您的 build.gradle 文件:

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

希望对您有所帮助。

您正在获得

       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)

OkHttpClient 的 NoClassDefFoundError

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.

引自NoClassDefFoundError

你应该使用

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

之后你会得到这个错误 finished with non-zero exit value 2

然后

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. 添加multiDexEnabled true

  2. 致电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.

可以调用最新版本

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

然后

Clean and Re-Build & Sync 您的项目。希望这有帮助。

您似乎启用了混淆器。 如果您的应用程序不需要混淆器,那么您可以在 build.gradle(app)

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

如果您的应用程序中需要混淆器,请遵循此 link Jake Wharton 先生已解决的问题。

最新版本的Piccasso使用旧版本的Okhttp,需要使用新的依赖

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'

示例:

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