246

I created a new project in Android Studio 2.2 Preview 1 with Android App and Backend module with Google Messaging. This is the app file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
    compile 'com.google.android.gms:play-services-gcm:9.0.0'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support:support-annotations:23.4.0'
    compile project(path: ':backend', configuration: 'android-endpoints')
}

But it's giving:

Error:Conflict with dependency 'com.google.code.findbugs:jsr305'. Resolved versions for app (1.3.9) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

I am new to Android and not able to find what is this error. How do I fix it?

Omar Einea
  • 2,478
  • 7
  • 23
  • 35
Rahul Garg
  • 8,410
  • 8
  • 33
  • 28

15 Answers15

651

In your app's build.gradle add the following:

android {
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
}

Enforces Gradle to only compile the version number you state for all dependencies, no matter which version number the dependencies have stated.

gbhall
  • 13,139
  • 9
  • 37
  • 42
  • 58
    It might be useful to explain this snippet. – Andrew G Jul 25 '16 at 02:00
  • 18
    @Andy it explicitly tells Gradle which version shall be compiled (no matter which version number the dependencies state)... while enforcing v `2.0.1` works, too (whatever, both versions need to be forced to be identical). – Martin Zeitler Aug 28 '16 at 17:54
  • 2
    @MartinZeitler but it doesn't really explain where does this problem come from. I today get this problem with testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' – fralbo Mar 06 '18 at 10:59
  • @2ndGAB it most likely adds duplicate classes, which is another issue, than a version conflict. – Martin Zeitler Mar 06 '18 at 16:42
  • For me this does not work. I have a conflict with jsr305:1.3.9 and jsr305:3.0.2 Tried to force both version still getting conflict. Tried everything, there is no solution. – Adam Ri Jul 25 '19 at 09:29
  • 1
    Thanks a ton. This is really helpful still after 3 and a half years. ;) – Kartik Nov 22 '19 at 12:59
  • 1
    @Kartik haha jeeze, it was a long time ago! No worries, thank you, I hope it helped! I’ve long stopped Android development but I’m sure one day I’ll pick it up again – gbhall Nov 22 '19 at 13:01
170

This is due to espresso. You can add the following to your apps build.grade to mitigate this.

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
  exclude group: 'com.google.code.findbugs'
}
Santhosh
  • 3,781
  • 2
  • 14
  • 11
  • 1
    Not working. The question that is set as answer works. – Warpzit Mar 15 '17 at 09:43
  • 2
    @Warpzit - This worked for me (as did the accepted answer). – Ted Hopp Mar 15 '17 at 18:59
  • @TedHopp I actually ended up doing something else. I had issue with gradle compile for youtube api so I ended up using the lib jar instead. – Warpzit Mar 15 '17 at 19:08
  • @Santhosh.. could you please explain.. what ur code will actually do? – kumar Apr 17 '17 at 12:38
  • @kumar Espresso loads a different version of findbugs library than the main apk. Since our main apk already uses this library we basically exclude espresso from loading it so that there wont be a vesion clash. The accepted version also does the same thing, except that my answer doesn't need you to enforce a version. – Santhosh Apr 18 '17 at 10:38
  • 2
    This is most likely the answer everyone is looking for. In most cases Android Studio includes Espresso by default in the dependencies list, and you need to exclude a couple of things in order to use it along side other libraries. Apart from what @Santhosh said, I also `exclude group: 'com.android.support', module: 'support-annotations'` – milosmns Sep 11 '17 at 18:00
  • i think this solution is cleaner. – Utkan Ozyurek Nov 16 '17 at 08:25
  • 1
    Well i just removed *androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') { exclude group: 'com.google.code.findbugs' }* and it worked :D because primarily the problem with your fix is that some people already have * androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })* something like this in their gradle file, and adding another *exclude* is throwing an exception, all hail gradle scripting, still an upvote for you :) – ShayHaned Feb 05 '18 at 22:36
31

METHOD 1: I deleted the androidTestCompile on espresso-core line which was automatically included in a new project. Then my Android Studio compiles clean.

The androidTestCompile is in "build.gradle (Module:app)":

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    ...
}

I don't know whether this deletion will have any problem down the road, but it surely works for my current project now.

METHOD 2: Adding an exclude on findbugs works too:

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs'
    })
    ...
}

METHOD 3: Forcing compiling with a specific version:

(In the following I force it to compile with the higher version.)

dependencies {
    ...
    androidTestCompile 'com.google.code.findbugs:jsr305:3.0.0'
    ...
}
Liwen Zhao
  • 545
  • 5
  • 9
18

