Search code examples
androidjenkinsgradleandroid-build-flavorsandroid-flavors

Android app flavor name as a variable instead of static string


I have been trying for the past hour to set some variable name like FLAVOR_NAME in gradle.properties and then use that in build.gradle to have a sort of a dynamic flavor name. No success yet. Is it even possible to have a dynamic flavor name? I mean something like:

$FLAVOR_NAME{
     versionName FLAVOR_VERSION
     versionCode FLAVOR_CODE
}

Solution

  • @Eloy_007,

    While your question is similar to this one, there are some nuances in your question that I think the following will answer.

    First off, there is a great article that you should read where the author(Coby Plain) goes about providing a better framework for working with flavors. Also, the android10/Android-CleanArchitecture repo has a really nice structure for importing gradle properties. Look in buildsystem/dependencies.gradle and presentation/build.gradle for guidance.

    That said, here's a quick sample of what importing properties looks like when working with semi-dynamic/imported flavors:

    In the gradle file, we can reference an external file that has all of our related properties defined together and maybe use some simple groovy magic to join strings etc.

    build.gradle:

    apply from: "properties.gradle" // load in properties from some other file
    
    // ... plugins, etc.
    
    android {
        // some other sections
        productFlavors {
            dev {
                // you may want to keep some non-imported flavors.
                // maybe you're working with flavor dimension here
            }
    
            // ... maybe more standard flavors
    
            flavors.each {name, flavor -> 
                def wrapEscaped = { s -> "\"${s}\""} // simple function to wrap strings
                "$name" {
                    applicationId flavor.applicationId
                    versionName "${some_version_number}"
    
                    buildConfigField "String", "MAJOR_KEY", wrapEscaped(flavor.majorKey)
                    buildConfigField "String", "ANOTHER_ONE", wrapEscaped(flavor.anotherOne)
                    buildConfigField "int", "PILLOW_COUNT", flavor.pillowCount
    
                    // ... additional configs
                }
            }
        }
    }
    

    Then in the referenced properties.gradle you define flavors as follows:

    ext {
      config = [
          baseId : "io.wethebest.mobile"
      ]
    
      flavors = [
          free    : [
              applicationId: config.baseId + ".free",
              majorKey     : "COCOA BUTTER",
              anotherOne   : "ANOTHER ONE",
              pillowCount  : 1,
          ]
          simple : [
              applicationId: config.baseId + ".simple",
              majorKey     : "A HAMMOCK",
              anotherOne   : "ANOTHER ONE",
              pillowCount  : 10,
          ],
          paid   : [
              applicationId: config.baseId,
              majorKey     : "A JETSKI",
              anotherOne   : "ANOTHER ONE",
              pillowCount  : 100000000,
          ]
      ]
    }
    

    At a minimum, this allows for you define your flavors outside of your build.gradle file so that you can minimze the amount of cruft you have to sift through. Plus, major key, it keeps the revision history for the build.gradle file clean.

    HTH, Good Luck!