Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Gradle Command Line -P Variables in Android Build

First of all I am new to gradle so this might be a stupid question but I was not able to find a solution. I am trying to build a signed Android .apk file in the terminal and I want to use the command line in order to pass some arguments:

gradle assembleRelease -PkeyPw='secret123' -PstorePw='secret123' -PkeyAlias='My-Testkey' -PkeyLocation='/home/someUser/test-key.keystore'

Now I want to use these Variables inside the build.gradle file:

signingConfigs {
   release {
      storeFile $keyLocation
      storePassword $storePw
      keyAlias $keyAlias
      keyPassword $keyPw
   }
}

But they are null (Probably because it does not make any sense at all, but I did not find out how to do this).

Thanks for your help!

EDIT

I run the gradle build from java using the command

new ProcessBuilder().inheritIO.command(cmd).start 

and get the following error:

FAILURE: Build failed with an exception. 
* Where: 
Build file 'somePlace/app/build.gradle' line: 16 
* What went wrong: 
A problem occurred evaluating project ':app'. 
> Could not find property 'keyLocation' on project ':app'. 
* Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
like image 696
p0wl90 Avatar asked Apr 16 '15 15:04

p0wl90


People also ask

How do I get environment variables in build Gradle?

Enabling Environment Variables in Gradle Android developers can use environment variables using Gradle's module-level build configuration. This module-level Gradle configuration file lets you specify build settings for that application module.

How do I declare a variable in Gradle?

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language. Local variables are declared with the val keyword.

Where is Gradle scripts in Android Studio?

Located in the project/module directory of the project this Gradle script is where all the dependencies are defined and where the SDK versions are declared. This script has many functions in the project which include additional build types and override settings in the main/app manifest or top-level build.


1 Answers

All properties passed via -P switch are accessible via project variable. So it will be:

signingConfigs {
   release {
      storeFile project.keyLocation
      storePassword project.storePw
      keyAlias project.keyAlias
      keyPassword project.keyPw
   }
}

It's good idea to check if project has specified property before using it (to avoid problems):

signingConfigs {
   release {
      storeFile project.hasProperty('keyLocation') ? project.keyLocation : 'default'
      storePassword project.hasProperty('storePw') ? project.storePw : 'default'
      keyAlias project.hasProperty('keyAlias') ? project.keyAlias : 'default'
      keyPassword project.hasProperty('keyPw') ? project.keyPw : 'default'
   }
} 
like image 125
Opal Avatar answered Sep 19 '22 14:09

Opal