54

I'm using Retrofit2 library.

I already tried to update latest version : Retrofit2, Gson, Rxjava, OKHttp, HttpLoggingInterceptor ... in build.gradle file

build.grade in application

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

But I got error

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

in below code

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 in Project :

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

People who knows why I got this error?

Please tell me how to fix it,

Thank you,

Huy Tower
  • 7,769
  • 16
  • 61
  • 86

2 Answers2

114

I guess, the problem is here:

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

Try to make that:

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
Michael Katkov
  • 2,256
  • 1
  • 20
  • 17
  • 10
    Just add the same version in both libraries. – android iPhone Dec 14 '16 at 14:17
  • It does not work for me! I don't have `compile 'com.squareup.okhttp3:okhttp:3.4.1'` – Hamed Ghadirian Jun 24 '17 at 11:06
  • 11
    Maybe, you have okhttp library inside another library. Possibly, retrofit – Michael Katkov Jun 27 '17 at 08:20
  • 3
    I am having the same issue too. My app is using my custom library. which has inturn defined this library 'com.squareup.okhttp3:okhttp-urlconnection:3.9.1' in its build.gradle file. But when i run my application at runtime i keep getting the below error - Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/OkHttpClient$Builder; Caused by: java.lang.ClassNotFoundException: Didn't find class "okhttp3.OkHttpClient$Builder" on path: DexPathList[[zip file "/data/app/com......." – Kaveri Jul 24 '19 at 09:04
  • 1
    In my case, another library had pushed up one of the library's version. Using conflict resolution fixed it https://stackoverflow.com/a/39292202/113012 – Simon Featherstone Nov 12 '19 at 11:59
2

Here is the issue https://github.com/square/retrofit/issues/2266

You have to put the same version. This is the last version

implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
implementation 'com.squareup.okhttp3:okhttp:4.7.2'
Pabel
  • 652
  • 7
  • 15