Alexey Nikitin
Alexey Nikitin

Reputation: 654

Unique buildConfigField for all flavors with dimensions

I has two flavor dimensions: brand and version, my flavors config looks like:

flavorDimensions 'brand', 'version'

Brand1 { 
    dimension 'brand'
    ...
}

Brand2 {
    dimension 'brand'
    ...
}

Version1 {
    dimension 'version'
    ...
}

Version2 {
    dimension 'version'
    ...
}

And I want to have four unique buildConfigField-s (for example HockeyAppId) for every configurations:

How I can do this?

Upvotes: 1

Views: 1070

Answers (2)

Alexey Nikitin
Alexey Nikitin

Reputation: 654

I wrote my own plugin for that purpose: https://github.com/nikialeksey/porflavor, and now I can define such fields:

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"
  }
  ...
}

Upvotes: 3

Sam
Sam

Reputation: 5392

so this is pretty straight forward. You can modify per flavor or release type without any crazy effort.

If you are attempting to reuse a flavor in multiple dimensions, that is not their desired functionality. a flavor is meant to be a built compiled packaged version of the app. It's not really meant to be a generic set of parameters. So you will need a flavor for every variance such as

flavor1 -> in dimension 1

flavor1Dimension2 -> in dimension 2

flavor2 -> in dimension 1

flavor2Dimension2 -> in dimension 2 etc..

Here I will give an example of using dynamic

  • resources
  • buildconfig
  • manifest placeholder
  • application id

Of course there is more you can do but this should get you going with your request.

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
    }
}

Happy Coding!

Upvotes: 0

Related Questions