java - 不同版本的相同jar中的重复类-Android

标签 java android gradle

同步成功完成后,我正在尝试将iText集成到我的Android Studio项目中。
然后,我写了一些代码,并尝试构建并测试它,当出现错误(一个巨大的错误)时,即在不同版本的同一jar中发现了重复的类。 (我删除了一些,因为有很多)

Duplicate class com.keypoint.PngEncoder found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.JCommon found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.JCommonInfo found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.base.AbstractBoot found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.base.BaseBoot found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.base.BasicProjectInfo found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
...
Duplicate class org.jfree.base.BasicProjectInfo$OptionalLibraryHolder found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.base.BootableProjectInfo found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)
Duplicate class org.jfree.util.WaitingImageObserver found in modules jcommon-1.0.15.jar (jfree:jcommon:1.0.15) and jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17)

Go to the documentation to learn how to Fix dependency resolution errors.

由此,我了解到同一 jar 的不同版本导致了此问题。因此,我尝试删除版本较低的版本(jcommon-1.0.15和1个其他版本),但是当我使用gradle同步项目时,它再次下载了它们,如果我离线运行它,则表示无法解决依赖项。更高版本的 jar 也是如此。
更新:
这是我要求的build.gradle文件:
项目级别:
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序级别:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.nalin.app"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.activity:activity:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation project(path: ':libraries:OpenCV')
    implementation 'itext:itext:4.2.1'
}

我还包括了OpenCV,因此这是OpenCV的gradle文件:
apply plugin: 'com.android.library'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'
    }
}

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 2480
        versionName "2.4.8"
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            aidl.srcDirs = ['src']
        }
    }
}

最佳答案

感谢评论中的人,我可以很轻松地解决问题。
我所做的是运行gradlew app:dependencies,它给了我一棵依赖树。
从那棵树中,我可以确定出库的哪些部分需要哪些可传递依赖关系,并且我使用以下方法排除了较低版本的jcommon库,该库没有任何其他依赖关系:exclude group: 'jfree', module: 'jcommon'

关于java - 不同版本的相同jar中的重复类-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63450574/

相关文章:

java - "Unable to load library: JNA native support not found in resource path"尝试在 Mac OS X 上加载 JNA 库

java.lang.IndexOutOfBoundsException : Index: 0, 大小 : 0?

android - 如何为具有 flavor 维度的不同产品变体指定 signingConfigs?

mysql - Gradle 无法识别 mysql 依赖项

java - Android Studio 按钮导致崩溃

java - 多个屏幕在 Android 应用程序中共享数据的首选方式?

java - 我应该将整个对象锁定在修改其一小部分的函数中吗?

android - 在 Android App 中列出 JmDNS 服务

没有 CarModeEnabled 的 Android 夜间模式

android - 如何使用 Android Studio 获得代码覆盖率?