Flutter: how do I pass Gradle params when building an APK?

Viewed 2643

When building a regular Android app with Gradle, I can add params as such:

./gradlew assembleRelease -Pusername=foo -Ppassword=bar

With Flutter, this is what I'm supposed to call in order to assemble an APK:

flutter build apk

How do I pass the params to Gradle in this case?

P.S. I'm trying to use Jenkins credentials in a pipeline configuration. I do not want to expose my password, so avoiding using the arguments and putting it directly into the project is not an option.

2 Answers

You can pass variables via the environment, I use this method with Jenkins. The job can be configured to pass the credentials via environment variables.

In your build.gradle (where needed):

username = System.getenv('SECRET_USERNAME')
password = System.getenv('SECRET_PASSWORD')

Please notice that System.getenv(...) returns null if the variable is not defined.

In your development environment you should export the variables:

$ export SECRET_USERNAME="my secret username"
$ export SECRET_PASSWORD="my super secret password"

Please, I do not know which IDE are you using, but both IntelliJ and AndroidStudio do support declaring environment variables.

You can set project properties through environment variables like ORG_GRADLE_PROJECT_foo=bar, so that you don't have to modify the gradle scripts, for example:

export ORG_GRADLE_PROJECT_username=foo 
export ORG_GRADLE_PROJECT_password=bar
flutter build apk

Docs: Project properties

Related