无法导入AndroidJUnit4和ActivityTestRule

我试图做一个仪器测试的类。 这个类在androidTest文件夹app\src\androidTest\java\... 。 但是, AndroidJUnit4ActivityTestRule无法解析。 在import语句中, import android.support.test.runner.AndroidJUnit4单词“runner”是红色的。 考试class在Kotlin。 我试图删除它,并在Java中创建一个,但仍不能导入AdnroidJUnit4。 我读了一些类似的问题,但没有find解决我的情况。 这两个是项目和应用程序build.gradle文件:我的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() } 

顶级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 } 

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