Android Docx4J Gradle: дублирующий класс org.slf4j, несмотря на его исключение - PullRequest
0 голосов
/ 15 февраля 2020

Я пытаюсь использовать библиотеку Docx4j для чтения содержимого файлов MS Word, выбранных пользователем (я пытаюсь установить его с помощью этого примера: https://github.com/plutext/Docx4j4Android4). Тем не менее, я получаю следующую ошибку, когда читаю в файле MS Word (я не включил все ошибки, но все дублирующие классы принадлежат org.slf4j.*):

Duplicate class org.slf4j.ILoggerFactory found in modules slf4j-android-1.6.1-RC1.jar (slf4j-android-1.6.1-RC1.jar) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
Duplicate class org.slf4j.IMarkerFactory found in modules slf4j-android-1.6.1-RC1.jar (slf4j-android-1.6.1-RC1.jar) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
Duplicate class org.slf4j.Logger found in modules slf4j-android-1.6.1-RC1.jar (slf4j-android-1.6.1-RC1.jar) and slf4j-api-1.7.21.jar (org.slf4j:slf4j-api:1.7.21)
........

Это кажется странным, поскольку в моем build.gradle я исключаю org.sl4j при реализации библиотеки docx4j.

Ниже мой build.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'
        }
    }
    dexOptions {
        // Comment the following in order to see actual errors, not unhelpful "too many errors"!
//        preDexLibraries = false
        additionalParameters=['--core-library']
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude("META-INF/*.kotlin_module")
    }
}

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')
    implementation files('libs/commons-lang3-3.7.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 files('libs/commons-logging-1.2.jar')
    implementation 'com.bskim:maxheightscrollview:1.0.0@aar'
    implementation 'androidx.gridlayout:gridlayout:1.0.0'
    // Docx4j is the library used to read Word documents
    // https://mvnrepository.com/artifact/org.docx4j/docx4j
    implementation files('libs/commons-lang3-3.7.jar')
    implementation files('libs/commons-text-1.2.jar')

    //implementationtestCompile 'junit:junit:4.12'
    implementation 'javax.xml.bind:jaxb-api:2.2.12'
    implementation 'javax.xml.stream:stax-api:1.0-2'
    implementation 'com.fasterxml:aalto-xml:1.0.0'
    //compile 'org.codehaus.woodstox:stax2-api:4.0.0'
    implementation files('libs/awt-bastardised-17v8.jar')
    implementation files('libs/slf4j-android-1.6.1-RC1.jar')
    implementation files('libs/activation.jar')
    implementation files('libs/additionnal.jar')
    implementation files('libs/jaxb-core-2.3.0-SNAPSHOT-ANDROID.jar')
    implementation files('libs/jaxb-runtime-2.3.0-SNAPSHOT-ANDROID.jar')
    implementation files('libs/istack-commons-runtime-3.0.4-SNAPSHOT.jar')
    // with org.slf4j removed
    implementation('org.docx4j:docx4j:3.3.0') {
        ['org.apache.commons', 'commons-logging', 'org.sl4j'].each {
            exclude group: "$it"
        }
    }

}

А ниже скриншот библиотеки jar файлов, которые у меня есть: enter image description here

Ответы [ 4 ]

0 голосов
/ 21 февраля 2020

Вы можете исключить org.slf4j:slf4j-api:1.7.21 и все другие, которые получают конфликты от org.docx4j:docx4j:3.3.0, например,

implementation('org.docx4j:docx4j:3.3.0') {
    exclude group: 'org.slf4j'
}

, а также вы можете изменить свою зависимость следующим образом:

implementation('org.docx4j:docx4j:3.3.0') {
    ['org.apache.commons', 'commons-logging', 'org.sl4j', 'org.slf4j'].each {
            exclude group: "$it"
    }
}
0 голосов
/ 17 февраля 2020

Добавьте следующие строки в build.gradle приложения

configurations {
compile.exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}

configurations.all {
    resolutionStrategy {
        force 'org.slf4j:slf4j-log4j12:1.7.25'
    }
}
0 голосов
/ 19 февраля 2020

Ваш вариант использования - именно мотивация для возможностей Gradle. Есть хороший пост в блоге , объясняющий его подробно.

Результатом этого поста в блоге является новый плагин .

plugins {
  id("dev.jacomet.logging-capabilities") version "0.7.0"
}

loggingCapabilities {
  enforceLog4J2() // <- adjust to your needs see plugin documentation
}
0 голосов
/ 17 февраля 2020

Когда вы смотрите на org.docx4j:docx4j:6.1.2, возникает вопрос, для чего вам вообще нужно libs/slf4j-android-1.6.1-RC1.jar? org.slf4j уже включен и работает на Android без этих Android привязок; pdfbox-android - это примерно тот же сыр. Когда перечитываете эти требования, едва заметна какая-либо реальная попытка; потому что вообще нет кода Java / Kotlin.

...