web-dev-qa-db-ja.com

NoClassDefFoundError:次の解決に失敗しました:Lokhttp3 / internal / Platform

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
}
}

このエラーが発生した理由を知っている人ですか?

修正方法を教えてください、

ありがとうございました、

42
Huy Tower

問題はここにあると思います:

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'
103
Michael Katkov