Reputation: 1033
I am trying to migrate to Android Gradle Plugin 7.
I have the following code that needs migration:
applicationVariants.all { variant ->
// do some processing to obtain string apiKeyValue
variant.resValue "string", "api_key", apiKeyValue
}
I have looked at the migration blog post here android developers blog but still no clear reference to how to create dynamic
resValue
Any thoughts?
I also tried to use something similar to
androidComponents {
onVariants(selector().all(), { variant ->
// do some processing to obtain string apiKeyValue
addResValue("api_key", "string", apiKeyValue, "Value from variant")
})
}
But no luck as
addResValue
method is not found.
Upvotes: 2
Views: 699
Reputation: 194
For groovy and AGP 7.2.1 old way still works. Only keys for resValues now have type prefix ie "string/app_name"
. instead of "app_name"
.
android{
applicationVariants.all{ variant ->
def strRes = variant.mergedFlavor.resValues.get("string/app_name")
if(strRes != null){
println( "string/app_name = " + strRes.getValue())
variant.resValue 'string', 'app_name', "${strRes}New"
}
}
}
Upvotes: 2
Reputation: 1033
After a bit of searching and trial and error, the following code worked for me:
androidComponents {
onVariants(selector().all(), { variant ->
// do some processing to obtain string apiKeyValue
variant.resValues.put(variant.makeResValueKey("string", "api_key"), new ResValue(apiKeyValue, "Variant Name"))
})
}
The above code is a snippet from my build.gradle file. I took inspiration from here gradle-recipes
Upvotes: 2