【发布时间】:2020-01-24 18:34:35
【问题描述】:
目标
使用构建类型为不同环境构建 APKs
方法
我正在使用这个post 在没有 android studio 的情况下构建我的 apk。除了我在 gradle 中使用环境变量时,一切正常:
在构建之前,我设置了环境变量
export MY_API_TOKEN="ABCDEFGH"
然后在我的 build.gradle 中:
buildTypes {
debug {
buildConfigField("String", "MY_API_TOKEN", System.getenv('MY_API_TOKEN'))
}
}
当我执行:gradle assemble 我得到这个错误:
/home/apps/app/src/main/java/com/my/package/controller/api/MyAwesomeCode.java:64: error: cannot find symbol
BuildConfig.MY_API_TOKEN;
^
symbol: variable BuildConfig
location: class RestAPI
/home/apps/app/build/generated/source/buildConfig/debug/com/my/package/BuildConfig.java:14: error: cannot find symbol
public static final String MY_API_TOKEN = ABCDEFGH;
^
symbol: variable ABCDEFGH
location: class BuildConfig
我尝试了几种组合,我得到了同样的错误:
- BuildConfig not getting created correctly (Gradle Android)
- BuildConfig variable. Error: cannot find symbol
- https://medium.com/@rafamatias/gradle-android-build-variables-done-right-d0c0e296ee93
- Gradle : how to use BuildConfig in an android-library with a flag that gets set in an app
我还用变量验证了相同的行为
def MY_API_TOKEN_VAR = "ABCDEFGH"
debug {
buildConfigField "String", "MY_API_TOKEN", MY_API_TOKEN_VAR
}
错误:
BuildConfig.java:14: error: cannot find symbol
public static final String MY_API_TOKEN = ABCDEFGH;
^
symbol: variable ABCDEFGH
location: class BuildConfig
问题
buildTypes 仅适用于 build.gradle 中的硬编码值?
【问题讨论】: