9

I'm new to android.

I'm developing in Java.

My goal is to use FragmentScenraio in my test framework.

In the guides I've found I see I need to add:

dependencies {
    def fragment_version = "1.2.2"
    // ...
    debugImplementation 'androidx.fragment:fragment-testing:$fragment_version'
}

to my dependecies.

I'm adding it and the project builds succefuly.

The problem is that when I'm running the app in Android Studio I'm getting

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:preDebugAndroidTestBuild'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
   > Could not resolve androidx.test:core:1.3.0-alpha04.
     Required by:
         project :app
         project :app > androidx.test:core-ktx:1.3.0-alpha04
         project :app > androidx.test.ext:junit:1.1.2-alpha04
         project :app > androidx.test.espresso:espresso-intents:3.3.0-alpha04
      > Cannot find a version of 'androidx.test:core' that satisfies the version constraints: 
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
           Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'

   > Could not resolve androidx.test:core:{strictly 1.2.0}.
     Required by:
         project :app
      > Cannot find a version of 'androidx.test:core' that satisfies the version constraints: 
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
           Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'

   > Could not resolve androidx.test:monitor:{strictly 1.2.0}.
     Required by:
         project :app
      > Cannot find a version of 'androidx.test:monitor' that satisfies the version constraints: 
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
           Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:monitor:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:runner:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'

   > Could not resolve androidx.test:monitor:1.3.0-alpha04.
     Required by:
         project :app > androidx.test:core:1.3.0-alpha04
         project :app > androidx.test.ext:junit:1.1.2-alpha04
         project :app > androidx.test:runner:1.3.0-alpha04
      > Cannot find a version of 'androidx.test:monitor' that satisfies the version constraints: 
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
           Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:monitor:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:runner:1.3.0-alpha04' --> 'androidx.test:monitor:1.3.0-alpha04'

   > Could not resolve androidx.test:core:1.2.0.
     Required by:
         project :app > androidx.fragment:fragment-testing:1.2.2
      > Cannot find a version of 'androidx.test:core' that satisfies the version constraints: 
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
           Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
           Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s

Can someone please tell me what is going on here?

I tried to follow this, but it didn't work either.

Thanks.

I'm adding my app/gradle.build file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion rootProject.buildToolsVersion
    defaultConfig {
        applicationId "com.example.examscanner"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    def camerax_version = "1.0.0-beta01"
    def fragment_version = "1.2.2"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    //noinspection GradleCompatible
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    // CameraX core library using the camera2 implementation
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    // If you want to use the CameraX View class
    implementation "androidx.camera:camera-view:1.0.0-alpha08"
    // If you want to use the CameraX Extensions library
    implementation "androidx.camera:camera-extensions:1.0.0-alpha08"
    // If you want to use the CameraX Lifecycle library
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    // RxJava
    implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
    implementation "io.reactivex.rxjava2:rxjava:2.1.3"
    implementation ("androidx.fragment:fragment-testing:"+fragment_version, {
        exclude group: 'androidx.test', module: 'core'
    })

    // androidTest
    androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
    androidTestImplementation 'androidx.test:core-ktx:' + rootProject.coreVersion
    androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
    androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
    androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
    androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
    androidTestImplementation 'androidx.test.espresso:espresso-intents:'+rootProject.espressoVersion



    // test
    testImplementation 'androidx.test:core:' + rootProject.coreVersion;
    testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
    testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
    testImplementation 'androidx.test.espresso:espresso-intents:' + rootProject.espressoVersion
    testImplementation 'androidx.test.ext:truth:' + rootProject.extTruthVersion
    testImplementation 'org.json:json:20140107'



    debugImplementation 'androidx.fragment:fragment-testing:'+fragment_version
    apply plugin: "androidx.navigation.safeargs"
}

Rotem Barak
  • 154
  • 1
  • 9

1 Answers1

13

These are the important lines:

Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core:1.3.0-alpha04'
Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test:core-ktx:1.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.ext:junit:1.1.2-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.3.0-alpha04' --> 'androidx.test:core:1.3.0-alpha04'
Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'

Let's unpack what each means. The last line

Dependency path 'ExamScanner:app:unspecified' --> 'androidx.fragment:fragment-testing:1.2.2' --> 'androidx.test:core:1.2.0'

Says that fragment-testing:1.2.2 depends on androidx.test:core:1.2.0. This makes sense, it is a testing library.

The second line is

Constraint path 'ExamScanner:app:unspecified' --> 'androidx.test:core:{strictly 1.2.0}' because of the following reason: debugRuntimeClasspath uses version 1.2.0

Which says that the debugRuntimeClasspath uses androidx.test:core:1.2.0 (that's what is pulled in by fragment-testing:1.2.2, so that's expected).

The important part is that this is a {strictly 1.2.0} - you can't change the version used in androidTest or test since this potentially invalidate what the debug expects.

The rest of the lines indicate that your rootProject.coreVersion version is 1.3.0-alpha04. But there's no version that is both {strictly 1.2.0} (your Constraint) and 1.3.0-alpha04, hence the error.

You can upgrade your debugImplementation by adding those dependencies to debugImplementation as well:

debugImplementation 'androidx.test:core:' + rootProject.coreVersion
debugImplementation 'androidx.test:monitor:' + rootProject.coreVersion

This means that your debug and androidTest (and test) versions will all match, avoiding the constraint issue.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • So frustrating, so the official guides are wrong and one has to go to extra hoops just to setup basic stuff. This makes it very hard to encourage devs to incorporate UI testing, the effort is just not worth it – Alvin Dizon Feb 18 '21 at 07:07
  • No, you need to use `debugImplementation` to get `fragment-testing` to work at all. The documentation is right about that. – ianhanniballake Feb 18 '21 at 14:53
  • `debugImplementation` + a hack. And in the codelab for advanced testing, `implementation` is used: https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-test-doubles#7 – Alvin Dizon Feb 18 '21 at 21:54
  • 2
    `implementation` is a superset of both `debugImplementation` and `releaseImplementation` (and any other configurations you have). The important part is that you cannot use `androidTestImplementation`. That's always true. – ianhanniballake Feb 18 '21 at 22:30
  • 1
    The same currently happens with version 1.4.0 and Hilt (which brought me here). – Martin Zeitler Jul 03 '21 at 11:02