0

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
}
Eloy_007
  • 226
  • 1
  • 2
  • 9
  • Probably not with that syntax, but there should be some way in the DSL to call a method that adds a flavor based upon a variable. Or, consider your `build.gradle` to be output of something else that code-generates the file, so your variables are in your code generator, not `build.gradle` itself. – CommonsWare Dec 09 '16 at 20:48
  • @Eloy_007 would you mind pasting what you have for a `build.gradle` file? While flavorNames aren't really dynamic*, the [buildVariant](https://developer.android.com/studio/build/build-variants.html) can be constructed from multiple flavor names. – abest Dec 09 '16 at 23:23
  • Ah nevermind I ended up using bash scripts that dynamically replace all these names for me. It kind of sucks that Gradle plugin doesn't support that (at least for what I could tell) – Eloy_007 Dec 10 '16 at 00:23
  • Eloy_007 checkout the provided answer... i think this may get you what you're looking for. – abest Dec 10 '16 at 01:06

1 Answers1

2

@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!

Community
  • 1
  • 1
abest
  • 724
  • 6
  • 16