android - Gradle:如何摆脱重复的类错误

标签 android gradle

运行我的Android应用程序时出现以下重复类错误(我没有包含所有错误,但是重复错误都来自org.apache.commons.lang3.*org.apache.commons.logging.*):

Duplicate class org.apache.commons.lang3.AnnotationUtils found in modules commons-lang3-3.4.jar (org.apache.commons:commons-lang3:3.4) and commons-lang3-3.7.jar (commons-lang3-3.7.jar)
Duplicate class org.apache.commons.lang3.AnnotationUtils$1 found in modules commons-lang3-3.4.jar (org.apache.commons:commons-lang3:3.4) and commons-lang3-3.7.jar (commons-lang3-3.7.jar)
...........
Duplicate class org.apache.commons.logging.impl.WeakHashtable found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$1 found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$Entry found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$Referenced found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)
Duplicate class org.apache.commons.logging.impl.WeakHashtable$WeakKey found in modules commons-logging-1.2.jar (commons-logging-1.2.jar) and commons-logging-1.2.jar (commons-logging:commons-logging:1.2)

错误源自gradle中的这一行代码(我需要使用docx4j库,因为我的应用必须读取用户选择的MS Word文件的内容):
implementation "org.docx4j:docx4j:3.3.0"

以下是我的gradle文件的完整代码:
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.diffchecker"
        minSdkVersion 21
        targetSdkVersion 29
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'io.github.java-diff-utils:java-diff-utils:4.5'
    // For developers using AndroidX in their applications
    implementation 'pub.devrel:easypermissions:3.0.0'
    // For developers using the Android Support Library
    implementation 'pub.devrel:easypermissions:2.0.1'
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation files('libs/commons-text-1.2.jar')
    //Thanks for using https://jar-download.com
    implementation 'org.webjars.bowergithub.telecomsante:pdf-viewer:2.2.0'
    implementation 'com.tom_roush:pdfbox-android:1.8.10.1'
    implementation 'com.bskim:maxheightscrollview:1.0.0@aar'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'

    implementation files('libs/commons-logging-1.2.jar')
    implementation files('libs/commons-lang3-3.7.jar')

    // Docx4j is the library used to read Word documents
    // https://mvnrepository.com/artifact/org.docx4j/docx4j
    implementation "org.docx4j:docx4j:3.3.0"
}

如何在我的gradle中使用docx4j库,而不会与commons-lang3commons-logging库发生重复冲突?

最佳答案

我想我发现了与开始时类似的方式,但是这次都排除了两个。

试试这个:

implementation ('org.docx4j:docx4j:3.3.0') {
    ['org.apache.commons','commons-logging'].each {
        exclude group: "$it"
    }
}

关于android - Gradle:如何摆脱重复的类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60221811/

相关文章:

gradle - 依赖 JVM 中的 Kotlin Multiplatform JS 模块

android - 需要使用Gradle版本2.10,即时运行无法在Android Studio上运行。我用2.10

Android Studio 找不到任何与 gradle 匹配的版本

android - 如何设置CheckedTextView的选中/未选中

android - 即使设置最高优先级并首先安装也不会收到短信

android - 仅适用于 Nexus 5/5x 模拟器的 Play Store 应用程序

java - 无法在 Android 中创建 sqlite 数据库

java - 防止列在 GridView 中拉伸(stretch)

maven - Gradle将https maven存储库转换为http 443请求

android - 设置 applicationidsuffix 时找不到包名称的匹配客户端