2014-05-03 336 views
0

我试图用Gradle运行我的android测试,但它没有看到我的依赖关系。Gradle不包括测试依赖关系

在我的build.gradle我有这行:

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.+' 
} 

但在我的课:instrumentTest/JAVA /.../测试我无法导入即org.junit

完整的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 

回答

1

在0.9版本+ Android的摇篮的插件

instrumentTestCompile

改为

androidTestCompile

退房,如果你需要看的build.gradle文件,这个简单的模板:Deckard (for Gradle)

+0

+为戴克链接 – MAGx2