在我的项目中,我使用OkHttp(版本2.5.0)作为我的网络请求库。但是几天前,我尝试将Retrofit2作为我的应用程序的网络请求库的一部分,如你所知,新的Retrofit2库依赖于OkHttp(版本3.2.0)。所以这里出现的问题是当我的应用程序运行在android手机上的android android 5.0下的版本总是会导致崩溃(大版本运行良好并且不会导致崩溃),崩溃堆栈信息显示如下:
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$1.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$1.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);
}
}
答案 0 :(得分:11)
是的,最后我发现问题并解决了。我查看了类似的问题here并发现可能 64K方法可能会导致问题,所以我来official site 查看使用超过64K方法构建应用程序。 现在您应该在build.gradle文件中编译multidex库:
compile 'com.android.support:multidex:1.0.1'
不要忘记配置defaultConfig:
android {
defaultConfig {
multiDexEnabled true
}
}
最后,您可以编写 BaseApplication 扩展到应用程序,然后编写 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>