JUnit tests in Android from Gradle: “package andro

2019-07-22 04:36发布

问题:

I'm not sure how to get my unit tests to run through Gradle. I created a unitTest task, set appropriate source sets, and appropriate dependencies, but I'm getting the error:

package android.test does not exist

I followed directions in another post and added the following to my build.gradle:

// Unit tests
sourceSets {
    unitTest {
        java.srcDir file('test')
        resources.srcDir file('test/res')
    }
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/debug")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'com.google.android:android:4.0.1.2'
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}

Full build.gradle below:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}

configurations {
    apt
}

apply plugin: 'android'

dependencies {
    compile project(':PullToRefresh')
    compile project(':simple-crop-image-lib')
    compile project(':facebook')
    compile project(':gridlayout')
    compile project(':nineoldandroids')
    compile project(':ViewPagerIndicator')
    compile project(':volley')
    compile fileTree(dir: 'libs', include: '*.jar')
    apt files('compile-libs/androidannotations-2.7.1.jar')
}

android {
    buildToolsVersion '17.0'
    compileSdkVersion 17

    signingConfigs {
        debug {
            storeFile file('keystores/debug.keystore')
        }
    }

    buildTypes {
        debug {}

        ipconfigurable {
            debuggable true
            signingConfig signingConfigs.debug
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

// Unit tests
sourceSets {
    unitTest {
        java.srcDir file('src/test')
        resources.srcDir file('src/test/res')
    }
}

dependencies {
    unitTestCompile files("$project.buildDir/classes/debug")
    unitTestCompile 'junit:junit:4.11'
    unitTestCompile 'com.google.android:android:4.0.1.2'
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

task unitTest(type:Test, dependsOn: assemble) {
    description = "run unit tests"
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}

Directory Structure:

回答1:

Thanks to @PerrynFowler, the solution was to add the following line to my unitTestCompile dependencies:

unitTestCompile 'com.google.android:android-test:4.1.1.4'