[英]Android Gradle Flavor values dependent on other flavor dimensions
So I've been working on a bit of a white label style project for a while, and I've run into a bit of an issue with extensibility.所以我一直在做一些白标风格的项目,但我遇到了一些可扩展性问题。 Specifically, the way the project works, I need to be able to support multiple backend environments, each with unique credentials, on a per-client basis.
具体来说,项目的工作方式,我需要能够支持多个后端环境,每个后端环境都有唯一的凭据,在每个客户端的基础上。
I've baked this behavior into the gradle using two flavor dimensions, one for the client and one for the backend, but I'm having a bit of trouble in getting them to gel together as seamlessly as I'd like.我已经使用两种风味维度将此行为烘焙到 gradle 中,一种用于客户端,一种用于后端,但是我在让它们像我想要的那样无缝地融合在一起时遇到了一些麻烦。
Here's an example of the gradle setup:这是 gradle 设置的示例:
productFlavors {
client1 {
applicationId "com.app.client1"
dimension "customer"
resValue "string", "app_name", "Client 1"
}
client2{
applicationId "com.app.client2"
dimension "customer"
resValue "string", "app_name", "Client 2"
}
dev {
ext {
packageExtension = ".DEV"
}
dimension "environment"
resValue "string", "url", "https://dev.hostname.com/"
}
uat {
ext {
packageExtension = ".UAT"
}
dimension "environment"
resValue "string", "url", "https://uat.hostname.com/"
}
stage {
ext {
packageExtension = ".STAGE"
}
dimension "environment"
resValue "string", "url", "https://stg.hostname.com/"
}
prod {
ext {
packageExtension = ""
}
dimension "environment"
resValue "string", "url", "https://hostname.com/"
}
}
This works great for granting access to the right environment and unique code for each client, but it means that I can't include the client ID in the gradle.这对于为每个客户端授予对正确环境和唯一代码的访问权限非常有用,但这意味着我不能在 gradle 中包含客户端 ID。 I have to stick it in a resource file out in the client1Dev directory and the like in order to get the right value at run time.
我必须将它粘贴在 client1Dev 目录等的资源文件中,以便在运行时获得正确的值。
My question is, is there a way to move the client ID into gradle and get the right value based on the combination of customer and environment flavors?我的问题是,有没有办法将客户端 ID 移动到 gradle 中并根据客户和环境风味的组合获得正确的值?
For example, what I would like to do is something like:例如,我想做的是:
client1 {
applicationId "com.app.client1"
dimension "customer"
resValue "string", "app_name", "Client 1"
dev{
resValue "string", "clientId", "1032145d8eefa00aff0098b08c9d"
}
uat{
resValue "string", "clientId", "8654684561584798531985964/56"
}
}
client2{
applicationId "com.app.client2"
dimension "customer"
resValue "string", "app_name", "Client 2"
dev{
resValue "string", "clientId", "89612ad8967a00aff0098b08c08e"
}
uat{
resValue "string", "clientId", "8612ad890981237409d0ab08c08f"
}
}
Something along these lines that would allow me to store my client ID in the gradle file along with the other backend setup info.沿着这些思路,我可以将我的客户端 ID 与其他后端设置信息一起存储在 gradle 文件中。 I can't find anything about this kind of thing though, and I'm hoping someone on here can shed some light on how to do it, or at least tell me it's impossible.
不过,我找不到关于这种事情的任何信息,我希望这里的某个人可以对如何做到这一点有所了解,或者至少告诉我这是不可能的。
Thanks!谢谢!
You could always use separate source trees for the multi-flavor combination and store the Strings directly in the correct resource (since that's essentially what resValue "string" "{ID}" "{VALUE}"
is doing anyway).您始终可以为多口味组合使用单独的源树,并将字符串直接存储在正确的资源中(因为这本质上是
resValue "string" "{ID}" "{VALUE}"
无论如何)。
src/
main/
java/
res/
values/
strings_ids.xml
<string name="clientId">override_me</string>
client1Dev/
client1Uat/
client2Dev/
res/
values/
strings_ids.xml
<string name="clientId">89612ad8967a00aff0098b08c08e</string>
client2Uat/
res/
values/
strings_ids.xml
<string name="clientId">8612ad890981237409d0ab08c08f</string>
Or, if you really want to keep in inside the build.gradle file, you could script it with something (less elegant) along the lines of:或者,如果您真的想保留在 build.gradle 文件中,您可以使用以下内容(不太优雅)编写脚本:
android.applicationVariants.all { variant ->
if (variant.getFlavorName().equalsIgnoreCase("client1Dev")) {
variant.resValue "string", "clientId", "1032145d8eefa00aff0098b08c9d"
}
else if (variant.getFlavorName().equalsIgnoreCase("client1Uat")) {
variant.resValue "string", "clientId", "8654684561584798531985964/56"
}
}
But you can see the latter will get nasty, pretty quickly但是你可以看到后者很快就会变得讨厌
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.