buildConfigField depending on flavor + buildType
Edit2: The version after 0.14.2 will allow doing this:
applicationVariants.all { variant ->
variant.buildConfigField "int", "VALUE", "1"
}
So you'd be able to do something like this (to match the original question):
applicationVariants.all { variant ->
variant.buildConfigField "String", "WS_API_KEY", variant.productFlavors.get(0).name + '_' + variant.buildType.name
}
Edit: it's not currently possible. The API is missing for this.
Bug: https://code.google.com/p/android/issues/detail?id=67416
Try this:
applicationVariants.all { variant ->
variant.mergedFlavor.buildConfigField "String", "NAME", '"VALUE"'
}
Related videos on Youtube
Comments
-
Maragues over 3 years
I'm trying to define a buildConfigVariable depending on the flavor + buildType. Ideally, this is what I want
productFlavors { strawberry { buildConfigField "String", "WS_API_KEY", name + variant.buildType.name } ... more flavors .. }
name does contain "strawberry", but I don't know if it's possible to access the variant's buildType.
Placed outside the Android closure I do have access to the BuildType and variant, but then I can't invoke
buildConfigField
android.applicationVariants.all { variant -> println "****************************" println "variant: ${variant.name}" println "flavor: ${variant.flavorName}" println "****************************" if (variant.buildType.name == 'release') { if (variant.flavorName == 'strawberry') { buildConfigField "String", "WS_API_KEY", '"strawberry_release"' } else { buildConfigField "String", "WS_API_KEY", '"chocolate_release"' } } else if(variant.buildType.name == 'debug') { if (variant.flavorName == 'strawberry') { buildConfigField "String", "WS_API_KEY", '"strawberry_debug"' } else { buildConfigField "String", "WS_API_KEY", '"chocolate_debug"' } } **************************** variant: strawberryRelease flavor: strawberry **************************** org.gradle.api.internal.MissingMethodException: Could not find method buildConfigField() for arguments [String, WS_API_KEY, "strawberry_release"]
I can easily create a Java factory and return the appropriate
API_KEY
depending on someBuildConfig
constants, but I'd rather keep the code configuration agnostic.-
JJD over 9 years
-
abitcode over 4 yearsThis might be helpful. stackoverflow.com/a/50983681/3333878
-
-
Maragues over 10 yearsThat throws an error MissingMethodException: No signature of method: com.android.builder.DefaultProductFlavor.buildConfigField() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [String, NAME, "VALUE"] Possible solutions: getBuildConfigFields()
-
Maragues over 10 yearsI have a solution of my own tho it's probably fragile, so I'd be happy to see a better one. I'll post it in 5 minutes.
-
Xavier Ducrohet over 10 yearsoh yeah, mergedFlavor is not of the proper type for this. let me look.
-
Maragues over 10 yearsHi Xavier, I've updated my answer with a working version, but I'm curious if there's a better way (there must be, I guess). Thanks for your work!
-
Xavier Ducrohet over 10 yearswell this looks quite ugly, but I guess there's no better way right now. We'll have to improve this. code.google.com/p/android/issues/detail?id=67416
-
Maragues over 10 yearsThanks, I'll keep an eye on that bug.
-
JJD over 9 years@XavierDucrohet Any news here?
-
TWiStErRob over 9 yearsYou can use the public API, no need for
ClassFieldImpl
:com.android.builder.model.ClassField apiKeyField
and then declaredef createClassField = com.android.builder.core.AndroidBuilder.&createClassField
and then instead of callingnew ClassFieldImpl(...)
you can just callcreateClassField(...)
.