本文介绍了你如何使用gradle的``exclude``来替换依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



最简单的方法是从 slf4j-simple slf4j-nop



我怎么做呢?



我试过排除

 依赖项{
implementation fileTree(dir:'libs',include:['* .jar'])
implementationorg.jetbrains.kotlin:kotlin-stdlib-jdk8:$ kotlin_version

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

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

implementationorg.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'

testImple心理'junit:junit:4.12'
androidTestImplementation'com.android.support.test:runner:1.0.2'
androidTestImplementation'com.android.support.test.espresso:espresso-core:3.0。 2'
}

但仍然会导致

  SLF4J:类路径包含多个SLF4J绑定。 
SLF4J:在[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:在[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:见http:/ /www.slf4j.org/codes.html#multiple_bindings获得解释。
SLF4J:实际绑定的类型为[org.slf4j.helpers.NOPLoggerFactory] ​​

尝试配置

 依赖项{
implementation fileTree(dir :'libs',包括:['* .jar'])
实现org.jetbrains.kotlin:kotlin-stdlib-jdk8:$ kotlin_version

实现com.android。支持:appcompat-v7:$ support_version
implementation'com.android.support.constraint:constraint-layout:1.1.2'b

implementationorg.jetbrains.anko:anko-common :$ anko_version

implementationorg.slf4j:slf4j-simple:1.6.1
testImplementationorg.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-cor e:3.0.2'
}

配置{
testImplementation.exclude(group:'org.slf4j',module:'slf4j-simple')
}

没有区别。

试试这个:

  configurations.all {config  - > 
config.resolutionStrategy.dependencySubstitution {
if(config.name.toLowerCase()。contains('test')){
替换模块('org.slf4j:slf4j-simple:1.6。 1')带模块('org.slf4j:slf4j-nop:1.6.1')
}
}
}


So I want to disable logging for JUnit Tests.

Simplest way would be to switch the binding from slf4j-simple to slf4j-nop.

How do I do that, though?

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

But that still results in

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]

Also tried 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')
}

which made no difference.

解决方案

Try this :

configurations.all { config ->
    config.resolutionStrategy.dependencySubstitution {
        if (config.name.toLowerCase().contains('test')) {
            substitute module('org.slf4j:slf4j-simple:1.6.1') with module('org.slf4j:slf4j-nop:1.6.1')
        }
    }
}

这篇关于你如何使用gradle的``exclude``来替换依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:00