I want to run a command line with Gradle that this command has an output.
I run this command in windows powershell:
./mybat.bat myArgs when I hit enter, it will print some digit, like this:
123456
I want to run this command with gradle and save this result(123456)
here is some code that I written in android build.gradle file:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', './mybat.bat' , 'foo'//this is myArgs for example
}
this works and prints the value 123456, but I want to save it in a variable, how can I do that?
As you can see in the official doc HERE
This can be achieved with the following task
task executeCMD(type:Exec) {
workingDir '.'
commandLine 'mybat.bat', '>', 'log.txt'
doLast {
println "Executed!"
}
}
This will send the output of mybat.bat execution and set the results into a txt file called log .
the . is the directory where you have the script .
in my case its a project root directory .
the best approach I found is to add '/c' to commandLine arguments and use standardOutput, here some code that might help other people:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', '/c', './mybat.bat' , 'foo'//this is myArgs for example
standardOutput = new ByteArrayOutputStream()
doLast {
def result = standardOutput.toString()
println "the result value is: $result"
}
}
Related
I want to re-create verification-metadata.xml file with single click. I can create verification-metadata.xml with this command ./gradlew --write-verification-metadata sha256. I try to create Gradle Task inside the build.gradle(app) and execute, but it didn't work
task verificationMeta(type: Exec) {
workingDir "${rootDir}"
commandLine './gradlew ', '--write', '-verification', '-metadata', ' sha256'
doLast {
println "Executed!"
}
}
rootDir is root directory of project.
This code gives me error;
Execution failed for task ':verificationMeta'.
> A problem occurred starting process 'command './gradlew ''
How can I make it ?
Are you working on windows machine?
We had the problem that on windows machine you have to call gradlew.bat
Example:
task prodRepackage(type: Exec) {
group = "Build"
if (OperatingSystem.current().isWindows()) {
executable "gradlew.bat"
args "-Pprod", "bootJar"
} else {
executable "./gradlew"
args "-Pprod", "bootJar"
}
}
As a workaround for the issue, I use .bat or .sh scripts to be executed as the following.
task executeCMD(type:Exec) {
workingDir '.'
commandLine 'test.bat'
doLast {
println "Executed!"
}
}
This, for example, will execute the test.bat script, which only has one line
./gradlew --write-verification-metadata sha256
if you're using Linux/macOS, you can replace the .bat with .sh.
I have created a gradle closure to generate versionName for an Android Application from git tag. I can run it locally, however I cannot execute in a job of GitHub Actions. The machine is
ubuntu-latest.
Here is the closure and the error.
ext.getVersionName = {
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
} catch (ignored) {
return null
}
}
Error in Actions
> Process 'command 'git'' finished with non-zero exit value 128
How can I run this block in Github Actions ? Do you have any suggestions ?
Thanks in advance.
Force it to check everything out:
- uses: actions/checkout#v2
with:
fetch-depth: 0
I have a problem with Gradle script on Ubuntu 16.04. It looks like my .bashrc is not loaded when I'm invoking script from Android Studio.
My script:
task myTask {
doLast {
exec {
workingDir project.rootProject.rootDir
commandLine 'll' // alias provided from my .bashrc
}
}
When I'm starting it with ./gradlew myTask everything works, but when starting from gui I'm getting
A problem occurred starting process 'command 'll''
What am I doing wrong?
Try that :
task myTask {
doLast {
exec {
workingDir project.rootProject.rootDir
commandLine 'bash', '-c', '-i', 'll' // alias provided from my .bashrc
}
}
}
I'm attempting to add a pre-pre-build shell script to my gradle/Android-Studio build. I've added the following to app/build.gradle:
task prePreBuild << {
commandLine 'ls'
}
preBuild.dependsOn prePreBuild
When I invoke my build with ./gradlew assembleDebug I get the following error:
Could not find method commandLine() for arguments [ls] on project ':app'
If I replace the commandLine line with something like println 'Hello' then it works fine, and I can see the output from my new task.
I searched for other mentions of "Could not find method commandLine" and found nothing. What is the correct way to invoke a shell script from this gradle task?
You need to indicate the type of the task or use the exec block:
task execute(type: Exec) {
}
or
exec {
}
You can find more info on https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
I use bugsnag to monitor crashes in my Android app.
I also use ProGuard when I compile a release version.
I need to upload the mapping.txt file that is generated in build/outputs/mapping/prod/release folder of my project.
When I run this command in (Windows) command line:
curl https://upload.bugsnag.com -F proguard=#C:/mapping.txt -F apiKey=1111111111111111111111 -F versionCode=11111111 -F appId=com.my.package -F versionNumber=1.0.0
The file is uploaded and everything works.
All I need is to add a task to gradle so it uploads the file once its ready.
So once I compile the project for release, the mapping.txt file gets generated and once its ready, upload the file using this curl command (which is taken from bugsnag's web site by the way).
I tried many variations. The current one compiles but I do not think the file is being uploaded... I can't see any indication that it actually happened.
This is the code I currently use:
task uploadPro(type: Exec) {
logger.error("inside upload task")
commandLine 'cmd', '/c', 'curl', 'https://upload.bugsnag.com', '-F','proguard=#build/outputs/mapping/prod/release/mapping.txt', '-F', 'apiKey=1111111111111111111111', '-F', 'versionCode=111111', '-F', 'appId=com.my.package', '-F', 'versionNumber=1.0.0'
standardOutput = new ByteArrayOutputStream()
doLast {
String output = standardOutput.toString()
logger.info(output);
}
}
I also tried using this:
def p = ['cmd', '/c', 'curl', 'https://upload.bugsnag.com', '-F', 'proguard=#build/outputs/mapping/prod/release/mapping.txt', '-F', 'apiKey=11111111111111111111111', '-F', 'versionCode=1111111', '-F', 'appId=com.my.package', '-F', 'versionNumber=1.0.0'].execute()
The way I call this task is by using this command:
tasks.getByName("assembleRelease").finalizedBy(uploadPro)
Im really not sure how to do this. Any help is appreciated!! Thank you!
I found a solution to my own question... mostly. Here is the code I use to run the curl command at the end of the build process:
task uploadPro << {
logger.error("Uploading mapping.txt file to bugsnag")
def myCommand = "cmd /c curl https://upload.bugsnag.com -F proguard=#path\\to\mappingFile\\mapping.txt -F " +
"apiKey=111111111111111111111111 -F versionCode=" + versionCodeId + " -F " +
"appId=com.xxxx.yyy.zzz -F versionNumber=" + versionId
ProcessBuilder builder = new ProcessBuilder(myCommand.split(' '));
Process process = builder.start();
process.waitFor()
println process.err.text
println process.text
}
Then I use this to run it at the end of the build process:
gradle.buildFinished {
tasks.uploadPro.execute()
}
Cant be sure this is best practice but it works.
I would have liked to run the uploadPro task ONLY during a release buildType...but this proves to be difficult as well.