0

I'm trying to make a class for instrumentation test. This class is in the androidTest folder app\src\androidTest\java\.... However, AndroidJUnit4 and ActivityTestRule cannot resolve. In the import statement, import android.support.test.runner.AndroidJUnit4 the word "runner" is in red. The test class is in Kotlin. I tried deleted it and created one in Java, but still cannot import 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? 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
        }

update Finally find out that this is because a blank space in project path. I removed the space and now can import the classes.

Robin
  • 1,487
  • 4
  • 17
  • 29

1 Answers1

0

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'
Smaran
  • 1,651
  • 2
  • 11
  • 15