具有维度的所有口味的唯一 buildConfigField

Unique buildConfigField for all flavors with dimensions

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

flavorDimensions 'brand', 'version'

Brand1 { 
    dimension 'brand'
    ...
}

Brand2 {
    dimension 'brand'
    ...
}

Version1 {
    dimension 'version'
    ...
}

Version2 {
    dimension 'version'
    ...
}

而且我希望每个配置都有四个唯一的 buildConfigField-s(例如 HockeyAppId):

我该怎么做?

所以这很简单。您可以毫不费力地修改每个口味或版本类型。

如果您试图在多个维度上重复使用一种风味,那不是他们想要的功能。风味意味着是应用程序的构建编译打包版本。它并不是真正意义上的一组通用参数。因此,您需要为每个方差添加一种风味,例如

flavor1 -> in dimension 1

flavor1Dimension2 -> in dimension 2

flavor2 -> in dimension 1

flavor2Dimension2 -> in dimension 2 etc..

这里我举一个使用动态的例子

  • 资源
  • 构建配置
  • 清单占位符
  • 应用程序 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
    }
}

编码愉快!

我为此编写了自己的插件: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"
  }
  ...
}