gpt4 book ai didi

java - NoClassDefFoundError : Square Logging Interceptor v3. 12.0

转载 作者:行者123 更新时间:2023-11-30 05:11:25 27 4
gpt4 key购买 nike

问题

添加 Square Okhttp Logging Interceptor library 后对于我的 Kotlin 应用程序,我遇到以下错误,我认为这可能是由于 Guava 依赖冲突造成的。我有 filed an issue在 Square 的库中也是如此。

错误

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

设置

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

尝试的解决方案

  1. 重建 Jar Artifact 。
  2. 重建项目。
  3. 使 IntelliJ 缓存失效并重新启动 IDE。
  4. 正在重新同步 Gradle 文件。
  5. 明确定义 guavacom.google.api 库。
  6. 从库中排除旧版本的 guava。 ( 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"
}
...
}
...

最佳答案

在我的例子中,我安装了logging-interceptor (3.13.1)

    <dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.13.1</version>
</dependency>

它使用 okhttp (3.13.1)

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.13.1</version>
</dependency>

还有我的Retrofit (2.5.0)

<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.5.0</version>
</dependency>

它使用 okhttp (3.12.0)

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.0</version>
</dependency>

由于 logging-interceptor 的顺序(或 smth.)okhttp 依赖项被忽略了。我想 okhttp (3.12.0)logging-interceptor (3.13.1) 不兼容。

关于java - NoClassDefFoundError : Square Logging Interceptor v3. 12.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53675302/

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