androidandroid-gradle-pluginbuild.gradlesource-setsandroid-variants

Replace an XML file for each flavour in android


I am trying to integrate Braze into my application for push notification. Braze need us to create a braze.xml file inside src/main/res/values where we add the API key and other braze related stuff(here is the documentation).

Now I need to differentiate prod and qa environment meaning they will have 2 different API keys.

I was wondering how I could use a different braze.xml for different flavours.

I found this:

sourceSets {
    main {
         java {
            srcDirs = ['src']
         }
    }

    test {
        java {
            srcDirs = ['test']
        }
    }
}

and I was wondering how to use it to replace my braze.xml for different build variants.


Solution

  • You can create multiple source sets for different flavours of your project. By default there is only main/ source set created by studio which contains the common code that will be shared across different variants. For more details on how to create and maintain the source sets check official documentation.

    EDIT - 1

    To elaborate more you can create multiple flavor of your project by using the build.gradle (module level file) and specifying flavors like -

    buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
            debug {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    
            }
            sit {
             initWith debug
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
    }
    

    Once you have created your flavors Gradle Sync your project.

    Now you can create the braze.xml fike for each flavor by right click Values folder >> New >> Values Resource File. Type the name of file example braze.xml and under Source Set select name of flavor for which you want to create this file. As shown in the image name below

    enter image description here

    You can repeat this step until all the flavors are covered then whenever you switch the gradle flavor from the build variant then IDE will automatically start using the designated file for that flavor.