需求
根据 buildType & productFlavor 动态生成 strings 资源。
1 2 3
| buildTypeA + productFlavorX -> <string>buildA_productX</string> buildTypeA + productFlavorY -> <string>buildA_productY</string> buildTypeB + productFlavorY -> <string>buildB_productY</string>
|
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| apply plugin: 'com.android.application' ...
class BuildProperty { String appName
BuildProperty(String appName) { this.appName = appName }
public void setAppName(String appName) { this.appName = appName }
public String getAppName() { return appName } }
android { compileSdkVersion 25 ...
defaultConfig { applicationId com.example.applicationId ... }
productFlavors.whenObjectAdded { flavor -> flavor.extensions.create('buildProperty', BuildProperty, '') }
productFlavors { productA { buildProperty.appName 'productA' } productB { buildProperty.appName 'productB' } }
buildTypes { debug {} release {} }
android.applicationVariants.all { variant -> variant.resValue 'string', 'app_name', variant.buildType.name + variant.productFlavors.get(0).buildProperty.appName } }
|
参考资料:
android-app-name-depends-on-buildtype-and-flavours-gradle
android-studio-gradle-product-flavors-define-custom-properties