[英]Robolectric, Gradle, Espresso With Flavors
I was using this template to incorporate robolectric tests but it's not working because I use flavors and I get this error: 我使用此模板合并了robolectric测试,但是由于使用了口味并且出现此错误,因此无法正常工作:
Error:Could not find property 'testRelease' on project ':Component_Tests'.
How can I fix this? 我怎样才能解决这个问题? I'm using 2 flavors and 3 build types.
我正在使用2种口味和3种构建类型。 Here's the gradle file:
这是gradle文件:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:0.12.1+'
classpath "com.neenbedankt.gradle.plugins:android-apt:1.2"
}
}
apply plugin: 'android'
apply plugin: 'android-apt'
apply from : "$rootDir/gradle/signing.gradle"
apply from : "$rootDir/gradle/build_extras.gradle"
apply from : "$rootDir/gradle/environments.gradle"
loadConfiguration()
apply from : "$rootDir/gradle/checkstyle.gradle"
apply from : "$rootDir/gradle/pmd.gradle"
apply from : "$rootDir/gradle/findbugs.gradle"
apply from : "$rootDir/gradle/espresso.gradle"
tasks.whenTaskAdded { task ->
if (task.name.startsWith('connectedAndroidTest')) {
task.dependsOn grantAnimationPermission
}
}
def loadPropertiesFile = { filePath ->
def propertiesFile = file(filePath)
def Properties properties = new Properties()
if (!propertiesFile.canRead()) {
throw new GradleException("cannot read " + filePath)
}
properties.load(new FileInputStream(propertiesFile))
return properties
}
def writePropertiesFile = { filePath, properties ->
def propertiesFile = file(filePath)
if (!propertiesFile.canRead()) {
throw new GradleException("cannot read " + filePath)
}
properties.store(propertiesFile.newWriter(), null)
}
def loadAndStorePropertyFile = { filePath, propertyName, value ->
def Properties properties = loadPropertiesFile(filePath)
properties[propertyName] = value.toString()
writePropertiesFile(filePath, properties)
println "$propertyName is set to $value"
}
def getVersionCode = { ->
def Properties versionProperties = loadPropertiesFile('../version.properties')
return versionProperties['VERSION_CODE'].toInteger()
}
def getAlphaHash = { ->
def Properties versionProperties = loadPropertiesFile('../version.properties')
return versionProperties['LAST_ALPHA_HASH'].toString()
}
def getBetaHash = { ->
def Properties versionProperties = loadPropertiesFile('../version.properties')
return versionProperties['LAST_BETA_HASH'].toString()
}
def getReleaseCandidateHash = { ->
def Properties versionProperties = loadPropertiesFile('../version.properties')
return versionProperties['LAST_RC_HASH'].toString()
}
def getCurrentHash = { ->
def cmd = "git rev-parse --short HEAD"
def gitProcess = cmd.execute()
def gitHash = gitProcess.text.trim()
return gitHash
}
def getReleaseNotes = { ->
def Properties versionProperties = loadPropertiesFile('../version.properties')
return versionProperties['NOTES'].toString()
}
def getReleaseNotesSinceHash = { hash ->
def cmd = "git log --pretty=-%x20[%h]%x20%s%n " + hash + "..HEAD"
def gitProcess = cmd.execute()
def notes = gitProcess.text.trim()
loadAndStorePropertyFile( '../version.properties', 'NOTES', notes)
}
def copyAndRenameZipAlignedPackage = { variant ->
def timeStamp = (int)(new Date().getTime() / 1000)
def versionCode = getVersionCode()
def gitHash = getCurrentHash()
def environment = project.environment
def file = variant.zipAlign.outputFile
def copyTask = project.tasks.create("copy${variant.name}Apk", Copy)
copyTask.from(file)
copyTask.into(file.parent)
if (variant.buildType.name == "release") {
copyTask.rename(".apk", "-" + environment + "-" + versionCode + "-"
+ gitHash + "-" + timeStamp + "-RELEASE.apk");
} else {
copyTask.rename(".apk", "-" + environment + "-" + versionCode + "-"
+ gitHash + "-" + timeStamp + "-SNAPSHOT.apk");
}
variant.assemble.dependsOn copyTask
copyTask.dependsOn variant.zipAlign
}
android {
compileSdkVersion 19
buildToolsVersion '19.1'
useOldManifestMerger true
defaultConfig {
applicationId 'com.example.app’
minSdkVersion 14
targetSdkVersion 19
versionCode getVersionCode()
testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner'
}
productFlavors {
dev {
// For explicit testing with Espresso and other frameworks.
}
hockey {
// For distro to stakeholders.
}
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
}
lintOptions {
abortOnError true
}
jacoco {
version = '0.6.2.201302030002'
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
androidTest.setRoot('tests')
debug.setRoot('build-types/debug')
beta.setRoot('build-types/beta')
release.setRoot('build-types/release')
dev.setRoot('build-types/dev')
hockey.setRoot('build-types/hockey')
}
buildTypes {
debug {
applicationIdSuffix '.debug'
debuggable true
// still broken with dagger (espresso's dependency) as of 0.11.1
// https://code.google.com/p/android/issues/detail?id=69174
testCoverageEnabled false
}
beta {
applicationIdSuffix '.beta'
debuggable true
testCoverageEnabled false
signingConfig signingConfigs.betaSigning
}
release {
runProguard false
proguardFile 'proguard-config.txt'
signingConfig signingConfigs.releaseSigning
}
}
applicationVariants.all { variant ->
removeSetAnimationScaleFromManifest(variant)
task("generate${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for $variant.name."
group "Code Quality"
def Properties localProperties = loadPropertiesFile('../local.properties')
source = variant.javaCompile.source
classpath = files(variant.javaCompile.classpath.files) +
files(localProperties['sdk.dir'] + "/platforms/android-4.4.2/android.jar")
}
copyAndRenameZipAlignedPackage(variant)
}
}
apply plugin: 'monkey'
monkey {
teamCityLog = false
eventCount = 5000
seed = 42
failOnFailure = false
install = false
}
apply plugin: 'hockeyApp'
hockeyapp {
apiToken = “xxxxx”
releaseType = 0
notify = 0
status = 2
notesType = 1
commitSha = getCurrentHash()
notes = getReleaseNotes()
variantToApplicationId = [
hockeyDebug: “xxxxx”,
hockeyBeta: “xxxxx”,
hockeyRelease: “xxxxx”
]
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':android_ngn_stack:android-ngn-stack')
compile project(':SlidingMenuLibrary')
compile 'com.android.support:support-v4:19.1.+'
compile 'com.google.android.gms:play-services:5.0.77'
compile 'org.apache.james:apache-mime4j:0.6'
compile 'org.apache.httpcomponents:httpmime:4.2.3'
compile 'org.jsoup:jsoup:1.6.3'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.googlecode.ez-vcard:ez-vcard:0.9.3'
compile 'com.googlecode.libphonenumber:libphonenumber:5.9'
compile 'net.hockeyapp.android:HockeySDK:3.0.2'
compile("org.springframework.android:spring-android-auth:1.0.1.RELEASE") { exclude module: '*' }
compile("org.springframework.android:spring-android-core:1.0.1.RELEASE") { exclude module: '*' }
compile("org.springframework.android:spring-android-rest-template:1.0.1.RELEASE") { exclude module: '*' }
apt "org.androidannotations:androidannotations:" + androidAnnotationsVersion
provided 'com.jakewharton.espresso:espresso:1.1-r3'
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r3'
}
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
resourcePackageName 'com.example’
}
Here's the test module gradle file: 这是测试模块gradle文件:
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:" + androidToolsBuildGradle
classpath "com.novoda:gradle-android-test-plugin:0.9.9-SNAPSHOT"
}
}
apply plugin: 'java'
test.reports.html.enabled = false // just clean up dashboard from not generated reports
test.reports.junitXml.enabled = false // just clean up dashboard from not generated reports
apply plugin: 'android-test'
apply plugin: "jacoco"
gradle.taskGraph.beforeTask { task ->
if (task.name == "compileTestJava") {
task.deleteAllActions()
println "Task $task.name is being rendered useless"
}
}
//Note - Alphabetical order matters for projectUnderTest
android {
projectUnderTest ':Acision_RCS_Client'
}
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile('com.squareup:fest-android:1.0.+') { exclude module: 'support-v4' }
testCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-plugin-registry'
exclude module: 'maven-profile'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'nekohtml'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'support-v4' // crazy but my android studio don't like this dependency and to fix it remove .idea and re import project
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-http-shared'
exclude module: 'wagon-provider-api'
}
}
apply from: "$rootDir/gradle/jacoco-support.gradle"
apply from: "$rootDir/gradle/android-studio-robolectric-support.gradle"
If you use product flavours, gradle will not find the "testRelease" task. 如果使用产品调味剂,则gradle将找不到“ testRelease”任务。 You need to use the correct naming with the flavour, eg testFlavourRelease (Flavour must be replaced by a existing flavour name used in your project)
您需要对风味使用正确的命名,例如testFlavourRelease(风味必须替换为项目中使用的现有风味名称)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.