【问题标题】:NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/PlatformNoClassDefFoundError:解析失败:Lokhttp3/internal/Platform
【发布时间】:2017-01-25 12:25:16
【问题描述】:

我正在使用Retrofit2 库。

我已经尝试在build.gradle 文件中更新最新版本:Retrofit2、Gson、Rxjava、OKHttp、HttpLoggingInterceptor ...

build.grade 在应用程序中

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',         {
    exclude group: 'com.android.support', module: 'support-annotations'
})

// Default - Android Component
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'

// Retrofit2 + Gson
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'

// RxJva
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.4'
}

但我得到了错误

 > Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/Platform;
at okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:112)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:160)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at internal.NetworkModule$1.intercept(NetworkModule.java:150)

在下面的代码中

Interceptor provideInterceptor(final Context context, final PreferenceStore preferenceStore) {
    Interceptor headerAuthorizationInterceptor = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
            // Get Android ID
            String android_id = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.ANDROID_ID);

            // Get Application Version
            String appVersion = "";
            try {
                PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
                appVersion = packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                Timber.e("Cannot get app version");
            }

            /**
             * Add header with following attributes as agree with Web Server
             * - Token
             * - "Android"
             * - Release version
             * - Model
             * - App version
             * - Android ID
             */
            String token = preferenceStore.getAuthToken();

            Timber.i("X-Asukabu-Token: %s", token);

            // Add header to request
            Request request = chain.request();
            if (token != null)
                request.headers().newBuilder()
                        .add("X-Asukabu-Token", token)
                        .add("X-Asukabu-Client-OS", "Android")
                        .add("X-Asukabu-Client-OS-Version", Build.VERSION.RELEASE)
                        .add("X-Asukabu-Client-Device", Build.MODEL)
                        .add("X-Asukabu-Client-App-Version", appVersion)
                        .add("X-Asukabu-Client-Device-ID", android_id)
                        .add("Accept","*/*")
                        .build();
            else
                request.headers().newBuilder()
                        .add("X-Asukabu-Client-OS", "Android")
                        .add("X-Asukabu-Client-OS-Version", Build.VERSION.RELEASE)
                        .add("X-Asukabu-Client-Device", Build.MODEL)
                        .add("X-Asukabu-Client-App-Version", appVersion)
                        .add("X-Asukabu-Client-Device-ID", android_id)
                        .add("Accept","*/*")
                        .build();

            // ERROR IN THIS LINE : LINE 150
            return chain.proceed(request);
        }
    };

    return headerAuthorizationInterceptor;
}

build.gradle 在项目中:

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.2.0-rc2'

    // Dagger 2
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

知道我为什么会出现此错误的人吗?

请告诉我如何解决它,

谢谢,

【问题讨论】:

    标签: java android retrofit2 okhttp3


    【解决方案1】:

    我猜,问题出在这里:

    compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    

    试着做到这一点:

    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    

    【讨论】:

    • 只需在两个库中添加相同的版本即可。
    • 它对我不起作用!我没有compile 'com.squareup.okhttp3:okhttp:3.4.1'
    • 也许,你在另一个库中有 okhttp 库。可能,改造
    • 我也有同样的问题。我的应用程序正在使用我的自定义库。它又在其 build.gradle 文件中定义了这个库 'com.squareup.okhttp3:okhttp-urlconnection:3.9.1'。但是当我在运行时运行我的应用程序时,我不断收到以下错误 - 原因:java.lang.NoClassDefFoundError:解析失败:Lokhttp3/OkHttpClient$Builder;原因:java.lang.ClassNotFoundException:在路径上找不到类“okhttp3.OkHttpClient$Builder”:DexPathList[[zip file“/data/app/com.......”
    • 就我而言,另一个库推高了该库的一个版本。使用冲突解决解决了它stackoverflow.com/a/39292202/113012
    【解决方案2】:

    问题来了 https://github.com/square/retrofit/issues/2266

    你必须把相同的版本。 这是最后一个版本

    implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
    implementation 'com.squareup.okhttp3:okhttp:4.7.2'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 2017-05-10
      • 2020-04-10
      • 1970-01-01
      • 2017-04-12
      • 2020-10-31
      • 1970-01-01
      相关资源
      最近更新 更多