NoClassDefFoundError: okhttp3.OkHttpClient$Builder

最近的項目Application類初始化okhttp給拋出這個錯誤NoClassDefFoundError: okhttp3.OkHttpClient$Builder。

依賴沒有?no

衝突?反正我沒找到

重複構建?沒有。。。


以下內容來自https://stackoverflow.com/questions/36649121/java-lang-noclassdeffounderror-okhttp3-okhttpclientbuilder


Yes, finally I find the problem and solve it. I viewed a similar problem here and find that maybe the 64K Methods could cause the problem, so I came to official siteto view Building Apps with Over 64K Methods. Now you should compile the multidex library in your build.gradle file:

compile 'com.android.support:multidex:1.0.1'

And don't forget to config your defaultConfig:

android {
  defaultConfig {
    multiDexEnabled true
  }
}

And finally you can write a BaseApplication extends to Application, over write the attachBaseContext(Context base) method as follows:

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

Don't forget to add this to your AndroidManifest.xml file:

<application
        android:name=".BaseApplication"
        android:allowBackup="true">
</application>

通過上面的方法我解決了我目前的問題,不知道你們是不是一樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章