唯一的buildConfigField,适用于所有带有尺寸的样式

时间:2019-05-08 14:49:14

标签: android gradle android-productflavors android-flavors android-flavordimension

我有两个风味维度:brandversion,我的风味配置如下:

flavorDimensions 'brand', 'version'

Brand1 { 
    dimension 'brand'
    ...
}

Brand2 {
    dimension 'brand'
    ...
}

Version1 {
    dimension 'version'
    ...
}

Version2 {
    dimension 'version'
    ...
}

我想为每个配置使用四个唯一的buildConfigField-s(例如HockeyAppId):

  • Brand1Version1
  • Brand1Version2
  • Brand2Version1
  • Brand2Version2

我该怎么做?

2 个答案:

答案 0 :(得分:0)

所以这很简单。您可以根据口味或发布类型进行修改,而无需付出任何努力。

如果您试图在多个维度上重用某个风味,那不是其所需的功能。风味是指该应用的内置编译打包版本。并不是真正要作为一组通用参数。因此,您需要为每种差异提供一种风味,例如

  尺寸为1的

flavor1->

     

flavor1Dimension2->尺寸2

     维度1中的

flavor2->

     

flavor2Dimension2->尺寸2等。

在这里,我将举例说明使用动态

  • 资源
  • buildconfig
  • 清单占位符
  • 应用程序ID

当然,您可以做的更多,但这应该可以帮助您完成请求。

flavorDimensions 'default', 'secondary'

productFlavors {
    a35Demo {
        dimension 'default'
        applicationId "com.appstudio35.yourappstudio.demo"
        buildConfigField "int", "BUSINESS_ID", "1"
        resValue "string", "app_name", "App Studio 35"
        buildConfigField "String", "NOTIFICATION_ICON", '"ic_launcher"'
        manifestPlaceholders = [iconPath:"@mipmap/ic_launcher", roundIconPath:"@mipmap/ic_launcher_round"]
    }
    smallville {
        dimension 'secondary'
        applicationId "com.appstudio35.yourappstudio.smallville"
        buildConfigField "int", "BUSINESS_ID", "22"
        resValue "string", "app_name", "Smallville"
        buildConfigField "String", "NOTIFICATION_ICON", '"ic_launcher_smallville"'
        manifestPlaceholders = [iconPath:"@mipmap/ic_launcher_smallville", roundIconPath:"@mipmap/ic_launcher_round_smallville"]
    }
}

buildTypes {
    debug {
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "SERVER_URL", '"https://api.dev.myurl.com"'
        shrinkResources false //remove unused resources per flavor
        minifyEnabled false
    }
    release {
        buildConfigField "String", "SERVER_URL", '"https://api.prod.myurl.com"'

        shrinkResources true //remove unused resources per flavor
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        //production builds
        productFlavors.a35Demo.signingConfig signingConfigs.releaseA35YourAppStudio
        productFlavors.smallville.signingConfig signingConfigs.releaseA35YourAppStudio
    }
}

快乐编码!

答案 1 :(得分:0)

为此,我编写了自己的插件:https://github.com/nikialeksey/porflavor,现在可以定义以下字段:

flavorDimensions 'brand', 'version'

productFlavors {
  Brand1 { 
    dimension 'brand'
    ...
  }
  Brand2 {
    dimension 'brand'
    ...
  }

  Version1 {
    dimension 'version'
    ...
  }
  Version2 {
    dimension 'version'
    ...
  }
}

apply plugin: 'com.nikialeksey.porflavor'
porflavor {
  Brand1Version1 {
    buildConfigField "boolean", "fooFeatureEnabled", "false"
  }
  Brand2Version2 {
    buildConfigField "boolean", "fooFeatureEnabled", "true"
  }
  ...
}