java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
在我的项目中,我使用了OkHttp(version 2.5.0)作为我的网络请求库。但是几天前,我尝试将 Retrofit2 作为我应用程序网络请求库的一部分,如您所知,新的 Retrofit2 库依赖于 OkHttp(版本 3.2.0)。所以当我的应用程序运行在 android phone android 5.0以下的android版本老是崩溃(大版本运行良好,不会导致崩溃),崩溃堆栈信息如下:
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.client.ServiceGenerator.<clinit>(ServiceGenerator.java:22)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.ui.fragments.CircleNativeFragment.onViewCreated(CircleNativeFragment.java:105)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Looper.loop(Looper.java:193)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5348)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at dalvik.system.NativeStart.main(Native Method)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.client.ServiceGenerator.<clinit>(ServiceGenerator.java:22)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.ui.fragments.CircleNativeFragment.onViewCreated(CircleNativeFragment.java:105)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Looper.loop(Looper.java:193)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5348)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at dalvik.system.NativeStart.main(Native Method)
从堆栈跟踪来看,我猜测 OkHttp(版本 3.2.0)与 OkHttp(版本 2.5.0)有一些冲突。下面我给出我的外部库配置:
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
希望知道问题的朋友帮个忙!非常感谢!
更新 1:
这是我的 ServiceGenerator.java :
package com.comingx.athit.client;
import com.comingx.athit.model.Constants;
import com.comingx.athit.model.modelmanager.ModelManagerSingleton;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ServiceGenerator {
private static OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder().addHeader("Cookie", ModelManagerSingleton.getNative_cookie()).build();
return chain.proceed(request);
}
}
).build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(Constants.V3.API_PREFIX)
.addConverterFactory(GsonConverterFactory.create());
public static <T> T createService(Class<T> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
是的,终于找到问题并解决了。我看过类似的问题 and find that maybe the 64K Methods could cause the problem, so I came to official site
查看使用超过 64K 种方法构建应用程序。
现在您应该在 build.gradle 文件中编译 multidex 库:
compile 'com.android.support:multidex:1.0.1'
并且不要忘记配置您的默认配置:
android {
defaultConfig {
multiDexEnabled true
}
}
最后你可以写一个 BaseApplication 扩展到 Application,覆盖 attachBaseContext(Context base)方法如下:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
不要忘记将此添加到您的 AndroidManifest.xml 文件中:
<application
android:name=".BaseApplication"
android:allowBackup="true">
</application>
在我的项目中,我使用了OkHttp(version 2.5.0)作为我的网络请求库。但是几天前,我尝试将 Retrofit2 作为我应用程序网络请求库的一部分,如您所知,新的 Retrofit2 库依赖于 OkHttp(版本 3.2.0)。所以当我的应用程序运行在 android phone android 5.0以下的android版本老是崩溃(大版本运行良好,不会导致崩溃),崩溃堆栈信息如下:
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.client.ServiceGenerator.<clinit>(ServiceGenerator.java:22)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.ui.fragments.CircleNativeFragment.onViewCreated(CircleNativeFragment.java:105)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.os.Looper.loop(Looper.java:193)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5348)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at dalvik.system.NativeStart.main(Native Method)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.client.ServiceGenerator.<clinit>(ServiceGenerator.java:22)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at com.comingx.athit.ui.fragments.CircleNativeFragment.onViewCreated(CircleNativeFragment.java:105)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
04-15 21:28:51.185 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.os.Looper.loop(Looper.java:193)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5348)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
04-15 21:28:51.186 17238-17238/com.comingx.athit W/System.err: at dalvik.system.NativeStart.main(Native Method)
从堆栈跟踪来看,我猜测 OkHttp(版本 3.2.0)与 OkHttp(版本 2.5.0)有一些冲突。下面我给出我的外部库配置:
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
希望知道问题的朋友帮个忙!非常感谢!
更新 1: 这是我的 ServiceGenerator.java :
package com.comingx.athit.client;
import com.comingx.athit.model.Constants;
import com.comingx.athit.model.modelmanager.ModelManagerSingleton;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ServiceGenerator {
private static OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(
new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder().addHeader("Cookie", ModelManagerSingleton.getNative_cookie()).build();
return chain.proceed(request);
}
}
).build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(Constants.V3.API_PREFIX)
.addConverterFactory(GsonConverterFactory.create());
public static <T> T createService(Class<T> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
是的,终于找到问题并解决了。我看过类似的问题
compile 'com.android.support:multidex:1.0.1'
并且不要忘记配置您的默认配置:
android {
defaultConfig {
multiDexEnabled true
}
}
最后你可以写一个 BaseApplication 扩展到 Application,覆盖 attachBaseContext(Context base)方法如下:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
不要忘记将此添加到您的 AndroidManifest.xml 文件中:
<application
android:name=".BaseApplication"
android:allowBackup="true">
</application>