gpt4 book ai didi

java.lang.NoClassDefFoundError : okhttp3. OkHttpClient$Builder

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:36:54 34 4
gpt4 key购买 nike

在我的项目中,我使用了OkHttp(version 2.5.0)作为我的网络请求库。但是几天前,我尝试将 Retrofit2 作为我的应用程序网络请求库的一部分,如您所知,新的 Retrofit2 库依赖于 OkHttp(版本 3.2.0)。所以当我的应用程序在 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'

希望知道问题的 friend 帮个忙!非常感谢!

更新 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);
}
}

最佳答案

是的,终于找到问题并解决了。我查看了类似的问题here并发现可能是 64K Methods 导致了问题,所以我来到了 official site查看使用超过 64K 种方法构建应用。现在您应该在 build.gradle 文件中编译 multidex 库:

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

并且不要忘记配置您的 defaultConfig:

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>

关于java.lang.NoClassDefFoundError : okhttp3. OkHttpClient$Builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36649121/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com