Why gradle commandLine is not working?

task runAndroid19(type: Exec) {
 // linux
  commandLine "export ANDROID_HOME=......."
}

even tried …

task runAndroid19(type: Exec) {
 // linux
  commandLine "export", "ANDROID_HOME=......."
}

got error :-

A problem occurred starting process 'command ‘export’ ’

I’m not familiar with the Android plugin ecosystem, but I don’t think you can use the “commandLine” function to set environment variables. The “export” function is an internal operation in the Bash shell. I think it’s likely that you can only execute applications from “commandLine”, not set environment variables.

@David Karr

Noted, Thanks,

Regards, Taymindis

If you need to do things like set environment variables before executing some other command you’ll probably want to put that all in a single shell script, and then have the ‘Exec’ task execute the shell script. That way you are guaranteed to have the environment variables available from the same shell that runs the final command.

@Mark Viera,

Hi Mark, Yea I have tried that… But it does not really impress me :frowning:

task runScript(type: Exec) {
    commandLine "./android-19.sh"
}
  task testAndroidVer19(dependsOn: runScript) << {
    println "${-> System.getenv("ANDROID_HOME")}"
}

my shell script file :-

echo "exporting android version 19"
ANDROID_HOME=/home/taymindis/androidTools/19.1.0/android-sdks
export ANDROID_HOME

It executed the file, but it wont export env. However, I will set ext prop then.

I don’t think that will work since the environment variable will only set in the shell which executed your script. It is not possible to modify environment variables of the currently running Gradle process. Can you perhaps explain a bit more about what you are trying to accomplish by doing this?

I would emphasize Mark’s last statement. It’s quite common for people to go down rabbit holes trying to do things that don’t make sense. If you explain what you actually need to do, instead of what you think are the steps you need to follow, we might be able to help you.

Concerning this issue with exporting of environment variables, note that the way that Unix/Linux exports environment variable settings is different from how Windows/DOS does this, which is perhaps why you’re confused. Your example looks like you’re on Unix/Linux. In Windows, an environment variable set in a “.bat” file is GLOBAL to the environment. If you print the value of that variable after running the bat file, it will have the value set in the bat file. However, on Unix/Linux, when you execute an application or shell script, one of the first things that happens is that the set of environment variables is cloned (essentially), so that if that application sets one of those environment variables, it’s essentially private data. When that application finishes, the parent shell doesn’t have the settings made in that forked shell.

@mark @david

my Purpose;- I’m trying to build android apps by using android plugin, but the plugin required ANDROID_HOME environment to run the application, currently I’m attempting to run 2 different version (19,21) to ensure both version has no issue with my implementation.

my design:- I expected my team only care about how they implement the android apps and debugging the android apps by using gradle build tools, so I’m plannin to have zero configuration for them to implement(just check out and run gradle wrapper)

Issue:- Everyone has default ANDROID_HOME, but we need to debug the apps in different version, temp ANDROID_HOME is needed…what we are doing is

Export ANDROID_HOME=.......
ENTER
./gradlew clean installDebug.... ENTER
adb start.....
ENTER

I just want to wrap it in 1 task, runAndroid19, runAndroid21, to make comfortable for team

Regards, Taymindis

I don’t think you have to have completely separate SDK install locations to accomplish this. Couldn’t you simply modify the compile SDK version in your build script to accomplish the same thing?

@Mark

Did you mean build tool version on build script? Yes, if I define version 21 in my build script, it will look for build tools env whether is version 21, if I define version 19 in build script, it will look for build tools env whether 19, if build tool version is unmatch env version, it wil throw fail revision…

I think I just set the env into gradle wrapper script and use paramter to versioning env, it one time setup for all developer:)

./gradlew 19 assembleDebug
./gradlew 21 assembleDebug

Thanks,

Taymindis,

Gradle Fans