From Gradle Plugin User Guide:

When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).

To make the build succeed, just make sure both APKs use the same version. If the error is about an indirect dependency (a library you didn't mention in your build.gradle), just add a dependency for the newer version to the configuration

Add this line to your build.gradle dependencies to use newer version for both APKs:

compile('com.google.code.findbugs:jsr305:2.0.1')

For future reference, you can check your Gradle Console and it will provide a helpful link next to the error to help with any gradle build errors.

Community
  • 1
  • 1
lkisac
  • 2,007
  • 1
  • 26
  • 31
8

The reason why this happen is that diff dependency use same lib of diff version.
So, there are 3 steps or (1 step) to solve this problem.

1st

Add

configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
}

to your build.gradle file in android {...}

2nd

Open terminal in android studio
run ./gradlew -q app:dependencies command.

3rd

Click Clean Project from menu bar of android studio in Build list.
It will rebuild the project, and then remove code in 1st step.

Maybe you need just exec 2nd step. I can't rollback when error occurs. Have a try.

ElliotCui
  • 385
  • 2
  • 9
7

When I added module: 'jsr305' as an additional exclude statement, it all worked out fine for me.

 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude module: 'jsr305'

})

Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
6

The problem, as stated in your logs, is 2 dependencies trying to use different versions of 3rd dependency. Add one of the following to the app-gradle file:

androidTestCompile 'com.google.code.findbugs:jsr305:2.0.1'
androidTestCompile 'com.google.code.findbugs:jsr305:1.3.9'
nbtk
  • 3,039
  • 4
  • 21
  • 19
4
  1. The accepted answer is one way of fixing the issue, because it will just apply some strategy for the problematic dependency (com.google.code.findbugs:jsr305) and it will resolve the problem around the project, using some version of this dependency. Basically it will align the versions of this library inside the whole project.

  2. There is an answer from @Santhosh (and couple of other people) who suggests to exclude the same dependency for espresso, which should work by the same way, but if the project has some other dependencies who depend on the same library (com.google.code.findbugs:jsr305), again we will have the same issue. So in order to use this approach you will need to exclude the same group from all project dependencies, who depend on com.google.code.findbugs:jsr305. I personally found that Espresso Contrib and Espresso Intents also use com.google.code.findbugs:jsr305.

I hope this thoughts will help somebody to realise what exactly is happening here and how things work (not just copy paste some code) :).

Stoycho Andreev
  • 6,163
  • 1
  • 25
  • 27
3

Add this this to dependencies to force using latest version of findbugs library:

compile 'com.google.code.findbugs:jsr305:2.0.1'
Ayman Al-Absi
  • 2,630
  • 24
  • 22
2

delete espresso dependencies in gradle file works for me.

delete those lines in app gradle file:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
Kai Wang
  • 3,303
  • 1
  • 31
  • 27
1

i was trying to use airbnb deeplink dispatch and got this error. i had to also exlude the findbugs group from the annotationProcessor.

//airBnb
    compile ('com.airbnb:deeplinkdispatch:3.1.1'){
        exclude group:'com.google.code.findbugs'
    }
    annotationProcessor ('com.airbnb:deeplinkdispatch-processor:3.1.1'){
        exclude group:'com.google.code.findbugs'
    }
j2emanue
  • 60,549
  • 65
  • 286
  • 456
1

Those who are getting same error in Android 3.0.1,can resolve it by simply update the versions of compileSdkVersion and targetSdkVersion to 27 and also Implement com.android.support:appcompat-v7:27.1.1' in dependencies.

Arjun
  • 1,294
  • 13
  • 24
1

In project ':app' you can add the following to your app/build.gradle file :

android {
 configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
 }
 }
Diya Bhat
  • 235
  • 3
  • 12
1

For react-native-firebase, adding this to app/build.gradle dependencies section made it work for me:

implementation('com.squareup.okhttp3:okhttp:3.12.1') { force = true }
implementation('com.squareup.okio:okio:1.15.0') { force = true }
implementation('com.google.code.findbugs:jsr305:3.0.2') { force = true}
Indivision Dev
  • 1,097
  • 11
  • 16
  • This is the first time I've seen the { force = true } flag. I tried it and it worked right away for me. I needed to keep androidx.appcompat at v1.0.2 for my project because a library I added has that dependency set to v1.1.0. – MikeOscarEcho Feb 21 '20 at 22:07
0

REACT NATIVE

If you looking for react native solution, then write this snippet in your affected node_modules gradle build file, e.g. firebase in my case.

android {
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.0'
    }
}
Faisal Hassan
  • 517
  • 7
  • 10