I'm trying to add a custom task to my Android gradle build process.
task doSomethingCustom(type: Exec) {
println 'Do Something Custom'
commandLine 'bash', '-c', 'find ./app/res | grep res_name | xargs -I % custom_script_in_path %'
}
[...]
preBuild.dependsOn doSomethingCustom
I would like to run this custom script "custom_script_in_path" that is in the $PATH of all building machines as part of the build process. This works fine from the command line, and I can open the resulting app and see the modified behavior.
However, when I run this within the Android Studio environment, it errors out giving me the following error:
Execution failed for task ':android_app:doSomethingCustom'.
> Process 'command 'bash'' finished with non-zero exit value 127
UPDATE
Verified that the path seen by AndroidStudio is:
path: /usr/bin:/bin:/usr/sbin:/sbin
I also tried:
commandLine 'sh', '-c', 'command...'
To no success.
I'm working on
MacOSX: 10.11.6
AndroidStudio: 2.1.3
How can I modify the $PATH seen by AndroidStudio?
Related
I have encrypted a file(API key,passwords etc), added in codebase. I need to add a custom task(task encIt) to decrypt the file(will call decrypt.sh to decrypt).This task is added in build.gradle file.
I have tested this by calling "gradle encIt" from terminal and it works fine(decrypting the file as expected), But I need this to be happened every time I build the gradle; how to do that?
This is the task:
task encIt(type:Exec) {
println("WORKING")
commandLine "./decrypt.sh"
println("WORKING")
}
This is the script file :
#!/usr/bin/env sh
echo "inside working"
openssl enc -aes-256-cbc -d -in ../encrypted.key -out key.txt -pass pass:Abcdk5551
echo "inside working"
I have tested this
senario 1:gradle encIt
result : WORKING
inside working
inside working
WORKING
The file is decrypted
senario 1:gradle/gradlew (no arguments)
result : WORKING
WORKING
The file is not getting decrypted
please help!
using gradle version 5.4.1
macbook pro
android studio 3.4.1
editapp/build.gradle,add below code
task encIt(type:Exec) {
println("WORKING")
commandLine "./decrypt.sh"
println("WORKING")
}
afterEvaluate {
assembleDebug.dependsOn encIt
assembleRelease.dependsOn encIt
}
It's worked with:
click run button which on Android Studio
run ./gradlew build
run ./gradlew assemble
run ./gradlew assembleDebug
run ./gradlew assembleRelease
When I run command
ionic cordova run android
build is succeeded but app cannot be run on my android device.
The error log is as:
BUILD SUCCESSFUL in 4m 7s
47 actionable tasks: 45 executed, 2 up-to-date
Built the following apk(s):
ANDROID_HOME=C:\Users\AppData\Local\Android\sdk
JAVA_HOME=C:\Program Files\java\jdk1.8.0_121
No target specified, deploying to device 'UGM0217321003049'.
Skipping build...
Built the following apk(s):
Error: Could not find apk architecture: arm build-type: debug
[ERROR] An error occurred while running subprocess cordova.
cordova run android exited with exit code 1.
Re-running this command with the --verbose flag may provide more information.
Under the directory platforms/android/build/outputs/apk, there is only one folder called debug under which there are two file android-debug.apk and output.json.
apk
|---debug
|---android-debug.apk
|---output.json
I suspect there should be some arm architecture file in apk directory, but I don't know how to generate it.
Can you help me to solve this run android error?
To avoid extended discussions in comments, let me summarise the workaround as below:
Try to run below command to build the apk first.
cordova build android
or
ionic cordova build android
Look for the apk from platforms\android and manually install it.
How to automatically build the android-debug.apk in app folder?
the .json file is auto generated, usually you don't need to touch it.
By default, the apk path is under app/build/outputs/apk, you can copy it to a custom location wherever you prefer, for your case, maybe it is app/ root directory. You can do this with many means, e.g. bash scripts. Below gradle snippets is how I do this task, just copy below code to the end of your app/build.gradle:
def capitalizeFirstLetter(s) { s[0].toUpperCase() + s[1..-1] }
def publish = project.tasks.create("copyApks")
publish.description "Copies apks to custom directory"
android.applicationVariants.all { variant ->
def customPath
variant.outputs.all {
customPath = "$projectDir"
def capitalizedVariantName = capitalizeFirstLetter(variant.name)
def task = project.tasks.create("copy${capitalizedVariantName}Apk", Copy)
task.from(outputFile)
task.into(customPath)
task.dependsOn variant.assemble
publish.dependsOn task
}
}
From your command line, run ./gradlew copyApks or gradlew copyApks on macOS or Windows respectively.
Then you will find your apk is under app\ directory.
i want to make release for android application in react native so when i generate build i got below error message like
Error:Execution failed for task ':app:bundleReleaseJsAndAssets'.
A problem occurred starting process 'command 'node''
see attachment also
any idea how can i solve this ? your all suggestions are appreciable
After spending 2 days I resolved it, Please follow the below steps:-
1) Stop Running Gradle
$ cd RectNatoveProjectName & cd android (Open your project and go to android folder)
$ ./gradlew --stop (Run this command to stop old running Gradle service )
2) Update the android app build gradle
project.ext.react = [
nodeExecutableAndArgs : ["/usr/local/bin/node"]
];
3) Get Node details
$ which node
> (Result will be like "/usr/username/.nvm/versions/node/v10.7.0/bin/node")
4) Link node (very imp step copy the above result in step 3)
- ln -s /usr/username/.nvm/versions/node/v10.7.0/bin/node /usr/local/bin/node
5) If Step - 4 return file exist
- then to go the folder /usr/local/bin/ and delete the "node" file and perform the step -4 again.
6) If Step - 4 run successfully then this node issue will be resolved.
It can have following possible solutions:
1] You need to change the node executable path in your build.gradle(i.e android/app/build.gradle) file as follows:
project.ext.react = [
nodeExecutableAndArgs : ["/usr/local/bin/node"]
];
Or
2] You need to close your Android Studio and need to run below command from project root directory:
cd android
./gradlew assembleRelease
Or
3] You need to simply run "./gradlew --stop" command and then run "./gradlew assembleRelease" command
Add the below following code in app/build.gradle at the end of the file.
afterEvaluate{
android.applicationVariants.all { variant ->
variant.ext.bundleJsAndAssets.enabled = false
}
}
I have a task that is supposed to run a shell script. In Gradle, I have defined the following:
defaultTasks 'renaming'
... some other stuff goes here ...
task renaming(type: Exec) {
commandLine 'sh', 'src/main/bin/rename.sh'
}
I have the shell script under my module in src/main/bin/
However, it is not getting run (for test purposes, the shell creates a directory called "asfasf"). How can I fix this?
As you give sh a relative path, it is resolved against the working directory of the Exec task. Either set the working directory for the Exec task or use file('src/main/bin/rename.sh') as second argument to commandLine.
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