android - Declare variable into build.gradle that match the applicationId with flavors suffix
问题描述
I don't figure out how can I set a resource value (resValue
) in my build.gradle
, depending on the build variant selection.
Here a bit of explanation.
I'm working with the Skype For Business SDK (hereafter referred to as Sfb) and during is implementation, it ask me to add a resource value named ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE
.
So I was looking in their application example (available here) and found that in build.gradle
they have added as follow the corresponding value :
android {
...
defaultConfig {
applicationId "com.microsoft.office.sfb.sfbdemo"
...
resValue ("string", "ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE", "${applicationId}"
}
...
}
This value then used in a SfbSDK class, checking if it match the application package name.
And here is my trouble, I work with different flavorDimensions
as describe in my build.gradle
below.
apply plugin: 'com.android.application'
...
android {
...
defaultConfig {
applicationId "com.tsp.test"
...
resValue ("string", "ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE", "${applicationId}"
}
...
flavorDimensions("customer", "version")
productFlavors {
a {
dimension "customer"
applicationIdSuffix ".a"
}
b {
dimension "customer"
applicationIdSuffix ".b"
}
alpha {
dimension "version"
applicationIdSuffix ".alpha"
}
beta {
dimension "version"
applicationIdSuffix ".beta"
}
release {
dimension "version"
applicationIdSuffix ".release"
}
}
}
...
Depending on my build variant selection, this will generate me 6 different APK :
- com.tsp.test.a.alpha ; com.tsp.test.a.beta ; com.tsp.test.a.release
- com.tsp.test.b.alpha ; com.tsp.test.b.beta ; com.tsp.test.b.release
So when the match checking is does, my application crash with the message error
Caused by: java.lang.RuntimeException: ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE string not set to applicationId
at com.microsoft.office.sfb.appsdk.Application.initialize(Application.java:110)
at com.microsoft.office.sfb.appsdk.Application.getInstance(Application.java:144)
at com.tsp.test.RootActivity.onCreate(RootActivity.java:89)
Of course, because com.tsp.test
doesn't match com.tsp.test.a.alpha
(or any other APK).
How can I achieve a dynamic resValue
depending on the build variant selected that match the right application package name ?
EDIT :
To explain a bit more. First I choose the Build Variants as follow :
- Customer : A
- Version : Alpha
Then, in my RootActivity#onCreate()
(my launcher activity), I start to configure the SfbSDK with an application instance depending on the SfbSDK :
this.mApplication = com.microsoft.office.sfb.appsdk.Application.getInstance(this.getApplication().getApplicationContext());
Somewhere in getInstance()
method, the SfbSDK do an equals()
between the context.getPackageName()
and context.getString(string.ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE);
So, for debug, in my RootActivity#onCreate()
I just wrote this two lines
String whatIWant = this.getPackageName(); // give me *com.tsp.test.a.alpha*
String whatIGet = this.getString(R.string.ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE); // give me *com.tsp.test*
This doesn't match ! So in SfbSDK the condition wasn't passed.
解决方案
好吧,感谢拉德什和他提供给我的这个链接,我找到了我的解决方案。
我resValue
从defaultConfig
块中删除,然后在android
插件任务中添加了一个新块,resValue
为每个 变体创建。
apply plugin: 'com.android.application'
...
android {
...
defaultConfig {
applicationId "com.tsp.test"
...
}
...
flavorDimensions("customer", "version")
productFlavors {
a {
dimension "customer"
applicationIdSuffix ".a"
}
b {
dimension "customer"
applicationIdSuffix ".b"
}
alpha {
dimension "version"
applicationIdSuffix ".alpha"
}
beta {
dimension "version"
applicationIdSuffix ".beta"
}
release {
dimension "version"
applicationIdSuffix ".release"
}
}
...
applicationVariants.all { variant ->
variant.resValue "string", "ENTERPRISE_AUTHENTICATOR_ACCOUNT_TYPE", "\"${applicationId}\""
}
}
...
这将正确生成resValue
带有所选变体的包名称的 。
对于变体客户 A和版本 Alpha,我得到resValue = com.tsp.test.a.alpha
.
推荐阅读
- python - 填写时Tkinter检查按钮干扰
- amazon-web-services - API 网关/NLB/ECS 延迟
- python - on_raw_reaction_add(payload) 故障排除
- c# - Net Core 2.2 MVC WebApi 中的 JWT“自我”身份验证
- java - 如何用通用方法替换这些 for 循环
- c++ - C++ 从递归函数中打印出奇怪的字符
- python - Python:为了测试目的而断开 Socket 管道连接的最简单方法?
- python - 如何在python中抓取隐藏类型的html
- three.js - 在 Three.js 中为 OrbitContorls 分配多个键
- mysql - 计算一列的总和并按具有相同值的列分组