问题
After inspiration from this template, I was able to get roblolectric and espresso tests working with android and gradle. The key was to store robolectric in it's own module. However, I 'm unable to add the support library to robolectric. How can I do that?
Here's the gradle file of the robolectric module:
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'
}
}
Here's the top level project gradle file. As you can see I'm bringing in the correct repo's because the App module builds with no problems.
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()
}
}
Addition
While rereading the robolectric read me on github, I noticed they are telling people to move the android support files into their local repos. Since I'm using gradle, this might not do anything since gradle uses it's own caches instead of local maven (~./gradle vs ./m2).
Quote:
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"
回答1:
Theoretically, instead of copying those files into the local maven repository, you should be able to use android repository directly:
apply plugin: 'java'
repositories {
def androidHome = System.getenv("ANDROID_HOME")
maven {
url "$androidHome/extras/android/m2repository/"
}
}
Note, that you have to install Android Support Repository & Google Repository first through the SDK Manager.
来源:https://stackoverflow.com/questions/25130221/adding-support-library-to-robolectric-2-3