添加支持库到Robolectric 2.3



从这个模板中获得灵感后,我能够在android和gradle上进行roblolectric和espresso测试。关键是要把机器人电储存在自己的模块里。但是,我无法将支持库添加到robolelectric。我该怎么做呢?

这是机器人电气模块的gradle文件:

apply plugin: 'java'
// * What went wrong:
// Execution failed for task ':robolectric-tests:test'.
//        > superClassName is empty!
tasks.withType(Test) {
    scanForTestClasses = false
    include "**/*Test.class"
}
dependencies {
    def androidModule = project(':App')
    compile androidModule
    testCompile androidModule.android.applicationVariants.toList().first().javaCompile.classpath
    testCompile androidModule.android.applicationVariants.toList().first().javaCompile.outputs.files
    testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath())
    testCompile 'junit:junit:4.+'
    testCompile ('org.robolectric:robolectric:2.3') {
        exclude module: 'support-v13'
        exclude module: 'support-v4'
    }
}

这是最高级别的项目gradle文件。正如你所看到的,我正在引入正确的repo,因为App模块构建没有问题。

ext {
    versions = [
        findbugs: '2.0.3',
        checkstyle: '5.7',
        pmd: '5.1.1'
    ]
}
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.1+'
        classpath group: 'com.autoscout24.gradle', name: 'gradle-monkey-plugin', version: '0.7+'
        classpath 'de.felixschulze.gradle:gradle-hockeyapp-plugin:2.1+'
    }
}
allprojects {
    repositories {
        mavenCentral()
    }
}

添加

在github上重读robolectric read me时,我注意到他们正在告诉人们将android支持文件移动到他们的本地仓库中。由于我使用的是gradle,这可能不会做任何事情,因为gradle使用它自己的缓存而不是本地maven(~)。/gradle vs ./m2)。

:

Robolectric requires the Google APIs for Android (specifically, the maps JAR) and Android support-v4 library. To download this onto your development machine use the Android SDK tools and then run the following to install them to your local Maven repository:
mvn install:install-file -DgroupId=com.google.android.maps 
  -DartifactId=maps 
  -Dversion=18_r3 
  -Dpackaging=jar 
  -Dfile="$ANDROID_HOME/add-ons/addon-google_apis-google-18/libs/maps.jar"
mvn install:install-file -DgroupId=com.android.support 
  -DartifactId=support-v4 
  -Dversion=19.0.1 
  -Dpackaging=jar 
  -Dfile="$ANDROID_HOME/extras/android/support/v4/android-support-v4.jar"

理论上,你不需要将这些文件复制到本地maven存储库中,你应该可以直接使用android存储库:

apply plugin: 'java'
repositories {
    def androidHome = System.getenv("ANDROID_HOME")
    maven {
        url "$androidHome/extras/android/m2repository/"
    }
}

注意,你必须安装Android支持库&Google Repository首先通过SDK Manager.

最新更新