3

Is it possible to create two .properties files for two product flavors in Gradle?

I'm building an Android app with two flavors and I want to have separate properties for them, but have problems with overwritten variable here:

productFlavors {
    flavor1 {
        customProperties = getCustomProperties("flavor1.properties");
    }
    flavor2 {
        customProperties = getCustomProperties("flavor2.properties");
    }
}

My customProperties variable is always get values from flavor2 properties, even I build flavor1.

What am I doing wrong? :)

upd: defaultConfig:

 defaultConfig {
    minSdkVersion 17
    targetSdkVersion 22
    // Enabling multidex support.
    multiDexEnabled true
}
Slonorib
  • 79
  • 2
  • 11

2 Answers2

1

I have use something like this:

flavorDimensions "default"
    productFlavors {
        stand {

            def standPropsFile = file("${project.projectDir}/../stand.properties")
            Properties standProps = new Properties()
            if (standPropsFile.canRead()) {
                standProps.load(new FileInputStream(standPropsFile))
            }
            manifestPlaceholders = standProps
            applicationId = manifestPlaceholders["APP_ID"]
        }

        google {
            def standPropsFile = file("${project.projectDir}/../google.properties")
            Properties standProps = new Properties()
            if (standPropsFile.canRead()) {
                standProps.load(new FileInputStream(standPropsFile))
            }
            manifestPlaceholders = standProps
            applicationId = manifestPlaceholders["APP_ID"]
        }
    }

manifestPlaceholders - is just setter for Map<String, Object> mManifestPlaceholders, so after "manifestPlaceholders = standProps" you can use values right by key how you have write it in .properties file.

Konstantin
  • 159
  • 7
-2

You should also change the build variant and select the right flavor to bring every configuration for the desired flavor into effect.

Alternatively, if you have only a few fields in the properties file and just want different values for each flavor: you could use BuildConfigFields as follows:

buildConfigField 'String', 'FIELD1', '"value1"'
buildConfigField 'String', 'FIELD2', '"value2"'
buildConfigField "String", "FIELD3", "value2"

These can be accessed anywhere throughout the project using the BuildConfig class as follows:

BuildConfig.FIELD1
BuildConfig.FIELD2
BuildConfig.FIELD3
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • I tried to build both flavors, but I always got flavor2.properties – Slonorib Jan 02 '16 at 18:54
  • can you also share your default config? – Viral Patel Jan 02 '16 at 18:56
  • 1
    can you also check the alternate mechanism (updated in my answer) to have different values per product flavors? You could avoid the complexity of switching and maintaining the separate files per flavor. – Viral Patel Jan 02 '16 at 19:09
  • sure, do let me know if it servers your purpose. – Viral Patel Jan 02 '16 at 19:22
  • finally, I found solution [here](http://stackoverflow.com/questions/28232261/android-studio-gradle-product-flavors-define-custom-properties/28279105#28279105), but your answer is right too. Accepted it :) – Slonorib Jan 03 '16 at 06:10