1

This is my test:

@RunWith(AndroidJUnit4.class)
@MediumTest
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<MainActivity>(MainActivity.class);

    @Test
    public void buttonShouldBePresent(){
        Intents.init();
        onView(withId(R.id.button)).perform(click());
        intended(hasComponent(SecondActivity.class.getName()));
    }


}

I have these dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:preference-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'

}

I am getting this error:

Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (24.1.1) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Tried to use support library with earlier version (could import successfully but cannot use TextInputEditText):

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'

}

allprojects {
    repositories {
        jcenter()
    }
    configurations.all {
        resolutionStrategy.force('com.android.support:support-annotations:23.1.1')
    }
}

I could import earlier version, but 2 reasons which makes me unable to use earlier version.

  1. I am using TextInputEditText which is not available in earlier version

  2. Project requirement

    • Android Studio 2.1+
    • Android SDK Platform-tools 24+
    • Android SDK Tools 25+
    • SDK Build Tools 24.0.0
    • Android Support Repository 34+
    • Android SDK Platform 24

How can I solve this problem without lowering support library version?

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109
  • Check this question: http://stackoverflow.com/questions/33317555/conflict-with-dependency-com-android-supportsupport-annotations-resolved-ver/33318482#33318482 – Gabriele Mariotti Aug 29 '16 at 07:06

3 Answers3

2

You can force the annotation library in your test using:

androidTestCompile 'com.android.support:support-annotations:24.1.1'
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

Exclude support-annotations from Espresso.

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

You can't mix support library versions (e.g. 24.x.x everything with 23.1.1 support-annotations so remove this:

configurations.all {
    resolutionStrategy.force('com.android.support:support-annotations:23.1.1')
}

Pro tip:

design depends on appcompat-v7 and recyclerview-v7 so you don't need to specify them.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
0

There are two solutions to fix it:

  • The easiest way would be to include in your dependencies this line:

    androidTestCompile 'com.android.support:support-annotations:24.1.1'

and remove this:

configurations.all {
    resolutionStrategy.force('com.android.support:support-annotations:23.1.1')
}
  • Another way would be exclude android:support:annotations libs:

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

and remove this:

configurations.all {
    resolutionStrategy.force('com.android.support:support-annotations:23.1.1')
}

Espresso and its all artifacts like web or intents are using Android Support libraries in version 23.1.1and older (espresso-contrib)

Notice that the latest version of Android Support Library is already 24.2.0, not 24.1.1.

Hope it will help

piotrek1543
  • 19,130
  • 7
  • 81
  • 94