相同JAR包中不同版本的重复类 – Android

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

Duplicate classes in same jars of different versions - Android

问题

我在尝试将iText集成到我的Android Studio项目中,同步成功后,编写了一些代码并尝试构建和测试。但是出现了一个错误(一个很大的错误),指出在不同版本的相同JAR文件中找到了重复的类(由于数量众多,我已经删除了一些)。

在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 com.keypoint.PngEncoder
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.JCommon
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.JCommonInfo
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.base.AbstractBoot
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.base.BaseBoot
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.base.BasicProjectInfo
...
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.base.BasicProjectInfo$OptionalLibraryHolder
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.base.BootableProjectInfo
在模块 jcommon-1.0.15.jar (jfree:jcommon:1.0.15) 和 jcommon-1.0.17.jar (org.jfree:jcommon:1.0.17) 中找到重复类 org.jfree.util.WaitingImageObserver

请查阅文档了解如何解决依赖解析错误。

根据这个错误信息,我理解是同一JAR文件的不同版本导致了这个问题。因此,我尝试删除较低版本的一个版本(jcommon-1.0.15 和另外一个版本),但是当我用Gradle同步项目时,它会再次下载它们,如果我离线运行它,会显示无法解析依赖项的错误。较高版本的JAR文件也存在类似情况。

更新:
这是我build.gradle文件的内容:
项目级别:

// 顶层 build 文件,在这里您可以添加所有子项目/模块都通用的配置选项。

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

        // 注意:不要在这里放置应用程序依赖项;它们属于各个模块的 build.gradle 文件
    }
}

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']
        }
    }
}
英文:

I was trying to integrate iText in my Android Studio project when the sync successfully completed.
Then I wrote some code and tried to build and test it when it gave an error (A huge one) that duplicate classes were found in same jars of different versions. (I have removed some because there were a lot)

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.

From this I Understand that the different versions of the same jar caused this issue. So I tried to remove the one with lower version (jcommon-1.0.15 and 1 more ) but when i synced the project with gradle, it again downloaded them and if i ran it offline it said that dependencies couldn't be resolved. Similar is the case with the jars of higher version.

Update:
Here are my build.gradle files as requested:
Project level:

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

App level:

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

I Also have included OpenCV so here is the gradle file for OpenCV:

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

答案1

得分: 1

多亏了评论中的这些人,我很容易地解决了这个问题。
我所做的是运行了gradlew app:dependencies命令,它给了我一个依赖树。

从那个树中,我可以看出库的哪些部分需要哪些传递性依赖,并且我使用以下方式排除了较低版本的jcommon库,因为它没有任何其他依赖关系:
exclude group: 'jfree', module: 'jcommon'

英文:

Thanks to the guys in the comments, I could solve the problem quite easily.
What i did was ran gradlew app:dependencies and it gave me a dependency tree.

From that tree i could make out which parts of the library required which transitive dependencies and i excluded the jcommon library of the lower version which did not have any other dependencies using:
exclude group: 'jfree', module: 'jcommon'

huangapple
  • 本文由 发表于 2020年8月17日 20:12:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450574.html
匿名

发表评论

匿名网友

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

确定