How to configure gradle to use logback-classic ONLY for unit tests in Android?

I have an android app that uses slf4j + logback. The Gradle file is as follows:

... compile('com.github.tony19:logback-android-core:1.1.1-6') { exclude group: 'ch.qos.logback.core.net' } compile('com.github.tony19:logback-android-classic:1.1.1-6') { exclude group: 'ch.qos.logback.core.net' } compile 'org.slf4j:slf4j-api:1.7.25' 

And I have logback.xml in assets /. When I start the application registration work, as expected. I recently decided to add new unit tests (normal, not tests for Android), but I saw that the log messages are not displayed.

I added the following to the gradation file:

 testCompile('ch.qos.logback:logback-classic:1.2.3') { exclude group: 'com.github.tony19' } 

and now log messages are displayed, but every time I run unit tests, I get this warning:

 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/home/o/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/home/o/.gradle/caches/modules-2/files-2.1/com.github.tony19/logback-android-classic/1.1.1-6/614c66c9ece69eef48c65a8558de4fd7266d9f69/logback-android-classic-1.1.1-6.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 

Is there a way to indicate which slf4j binding will be used for regular application runs and which is only for unit tests (or any other solution)?

Dependency Graph:

 compile - Classpath for compiling the main sources. +--- com.bolyartech.forge.android:forge-android:7.1.0 | +--- com.bolyartech.forge:base:5.1.0 | | +--- org.slf4j:slf4j-api:1.7.7 -> 1.7.25 | | +--- com.darwinsys:hirondelle-date4j:1.5.1 | | +--- com.google.guava:guava:20.0 | | +--- javax.inject:javax.inject:1 | | \--- com.squareup.okhttp3:okhttp:3.1.2 | | \--- com.squareup.okio:okio:1.6.0 | +--- com.google.dagger:dagger:2.9 | | \--- javax.inject:javax.inject:1 | +--- com.android.support:support-annotations:25.3.1 -> 26.0.1 | +--- javax.annotation:jsr250-api:1.0 | +--- com.android.support.test.espresso:espresso-idling-resource:2.2.2 | \--- net.sourceforge.streamsupport:streamsupport:1.5.1 +--- com.bolyartech.scram_sasl:scram_sasl:2.0.1 +--- com.android.support:multidex:1.0.1 +--- com.squareup:otto:1.3.8 +--- com.google.code.gson:gson:2.7 +--- com.github.franmontiel:PersistentCookieJar:v0.9.3 | \--- com.squareup.okhttp3:okhttp:3.1.2 (*) +--- com.github.tony19:logback-android-core:1.1.1-6 +--- com.github.tony19:logback-android-classic:1.1.1-6 | +--- com.github.tony19:logback-android-core:1.1.1-6 | \--- com.github.tony19:apktool-lib:1.4.4-3 | \--- com.google.android:android:2.1_r1 | +--- commons-logging:commons-logging:1.1.1 | +--- org.apache.httpcomponents:httpclient:4.0.1 | | +--- org.apache.httpcomponents:httpcore:4.0.1 | | +--- commons-logging:commons-logging:1.1.1 | | \--- commons-codec:commons-codec:1.3 | +--- org.khronos:opengl-api:gl1.1-android-2.1_r1 | +--- xerces:xmlParserAPIs:2.6.2 | \--- xpp3:xpp3:1.1.4c +--- ch.acra:acra:4.7.0 | \--- com.android.support:support-v4:23.0.1 -> 26.0.1 | +--- com.android.support:support-compat:26.0.1 | | \--- com.android.support:support-annotations:26.0.1 | +--- com.android.support:support-media-compat:26.0.1 | | +--- com.android.support:support-annotations:26.0.1 | | \--- com.android.support:support-compat:26.0.1 (*) | +--- com.android.support:support-core-utils:26.0.1 | | +--- com.android.support:support-annotations:26.0.1 | | \--- com.android.support:support-compat:26.0.1 (*) | +--- com.android.support:support-core-ui:26.0.1 | | +--- com.android.support:support-annotations:26.0.1 | | \--- com.android.support:support-compat:26.0.1 (*) | \--- com.android.support:support-fragment:26.0.1 | +--- com.android.support:support-compat:26.0.1 (*) | +--- com.android.support:support-core-ui:26.0.1 (*) | \--- com.android.support:support-core-utils:26.0.1 (*) +--- org.slf4j:slf4j-api:1.7.25 +--- javax.inject:javax.inject:1 +--- javax.annotation:jsr250-api:1.0 +--- com.google.dagger:dagger:2.9 (*) \--- com.android.support:appcompat-v7:26.0.1 +--- com.android.support:support-annotations:26.0.1 +--- com.android.support:support-v4:26.0.1 (*) +--- com.android.support:support-vector-drawable:26.0.1 | +--- com.android.support:support-annotations:26.0.1 | \--- com.android.support:support-compat:26.0.1 (*) \--- com.android.support:animated-vector-drawable:26.0.1 +--- com.android.support:support-vector-drawable:26.0.1 (*) \--- com.android.support:support-core-ui:26.0.1 (*) testCompile - Classpath for compiling the test sources. +--- ch.qos.logback:logback-classic:1.2.3 | +--- ch.qos.logback:logback-core:1.2.3 | \--- org.slf4j:slf4j-api:1.7.25 +--- junit:junit:4.12 | \--- org.hamcrest:hamcrest-core:1.3 \--- org.mockito:mockito-core:1.10.19 +--- org.hamcrest:hamcrest-core:1.1 -> 1.3 \--- org.objenesis:objenesis:2.1 
+7
android unit-testing gradle slf4j logback
source share
3 answers

With the Android Gradle Plugin 3 and above, try using the following:

 implementation 'org.slf4j:slf4j-api:1.7.25' runtimeOnly 'com.github.tony19:logback-android-core:1.1.1-6' 
+1
source share

Dependency Area

In the old version from gradle, the scope of compilation merging will be included in all classes of classes, i.e. it includes compilation, runtime, compilation and test execution. Thus, two implementations of the SLF4J class will be included in the test.

Linking SLF4J

And since the implementation of the SLF4J bindings , the two bindings will raise a warning, SLF4J will randomly select the binding, in this case not Logback.

Decision

So, the only way to solve the problem is to exclude the path to logback-android from your test path.

In the new version of Gradle (4.0.2), we define logback-android in runtimeElements to avoid using a test environment. In the old version, it seems that we cannot solve this problem with gradle.

Link

+1
source share

You need to exclude it at the root level instead of the classic log dependency:

 configurations { testCompile.exclude group: 'com.github.tony19' } 
0
source share

All Articles