如何为AndroidX测试配置Android项目

时间:2019-04-11 07:02:57

标签: junit android-gradle androidx androidx-test

我在配置Android应用程序以进行AndroidX测试时遇到困难。

当我尝试运行初始测试时,出现此错误:-

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:processDebugAndroidTestManifest'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
   > Could not resolve com.google.guava:listenablefuture:1.0.
     Required by:
         project :app
      > Cannot find a version of 'com.google.guava:listenablefuture' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.0.1-android' --> 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'com.google.guava:listenablefuture:1.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'androidx.concurrent:concurrent-futures:1.0.0-alpha02' --> 'com.google.guava:listenablefuture:1.0'
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0

   > Could not resolve com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava.
     Required by:
         project :app > androidx.test.ext:truth:1.1.0 > com.google.guava:guava:27.0.1-android
      > Cannot find a version of 'com.google.guava:listenablefuture' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.0.1-android' --> 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'com.google.guava:listenablefuture:1.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'androidx.concurrent:concurrent-futures:1.0.0-alpha02' --> 'com.google.guava:listenablefuture:1.0'
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0

   > Could not resolve com.google.guava:listenablefuture:1.0.
     Required by:
         project :app > androidx.core:core:1.1.0-alpha05
         project :app > androidx.concurrent:concurrent-futures:1.0.0-alpha02
      > Cannot find a version of 'com.google.guava:listenablefuture' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.0.1-android' --> 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'com.google.guava:listenablefuture:1.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'androidx.concurrent:concurrent-futures:1.0.0-alpha02' --> 'com.google.guava:listenablefuture:1.0'
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.guava:listenablefuture' strictly '1.0' because of the following reason: debugRuntimeClasspath uses version 1.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

我的摇篮像这样:-

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.research.jayunit"
        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'
        }
    }
    buildToolsVersion '29.0.0 rc2'
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.core:core-ktx:1.1.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'

    // Core library
    androidTestImplementation 'androidx.test:core:1.1.0'

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.1.1'

    // Assertions
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.ext:truth:1.1.0'
    androidTestImplementation 'com.google.truth:truth:0.44'

    // Espresso dependencies
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.1'
    androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.1'

    // The following Espresso dependency can be either "implementation"
    // or "androidTestImplementation", depending on whether you want the
    // dependency to appear on your APK's compile classpath or the test APK
    // classpath.
    androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.1'
}

我试图执行的测试类似于:-

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val context = ApplicationProvider.getApplicationContext<Context>()
        assertEquals("com.research.jayunit", context.packageName)
    }
}

我在做什么错了?

如何解决gradle依赖问题?

我遵循了这个link节目,该节目显示了配置AndroidX进行测试

当我更改我的应用程序gradle以明确包括番石榴

   // https://mvnrepository.com/artifact/com.google.guava/guava
    implementation group: 'com.google.guava', name: 'guava', version: '27.1-android'

相关性问题更改为以下内容:-

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 org.checkerframework:checker-compat-qual:2.5.2.
     Required by:
         project :app
      > Cannot find a version of 'org.checkerframework:checker-compat-qual' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.truth:truth:0.44' --> 'org.checkerframework:checker-compat-qual:2.5.5'
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.guava:guava:27.1-android' --> 'org.checkerframework:checker-compat-qual:2.5.2'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.1-android' --> 'org.checkerframework:checker-compat-qual:2.5.2'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.truth:truth:0.44' --> 'org.checkerframework:checker-compat-qual:2.5.5'
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2

   > Could not resolve com.google.errorprone:error_prone_annotations:2.2.0.
     Required by:
         project :app
      > Cannot find a version of 'com.google.errorprone:error_prone_annotations' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.truth:truth:0.44' --> 'com.google.errorprone:error_prone_annotations:2.3.1'
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.guava:guava:27.1-android' --> 'com.google.errorprone:error_prone_annotations:2.2.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.1-android' --> 'com.google.errorprone:error_prone_annotations:2.2.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.truth:truth:0.44' --> 'com.google.errorprone:error_prone_annotations:2.3.1'
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0

   > Could not resolve org.checkerframework:checker-compat-qual:2.5.5.
     Required by:
         project :app > com.google.truth:truth:0.44
      > Cannot find a version of 'org.checkerframework:checker-compat-qual' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.truth:truth:0.44' --> 'org.checkerframework:checker-compat-qual:2.5.5'
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.guava:guava:27.1-android' --> 'org.checkerframework:checker-compat-qual:2.5.2'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.1-android' --> 'org.checkerframework:checker-compat-qual:2.5.2'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.truth:truth:0.44' --> 'org.checkerframework:checker-compat-qual:2.5.5'
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2

   > Could not resolve com.google.errorprone:error_prone_annotations:2.3.1.
     Required by:
         project :app > com.google.truth:truth:0.44
      > Cannot find a version of 'com.google.errorprone:error_prone_annotations' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.truth:truth:0.44' --> 'com.google.errorprone:error_prone_annotations:2.3.1'
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.guava:guava:27.1-android' --> 'com.google.errorprone:error_prone_annotations:2.2.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.1-android' --> 'com.google.errorprone:error_prone_annotations:2.2.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.truth:truth:0.44' --> 'com.google.errorprone:error_prone_annotations:2.3.1'
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0

   > Could not resolve org.checkerframework:checker-compat-qual:2.5.2.
     Required by:
         project :app > com.google.guava:guava:27.1-android
      > Cannot find a version of 'org.checkerframework:checker-compat-qual' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.truth:truth:0.44' --> 'org.checkerframework:checker-compat-qual:2.5.5'
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.guava:guava:27.1-android' --> 'org.checkerframework:checker-compat-qual:2.5.2'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.1-android' --> 'org.checkerframework:checker-compat-qual:2.5.2'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.truth:truth:0.44' --> 'org.checkerframework:checker-compat-qual:2.5.5'
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2
           Constraint path 'JayUnit:app:unspecified' --> 'org.checkerframework:checker-compat-qual' strictly '2.5.2' because of the following reason: debugRuntimeClasspath uses version 2.5.2

   > Could not resolve com.google.errorprone:error_prone_annotations:2.2.0.
     Required by:
         project :app > com.google.guava:guava:27.1-android
      > Cannot find a version of 'com.google.errorprone:error_prone_annotations' that satisfies the version constraints: 
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.truth:truth:0.44' --> 'com.google.errorprone:error_prone_annotations:2.3.1'
           Dependency path 'JayUnit:app:unspecified' --> 'com.google.guava:guava:27.1-android' --> 'com.google.errorprone:error_prone_annotations:2.2.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.1-android' --> 'com.google.errorprone:error_prone_annotations:2.2.0'
           Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.truth:truth:0.44' --> 'com.google.errorprone:error_prone_annotations:2.3.1'
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.2.0
           Constraint path 'JayUnit:app:unspecified' --> 'com.google.errorprone:error_prone_annotations' strictly '2.2.0' because of the following reason: debugRuntimeClasspath uses version 2.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 1s

1 个答案:

答案 0 :(得分:4)

我找到了解决方法

通过将其添加到我的gradle构建文件中

 androidTestImplementation ("androidx.test.ext:truth:1.1.0") {
        exclude group: "com.google.truth", module: "truth"
    }

    androidTestImplementation ("com.google.truth:truth:0.44") {
        exclude group: "org.checkerframework", module: "checker-compat-qual"
        exclude group: "com.google.errorprone", module: "error_prone_annotations"
    }