简体   繁体   English

无法导入AndroidJUnit4和ActivityTestRule

[英]Cannot import AndroidJUnit4 and ActivityTestRule

I'm trying to make a class for instrumentation test. 我正在尝试开设一门仪器测试类。 This class is in the androidTest folder app\\src\\androidTest\\java\\... . 此类位于androidTest文件夹app\\src\\androidTest\\java\\... However, AndroidJUnit4 and ActivityTestRule cannot resolve. 但是, AndroidJUnit4ActivityTestRule无法解析。 In the import statement, import android.support.test.runner.AndroidJUnit4 the word "runner" is in red. 在import语句中, import android.support.test.runner.AndroidJUnit4 ,单词“ runner”为红色。 The test class is in Kotlin. 测试班在科特林。 I tried deleted it and created one in Java, but still cannot import AdnroidJUnit4. 我尝试将其删除并在Java中创建了一个,但仍无法导入AdnroidJUnit4。 I read some similar questions but didn't find the solution to my case. 我读了一些类似的问题,但没有找到解决方案。 Those two are the project and app build.gradle files: Is there anything wrong with my gradle files? 那两个是项目和应用程序的build.gradle文件:gradle文件有什么问题吗? app build.gradle: 应用程序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 最高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
        }

update Finally find out that this is because a blank space in project path. 更新最后发现这是因为项目路径中有空白。 I removed the space and now can import the classes. 我删除了空间,现在可以导入类了。

Try adding these in the dependancy these 尝试添加这些依赖项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM