Question

I'm trying to run my android tests with Gradle but it does not see my dependencies.

In my build.gradle I have these lines:

dependencies {
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile 'com.android.support:support-v4:19.1.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Android annotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"

    // ORMLite
    compile "com.j256.ormlite:ormlite-android:$ORMLiteVersion"
    compile "com.j256.ormlite:ormlite-core:$ORMLiteVersion"

    // Google Guava
    compile 'com.google.guava:guava:16.0.1'

    // Joda Time
    compile 'joda-time:joda-time:2.3'

    // Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
    testLocalCompile 'junit:junit:4.11'
    testLocalCompile 'com.google.android:android:4.1.1.4'
    testLocalCompile 'com.android.support:support-v4:19.1.0'
    testLocalCompile 'org.robolectric:robolectric:2.1.+'

    // Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
    // which is Android Studio's test task
    instrumentTestCompile 'junit:junit:4.11'
    instrumentTestCompile 'com.google.android:android:4.1.1.4'
    instrumentTestCompile 'com.android.support:support-v4:19.1.0'
    instrumentTestCompile 'org.robolectric:robolectric:2.1.+'
}

But in my class: instrumentTest/java/.../Test I cannot import i.e. org.junit

Full build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:0.9.2'
        // the latest version of the android-apt plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.1'
    }
}
apply plugin: 'android'
apply plugin: 'android-apt'

repositories {
    mavenCentral()
    mavenLocal()
}

configurations {
    apt
}

def AAVersion = '3.0.1'
def ORMLiteVersion = '4.46'
def mainPackage = 'pl.grzeslowski.transport'
def paidPackage = mainPackage + '.paid'
def freePackage = mainPackage + '.free'

def testPath = 'src/instrumentTest/'
sourceSets {
    testLocal {
        java.srcDir file(testPath + 'java')
        resources.srcDir file(testPath + 'resources')
    }
    instrumentTest {
        java.srcDir file(testPath + 'java')
        resources.srcDir file(testPath + 'resources')
    }
}

//configurations {
//    testLocalCompile {
//        extendsFrom compile
//    }
//}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"

        // tests
        testPackageName "pl.grzeslowski.transport.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        testHandleProfiling true
        testFunctionalTest true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }

    signingConfigs {
        debug {
            storeFile file("../../debug.keystore")
        }
    }

    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

            buildConfigField "java.lang.String", "DATABASE_NAME", "\"transporter\""
            buildConfigField "int", "DATABASE_VERSION", "1"

            buildConfigField "java.lang.String", "PAID_PACKAGE_NAME", "\"$paidPackage\""
            buildConfigField "java.lang.String", "FREE_PACKAGE_NAME", "\"$freePackage\""
        }

        debug.initWith(buildTypes.release)
        debug {
            debuggable true
            runProguard false
            signingConfig signingConfigs.debug
        }
    }

    productFlavors {
        free {
            packageName freePackage
            buildConfigField "pl.grzeslowski.transport.product_flavors.MonetizationType", "MONETIAZATION_TYPE", "pl.grzeslowski.transport.product_flavors.MonetizationType.FREE"
        }

        paid {
            packageName paidPackage
            buildConfigField "pl.grzeslowski.transport.product_flavors.MonetizationType", "MONETIAZATION_TYPE", "pl.grzeslowski.transport.product_flavors.MonetizationType.PAID"
        }
    }

    // tell Android studio that the instrumentTest source set is located in the unit test source set
    sourceSets {
        instrumentTest.setRoot(testPath)
        main.java.srcDirs += testPath + 'java'
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile 'com.android.support:support-v4:19.1.0'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // Android annotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"

    // ORMLite
    compile "com.j256.ormlite:ormlite-android:$ORMLiteVersion"
    compile "com.j256.ormlite:ormlite-core:$ORMLiteVersion"

    // Google Guava
    compile 'com.google.guava:guava:16.0.1'

    // Joda Time
    compile 'joda-time:joda-time:2.3'

    // Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
    testLocalCompile 'junit:junit:4.11'
    testLocalCompile 'com.google.android:android:4.1.1.4'
    testLocalCompile 'com.android.support:support-v4:19.1.0'
    testLocalCompile 'org.robolectric:robolectric:2.1.+'

    // Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
    // which is Android Studio's test task
    instrumentTestCompile 'junit:junit:4.11'
    instrumentTestCompile 'com.google.android:android:4.1.1.4'
    instrumentTestCompile 'com.android.support:support-v4:19.1.0'
    instrumentTestCompile 'org.robolectric:robolectric:2.1.+'
}

def getSourceSetName(variant) {
    return new File(variant.dirName).getName();
}

android.applicationVariants.all { variant ->
    def aptOutputDir = project.file("build/source/apt")
    def aptOutput = new File(aptOutputDir, variant.dirName)
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest: ${variant.processResources.manifestFile}"
    println "aptOutput: ${aptOutput}"
    println "****************************"

    android.sourceSets[getSourceSetName(variant)].java.srcDirs += aptOutput.getPath()

    variant.javaCompile.options.compilerArgs += [
            '-processorpath', configurations.apt.getAsPath(),
            '-AandroidManifestFile=' + variant.processResources.manifestFile,
            '-AresourcePackageName=' + mainPackage,
            '-s', aptOutput
    ]

    variant.javaCompile.source = variant.javaCompile.source.filter { p ->
        return !p.getPath().startsWith(aptOutputDir.getPath())
    }

    variant.javaCompile.doFirst {
        aptOutput.mkdirs()
    }
}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir

    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().replaceAll(/\\+/, '/').split('/')
        buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }

    classpath = sourceSets.testLocal.runtimeClasspath
}

check.dependsOn localTest

assembleDebug.finalizedBy testLocalClasses
Was it helpful?

Solution

In version 0.9.+ of the Android Gradle Plugin

instrumentTestCompile

was changed to

androidTestCompile

Check out this simple template if you need to see a build.gradle file: Deckard (for Gradle)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top