Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoClassDefFoundError: Logging Interceptor v3.12.0 #4436

Closed
adam-hurwitz opened this issue Dec 7, 2018 · 6 comments
Closed

NoClassDefFoundError: Logging Interceptor v3.12.0 #4436

adam-hurwitz opened this issue Dec 7, 2018 · 6 comments

Comments

@adam-hurwitz
Copy link

adam-hurwitz commented Dec 7, 2018

Issue

After adding the Square Okhttp Logging Interceptor library to my Kotlin app I am experiencing the following error which I think may be due to a Guava dependency conflict. I've created a StackOverflow post as well.

Error

Exception in thread "Timer-0" java.lang.NoClassDefFoundError: okhttp3/logging/HttpLoggingInterceptor
    at utils.Retrofit.retrofitBuilder(Retrofit.kt:28)
    at utils.Retrofit.<clinit>(Retrofit.kt:19)
    at content.ContentRequestsKt.getEventRegistryContent(ContentRequests.kt:40)
    at content.Task.retrieveAndSaveContent(ContentTasks.kt:146)
    at content.Task.run(ContentTasks.kt:57)
    at java.base/java.util.TimerThread.mainLoop(Timer.java:556)
    at java.base/java.util.TimerThread.run(Timer.java:506)
Caused by: java.lang.ClassNotFoundException: okhttp3.logging.HttpLoggingInterceptor
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 7 more

Setup

build.gradle (dependency scan)

buildscript {
    ext.kotlin_version = '1.3.11'
    ext.junitJupiterVersion  = '5.3.2'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
    }
}

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.2.51'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testImplementation group: 'junit', name: 'junit', version: '5.3.2'
    // JUnit Jupiter API and TestEngine implementation
    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
    testImplementation "org.assertj:assertj-core:3.11.1"
    // To avoid compiler warnings about @API annotations in JUnit code
    testCompileOnly 'org.apiguardian:apiguardian-api:1.0.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
    implementation 'com.google.firebase:firebase-admin:6.6.0'
    implementation 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

Retrofit.kt

package utils

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import utils.Enums.EnvironmentType.PRODUCTION
import utils.EnvironmentType.environmentType

object Retrofit {

    val eventRegistryRetrofit: Retrofit
    val youTubeRetrofit: Retrofit

    init {
        eventRegistryRetrofit = retrofitBuilder(EVENTREGISTRY_BASE_URL)
        youTubeRetrofit = retrofitBuilder(YOUTUBE_BASE_URL)
    }

    /**
     * Build Retrofit object based on source.
     */
    fun retrofitBuilder(baseUrl: String): Retrofit {
        val debugOkHttpClient =
            okhttp3.OkHttpClient.Builder().addInterceptor(HttpLoggingInterceptor().setLevel(Level.BASIC)).build()
    return Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
            .baseUrl(baseUrl)
            .client(if (environmentType == PRODUCTION) OkHttpClient() else debugOkHttpClient)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build()
    }
}

Attempted Solutions

  1. Rebuilding Jar Artifact.
  2. Rebuilding project.
  3. Invalidating IntelliJ cache and restarting IDE.
  4. Re-syncing Gradle file.
  5. Explicitly define guava and com.google.api libraries.
  6. Excluding older versions of guava from libraries. (dependency scan)

build.gradle

...
dependencies {
    implementation "com.google.guava:guava:27.0.1-jre"
    implementation ("com.google.api:gax:1.33.1") {
        exclude group: "com.google.guava", module: "guava"
    } 
    implementation ("com.google.api:gax-grpc:1.33.1") {
        exclude group: "com.google.guava", module: "guava"
    }
    ...
    implementation ('com.squareup.okhttp3:logging-interceptor:3.12.0') {
        exclude group: "com.google.guava", module: "guava"
    }
    ...
}
...
@swankjesse
Copy link
Member

Unclear what action we would take here.

@adam-hurwitz
Copy link
Author

adam-hurwitz commented Dec 8, 2018

@swankjesse, the action is looking into if and how the com.squareup.okhttp3:logging-interceptor:3.12.0 library is creating breaking dependency conflicts.

Judging from similar exception's with other libraries in the past, this may be regarding Square's dependency / sub-dependencies with Google's Guava library.

@dave-r12
Copy link
Collaborator

dave-r12 commented Dec 8, 2018

@AdamSHurwitz I copied/pasted the files you provided and had to fix a few compiler issues. But it seems working for me. Post a complete example?

@adam-hurwitz
Copy link
Author

@dave-r12 Thank you for investigating! This leads me to believe it is an issue with the build environment. What Project SDK (File > Project Structure > Project SDK) are you using?

My Project SDK: IntelliJ IDEA Community Edition IC-183.4284.148

Currently the existing code is not open source friendly so I will need to create a new sample app in order to share if it is not an environment fix.

@dave-r12
Copy link
Collaborator

@AdamSHurwitz I don't follow. I downloaded that IntelliJ version and it's still working. Also I'm building with OpenJDK11.

@adam-hurwitz
Copy link
Author

@dave-r12, interesting. It seems the build environment may not be the issue then. I will create a new app to share if the problem persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants