如何使用gradle的“排除”替换依赖项?

Use*_*291 5 java dependencies slf4j gradle kotlin

所以我想为JUnit测试禁用日志记录。

最简单的方法是将绑定从切换slf4j-simpleslf4j-nop

但是,我该怎么做呢?

我想exclude

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    implementation "com.android.support:appcompat-v7:$support_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    implementation "org.jetbrains.anko:anko-common:$anko_version"

    implementation "org.slf4j:slf4j-simple:1.6.1"
    testImplementation("org.slf4j:slf4j-nop:1.6.1"){
        exclude(group:'org.slf4j', module:'slf4j-simple')
    }
    implementation 'io.github.microutils:kotlin-logging:1.6.10'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Run Code Online (Sandbox Code Playgroud)

但这仍然导致

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/user1291/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-nop/1.6.1/70249094d4e5653b6bdfea46f3a1a4165c1e1993/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/user1291/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-simple/1.6.1/58e59bfb3e247097b8122243b3bfe0049c8cfae8/slf4j-simple-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]
Run Code Online (Sandbox Code Playgroud)

还尝试了configurations

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    implementation "com.android.support:appcompat-v7:$support_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    implementation "org.jetbrains.anko:anko-common:$anko_version"

    implementation "org.slf4j:slf4j-simple:1.6.1"
    testImplementation "org.slf4j:slf4j-nop:1.6.1"
    implementation 'io.github.microutils:kotlin-logging:1.6.10'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

configurations{
    testImplementation.exclude(group:'org.slf4j',module:'slf4j-simple')
}
Run Code Online (Sandbox Code Playgroud)

没什么区别。

Tod*_*odd 0

我会走另一条路。SLF4J 的简单记录器将在您的类路径中查找simplelogger.properties。如果您将其放入 中src/test/resources,您的测试代码应该会在类路径中找到它,并且您可以使用它来关闭 SLF4J 的输出。

# src/test/resources/simplelogger.properties
org.slf4j.simpleLogger.defaultLogLevel=off
Run Code Online (Sandbox Code Playgroud)

您可以在其 javadoc 页面上查看配置属性的完整列表以及简单记录器的详细描述。