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
}
}
}
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 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"
}
}
I was struggling with a problem where I wanted to use Gradle to execute .bat script after .apk build that signed the application with platform key, and reinstalled it on the device.
Folder structure
File instruction.bat content
java -jar "..\app\build\outputs\apk\signapk.jar"
"..\app\build\outputs\apk\platform.x509.pem"
"..\app\build\outputs\apk\platform.pk8"
"..\app\build\outputs\apk\app-debug.apk"
"..\app\build\outputs\apk\signed.apk"
adb install -r "..\app\build\outputs\apk\signed.apk"
adb shell am start -n
com.your.package/com.your.package.MainActivity
Add this to your build.gradle -
task signAndInstall(type: Exec) {
def finalPath = projectDir.toString() + '\build\outputs\apk\instruction.bat'
commandLine = [finalPath] }
android {
applicationVariants.all { variant ->
variant.assemble.doLast {
signAndInstall.execute()
}
} }
I need that every time before running the tests, run the task with the command for adb
adb shell pm reset-permissions
I tried doing this in my gradle file, it`s compilling, but how does it run?
buildscript {
...
}
android {
...
}
task resetPermission(type: Exec) {
commandLine 'adb', 'shell', 'pm', 'reset-permissions'
}
You'll need to edit your test running configurations, to add the task.
See my (similar) answer here : https://stackoverflow.com/a/35157119/4706693
I am trying to run ndk-build from my build.gradle in an Android Studio 1.0 project on MAC OSX Yosemite.
task ndkBuild(type: Exec) {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
I have specified the ndk-dir in the local.properties file but I am getting this error
A problem occurred starting process 'command 'ndk-build'
If I run the gradle script from command line like this everything successfully builds
./gradlew :myproject:assembleDebug
So for some reason the IDE is unable to call ndk-build. I enabled some debug info in
Android studio and I have the following error
Caused by: java.io.IOException: error=2, No such file or directory
So the IDE cannot find the ndk-build exe however running from the terminal inside the IDE the ndk-build exe can be found.
Thanks
EDIT
You can now retrieve the path like this :
android.ndkDirectory.getAbsolutePath()
I updated the sample below.
As you said in the comments, commandLine requires the path of ndk-build program to make it work. Here is a way to retrieve the ndk path in build.gradle :
// call regular ndk-build script from app directory
task ndkBuild(type: Exec) {
def ndkDir = android.ndkDirectory.getAbsolutePath()
commandLine ndkDir + "/ndk-build", '-C', file('src/main').absolutePath
}
You will have a "cannot infer argument type" lint warning, You can safely ignore this warning. Add // noinspection GroovyAssignabilityCheck to get rid of it.
This was tested with gradle 1.2.3