Android:仅在使用远程依赖时出现重复类错误,但不在使用本地JAR时出现。

huangapple go评论131阅读模式
英文:

Android: duplicate classes errors only when using remote dependency but not local JAR

问题

I've a simple Android application project in Android Studio, where I am including this dependency in build.gradle:

When I build the project, I get lots of errors about duplicate classes. Example:

I'm not using any other library that uses these dependencies so I don't know what is happening.

Now, the interesting thing is that when I actually download the JAR directly from Maven Central and depend on it as a local JAR, the errors vanish!

Also, when I use the dependency in a vanilla Java/gradle project, I don't face any issues. What is going wrong here?

In case it helps, here's the source of the library I'm trying to depend on.

英文:

I've a simple Android application project in Android Studio, where I am including this dependency in build.gradle:

dependencies {
    // dependency I care about:
    implementation 'org.typesense:typesense-java:0.0.9-beta9'

    // other android deps    
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'

    // test deps
    testImplementation 'junit:junit:4.13.2'
}

When I build the project, I get lots of errors about duplicate classes. Example:

Duplicate class com.fasterxml.jackson.databind.type.ArrayType found in modules jackson-databind-2.14.1 (com.fasterxml.jackson.core:jackson-databind:2.14.1) and typesense-java-0.0.9-beta9 (org.typesense:typesense-java:0.0.9-beta9)
Duplicate class okio.Throttler found in modules okio-jvm-2.8.0 (com.squareup.okio:okio:2.8.0) and typesense-java-0.0.9-beta9 (org.typesense:typesense-java:0.0.9-beta9)

I'm not using any other library that uses these dependencies so I don't know what is happening.

Now, the interesting thing is that when I actually download the JAR directly from Maven Central and depend on it as a local JAR, the errors vanish!

implementation files('libs/typesense-java-0.0.9-beta9.jar')

Also, when I use the dependency in a vanilla Java/gradle project, I don't face any issues. What is going wrong here?

In case it helps, here's the source of the library I'm trying to depend on.

答案1

得分: 2

The jar contains it's own dependencies, so the classes fetched by Gradle's dependency resolution are duplicates of those included in the jar.

You can instruct Gradle not to manage the dependencies :

implementation("org.typesense:typesense-java:0.0.9-beta9") {
exclude group: '', module: ''
}

The problem will reappear if something (e.g. another library) declares a dependency on one of the included libraries (Jackson, OkHttp, Log4j, ...)

The jar also contains the Kotlin stdlib. You have to avoid pulling it, directly (in a Kotlin project) or indirectly (e.g. using an AndroidX library written in Kotlin)

A much better solution would probably be to have a version that doesn't include the dependencies in the jar.

By the way, I think there is currently no way to use this library in a Kotlin project.

英文:

The jar contains it's own dependencies, so the classes fetched by Gradle's dependency resolution are duplicates of those included in the jar.

You can instruct Gradle not to manage the dependencies :

implementation("org.typesense:typesense-java:0.0.9-beta9") {
    exclude group: '*', module: '*'
}

The problem will reappear if something (e.g. another library) declares a dependency on one of the included libraries (Jackson, OkHttp, Log4j, ...)

The jar also contains the Kotlin stdlib. You have to avoid pulling it, directly (in a Kotlin project) or indirectly (e.g. using an AndroidX library written in Kotlin)

A much better solution would probably be to have a version that doesn't include the dependencies in the jar.

By the way, I think there is currently no way to use this library in a Kotlin project.

答案2

得分: -1

基本问题出在依赖关系上。这个远程依赖包含了多个依赖项。

如果你想要使用这个远程依赖,你需要排除这些依赖项。

英文:

Basically the issue lies in the dependency. Multiple dependencies are included in this remote dependency.

Android:仅在使用远程依赖时出现重复类错误,但不在使用本地JAR时出现。

If you want to use the remote dependency, you need to exclude the dependencies.

huangapple
  • 本文由 发表于 2023年3月3日 19:25:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定