問題描述
Flutter:構建 APK 時如何傳遞 Gradle 參數? (Flutter: how do I pass Gradle params when building an APK?)
在使用 Gradle 構建常規 Android 應用程序時,我可以這樣添加參數:
./gradlew assembleRelease ‑Pusername=foo ‑Ppassword=bar
使用 Flutter,我應該調用它來組裝 APK:
flutter build apk
在這種情況下如何將參數傳遞給 Gradle?
PS 我正在嘗試在管道配置中使用 Jenkins 憑據。我不想暴露我的密碼,因此避免使用參數並將其直接放入項目中是不可行的。
參考解法
方法 1:
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.
方法 2:
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
(by Alex Timonin、spacifici、xinthink)