无法导入AndroidJUnit4和ActivityTestRule

时间:2018-01-31 16:37:25

标签: android kotlin android-espresso gradle-android-test-plugi

我正在尝试为仪器测试做一个课程。该类位于androidTest文件夹app\src\androidTest\java\...中。但是,AndroidJUnit4ActivityTestRule无法解决。在import语句中,import android.support.test.runner.AndroidJUnit4单词“runner”为红色。测试课在Kotlin。我尝试删除它并在Java中创建一个,但仍无法导入AdnroidJUnit4。我读了一些类似的问题,但没有找到我的案例的解决方案。这两个是项目和应用程序build.gradle文件: 我的gradle文件有什么问题吗? app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "com.sample.me"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        archivesBaseName = "$applicationId-$versionName-$versionCode"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        // For room compat
        multiDexEnabled true
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation":
                                     "$projectDir/schemas".toString()]
            }
        }
    }

    sourceSets {
        androidTest.assets.srcDirs +=
                files("$projectDir/schemas".toString())
    }

    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            minifyEnabled true
        }
    }
    buildToolsVersion '27.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    // Room
    implementation "android.arch.persistence.room:runtime:$rootProject.ext.roomVersion"
    kapt "android.arch.persistence.room:compiler:$rootProject.ext.roomVersion"

    // Support libs
    implementation "com.android.support:recyclerview-v7:$rootProject.ext.supportVersion"
    implementation "com.android.support:design:$rootProject.ext.supportVersion"
    implementation "com.android.support:cardview-v7:$rootProject.ext.supportVersion"
    implementation "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
    implementation "com.android.support.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"

    implementation 'joda-time:joda-time:2.9.9'

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$rootProject.ext.kotlinVersion"

    // Dependencies for local unit tests
    testImplementation "junit:junit:$rootProject.ext.junitVersion"
    testImplementation "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
    testImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"

    // Android Testing Support Library's runner and rules
    androidTestImplementation "com.android.support.test:runner:$rootProject.ext.runnerVersion"
    androidTestImplementation "com.android.support.test:rules:$rootProject.ext.runnerVersion"

    // Dependencies for Android unit tests
    androidTestImplementation "junit:junit:$rootProject.ext.junitVersion"
    androidTestImplementation "org.mockito:mockito-android:$rootProject.ext.mockitoVersion"

    //androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"

    // Resolve conflicts between main and test APK:  not resolve
    androidTestImplementation "com.android.support:support-annotations:$rootProject.ext.supportVersion"
    androidTestImplementation "com.android.support:support-v4:$rootProject.ext.supportVersion"
    androidTestImplementation "com.android.support:recyclerview-v7:$rootProject.ext.supportVersion"
    androidTestImplementation "com.android.support:appcompat-v7:$rootProject.ext.supportVersion"
    androidTestImplementation "com.android.support:design:$rootProject.ext.supportVersion"
    androidTestImplementation "com.android.support.test.espresso.idling:idling-concurrent:$rootProject.ext.espressoVersion"
    androidTestImplementation "android.arch.persistence.room:testing:$rootProject.ext.roomVersion"
}

repositories {
    mavenCentral()
}

top build.gradle

    ext {

    // Sdk and tools
    compileSdkVersion = 26
    targetSdkVersion = 26
    minSdkVersion = 19
    buildToolsVersion = '27.0.1'
    supportVersion = '27.0.2'

    // App dependencies
    espressoVersion = '3.0.1'
    roomVersion = '1.1.0-alpha1'

    junitVersion = '4.12'
    mockitoVersion = '2.8.47'
    hamcrestVersion = '1.3'
    runnerVersion = '1.0.1'
    rulesVersion = '1.0.1'
    espressoVersion = '3.0.1'
}

buildscript {
    ext.kotlinVersion = '1.2.21'
    ext.gradlePluginVersion = '2.3.0'

    repositories {
        jcenter()
        mavenCentral()
        google()

    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0-beta1' // Use plain string for auto update
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" 


      }
    }
        allprojects {
            repositories {
                jcenter()
                google()
                maven { url 'https://maven.google.com' }
            }
        }

        task clean(type: Delete) {
            delete rootProject.buildDir
        }

更新最后发现这是因为项目路径中有空格。我删除了空格,现在可以导入类了。

1 个答案:

答案 0 :(得分:0)

尝试在依赖性中添加这些

androidTestCompile 'com.android.support.test:rules:1.0.2'
androidTestCompile 'com.android.support.test:runner:1.0.2'