I am facing an issue when I try to stop gradle when a condition is not met.
I want when I execute a specific task and a file is not present to throw a GradleException but instead it fails to sync with Android Studio.
My code:
def mapFile = project.rootProject.file('../../maps.json')
buildConfigField "error", "BUILD_ERROR", '"maps.json is missing"'
if(!mapFile.exists()) throw new GradleException("File does not exist")
I have found this solution(don't really like it) with code:
buildConfigField "error", "BUILD_ERROR", '"maps.json is missing"'
so the BuildConfig will generate an error variable and the gradle will stop. Any other options of actually throwing an exception and the sync does not fail?
To let the task fail and not the whole build, you must raise the exception at task execution:
task myTask(type: MyType) {
def mapFile = project.rootProject.file('../../maps.json')
// do some configuration ...
doFirst {
if(!mapFile.exists()) throw new GradleException("File does not exist")
}
}
Related
I have around 25 tests on my android tests package. I have some tests that I to run during integration and some tests that run as normal instrumentation tests. I am trying to call the integration tests using Gradle tasks but it seems impossible for instrumentation tests. I have checked for unit tests and it's possible with something like this-
task integrationTest(
type: Test,
description: 'Run integration tests only. Pass in \'-Pintegration=true\'',
dependsOn: ['testDebugUnitTest', 'clean'] ) {
//Here for task completion, not actually used since sub task of testDebugUnitTest
testClassesDir = file("src/integrationTest/java/");
classpath = files("$System.env.ANDROID_HOME/sources/android-18")
//
//Turn on integration testing when argument exists and is true
//
if (project.hasProperty('integration')) {
println integration
if (integration == 'true') {
integrationTests = true
}
}
}
and doing something like this
testOptions {
unitTests.all {
useJUnit()
if (integrationTests.toBoolean()) {
println "Integration Tests Only for " + it.name
options {
excludeCategories 'com.example.reactivemvp.categories.UnitTest'
}
} else {
println "Unit Tests Only for " + it.name
options {
excludeCategories 'com.example.reactivemvp.categories.IntegrationTest'
}
}
}
}
I have seen that the testOptions only works with unit tests and not with instrumentation tests. I found out what is the method for access the instrumentation tests-
testVariants.all { variant ->
if(connectedInstrumentTest.name)
variant.connectedInstrumentTest.doFirst {
println "This will be executed right after our connectedInstrumentTest!"
println "The name of the test type: $connectedInstrumentTest.name"
if (intergrationTests == true) {
exclude '**/*androidTest.functionalTests*'
}
}
}
I get the error as
Could not find method exclude() for arguments [**/*androidTest.functionalTests*] on task ':app:connectedAndroidTest'
Is there a way to call only a section of the instrumentation tests by some Gradle task?
I'm trying to upload to Crittercism on build, and I put this at the bottom of my gradle file. However, it doesn't seem to be working when I build debug. What am I doing wrong?
task uploadMappingToProd() << {
def critterAppId = "abcde"
def appVersionName = "1.0.1"
def appVersionCode = "DEBUG"
def critterKey = "12345"
commandLine 'curl',
"https://app.crittercism.com/api_beta/proguard/$critterAppId",
'-F', 'proguard=#build/outputs/mapping/production/release/mapping.txt',
'-F', "app_version=$appVersionName-$appVersionCode",
'-F', "key=$critterKey"
type Exec
dependsOn 'assembleDebug'
}
The way you've done it, the task that you have defined, uploadMappingToProd, will if invoked by some reason also invoke assembleDebug. Because you have asked uploadMappingToProd to depend on assembleDebug - not the reverse. Therefore assembleDebug will happily finish without getting anywhere close to uploadMappingToProd.
If you want the reverse dependency i.e. assembleDebug to depend on uploadMappingToProd then you need to add this line after defining your task.
afterEvaluate {
tasks.assembleDebug.dependsOn uploadMappingToProd
}
This will guarantee uploadMappingToProd is invoked everytime and before assembleDebug is invoked.
My solution is working fine.(local build and jenkins build)
for me, only working when build is from jenkins
make sure your APPID, APPKEY, and path(release_jenkins.... normally release)
build.gradle (app) (add at the end)
task uploadPro << {
logger.error("Uploading mapping.txt file to crittercism")
String temp = projectDir;
temp = temp.replace("\\", "/");
String[] cmd = ["curl", "-k", "https://app.crittercism.com/api_beta/proguard/AppIDSting",
"-F", "proguard=#" + temp + "/build/outputs/mapping/release_jenkins/mapping.txt",
"-F", "app_version=" + VERSION_NAME + '-' + VERSION_CODE,
"-F", "key=API_KEY"]
logger.error("CMD : " + cmd)
ProcessBuilder builder = new ProcessBuilder(cmd);
Process process = builder.start();
process.waitFor()
println process.err.text
println process.text
}
gradle.buildFinished {
//check your build type. I am not sure it's the best way to do it.
logger.error("JSC : 이름 ! - " + gradle.startParameter.taskNames);
if (gradle.startParameter.taskNames.contains("assembleRelease_jenkins")) {
logger.error("JSC : 올리기 시작 ! - " + gradle.startParameter.taskNames);
tasks.uploadPro.execute()
} else {
logger.error("JSC : PASS")
}
}
sample project -> https://github.com/SinsangMarket/CrittercismMappingTXT
I have a library that is being build within a Travis CI instance. After the build completes, I would like it to upload the artifact using ./gradlew uploadArchives. The problem right now is that, while I can do this locally, the Travis instance doesn't have my server key in its .ssh/known_hosts file (it probably doesn't even HAVE a .ssh/known_hosts file.
I am using the following configuration of uploadArchives in my gradle configuration:
uploadArchives {
repositories {
mavenDeployer {
pom.packaging = "aar"
pom.groupId = project.CORE_GROUP
pom.version = project.CORE_VERSION_NAME
}
}
}
gradle.taskGraph.beforeTask { Task aTask ->
if (aTask == uploadArchives) {
checkArtifactPublishProperties()
aTask.repositories.mavenDeployer.configuration = configurations.deployerJars
aTask.repositories.mavenDeployer.repository(url: "scp://" + project.publishArtifactHost + ":" + getArtifactTargetDirectory()) {
authentication(userName: project.publishArtifactUsername, privateKey: ext.publishArtifactKeyFile)
}
}
}
I'm not sure how to specify the StrictHostKeyChecking=no as a parameter to this method. My gut tells me that I have to add it to the configuration configurations.deployerJars, but I'm not sure how to do this.
Can someone point me to the documentation for this and perhaps provide a sample of how I might go about this?
I'm trying migrating a normal Android Studio (IntelliJ) project to Gradle project recently. And currently I'm encounter a problem: IntelliJ gives me a warning on the beginning of every file says that my 'package name does not correspond to the file path'. e.g.
The first line of my some/prefixes/a/b/c/d/E.java is:
package a.b.c.d;
....
IntelliJ thinks the package name should be 'c.d' instead of 'a.b.c.d'. Because I set
SourceSets {
main.java.srcDirs = ["some/prefixes/a/b"]
}
in the module's build.gradle.
I know I could do the change below to make IntelliJ happy:
SourceSets {
main.java.srcDirs = ['some/prefixes']
}
But I can't do that because there're huge numbers of projects under 'some/prefixes' and I definitely don't want to introduce all of them into this module.
I used to add a packagePrefix="a.b" in my 'module.iml' in my original Android studio project and it works well:
https://www.jetbrains.com/idea/help/configuring-content-roots.html#d2814695e312
But I don't know how to accomplish similar fix after migrating to Gradle project.
I end up to write a task for gradle.
The task add the famous packagePrefix to the *.iml file.
This solution only work for intelliJ, I hope someone have a better solution.
task addPackagePrefix << {
println 'addPackagePrefix'
def imlFile = file(MODULE_NAME+".iml")
if (!imlFile.exists()) {
println 'no module find '
return
}
def parsedXml = (new XmlParser()).parse(imlFile)
if(parsedXml.component[1] && parsedXml.component[1].content){
parsedXml.component[1].content.findAll { Node node ->
node.sourceFolder.findAll { Node s ->
def url = s.attribute("url").toString()
if (url.endsWith(SRC_DIR)) {
println 'Node founded '
def attr = s.attribute('packagePrefix')
if (attr == null) {
// add prefix
println 'Adding package prefix'
s.attributes().put('packagePrefix', PACKAGE_NAME)
println s.toString()
// writing
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)
imlFile.text = writer.toString()
}
}
}
}
}
I'm using a gradle task which executes the command-line inside a file collection loop:
...
collection.each { file ->
exec {
workingDir = file(props['WORKING_DIR']).getAbsolutePath()
commandLine "java", "-jar", file(props['SIGN_TOOL']).getAbsoluteFile(), file
}
}
...
Unfortunately, the gradle task ends up with this error:
Execution failed for task ':signFiles'.
No signature of method: java.io.File.call() is applicable for argument types: (java.lang.String) values: Possible
solutions: wait(), any(), wait(long), each(groovy.lang.Closure),
any(groovy.lang.Closure), list()
How can i fix this issue?
Thx MVM
You've called your loop var file and then it's trying to use that for the call to file()...
Try renaming your closure variable:
collection.each { aFile ->
exec {
workingDir = file(props['WORKING_DIR']).getAbsolutePath()
commandLine "java", "-jar", file(props['SIGN_TOOL']).getAbsoluteFile(), aFile
}
}