In my project, I have used OkHttp(version 2.5.0) as my network request library. But few days ago, I tried Retrofit2 as part of my app's network request library, as you know, the new Retrofit2 library dependence on OkHttp(version 3.2.0).So here comes the problem that when my app runs on android phone whose android version under android 5.0 always causes crash(larger version works well and causes no crash), the crash stack information shows as below:
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)
From the stack trace, I guess that OkHttp(version 3.2.0) has some conflicts with OkHttp(version 2.5.0). Below I will give my external library configuration:
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'
Hope anyone who knows the problem can give me a favor! Great thanks!
Update 1:
Here is my 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);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…