无法导入 AndroidJUnit4 和 ActivityTestRule



我正在尝试为仪器测试创建一个类。此类位于 androidTest 文件夹 appsrcandroidTestjava... 中。但是,AndroidJUnit4ActivityTestRule无法解决。在导入语句中,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
        }

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

尝试将这些添加到依赖项中

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

相关内容

  • 没有找到相关文章

最新更新