4

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.

hushed_voice
  • 3,161
  • 3
  • 34
  • 66
  • Have a look at the docs: https://developer.android.com/studio/build/build-variants. It covers the subject quite well. – gioravered Feb 02 '22 at 11:29

1 Answers1

3

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.

Taranmeet Singh
  • 1,199
  • 1
  • 11
  • 14
  • Either give a full answer or post a comment instead. Directing to docs can be done in a comment.. – gioravered Feb 02 '22 at 12:19
  • This actually creates a directory(values) for each flavour right?? Can I do it just for 1 file?? Because everything else will be same for me – hushed_voice Feb 02 '22 at 12:23
  • @hushed_voice all the common files will come under the main/ source set only the single file that you want diff for each flavour will be provided by respective folder. – Taranmeet Singh Feb 02 '22 at 13:26
  • I created the folders like this `app/src/qa/res/values/braze.xml` and `app/src/prod/res/values/braze.xml` do I have to add something to the gradle.build. Also, do I have to add the file in main folder??? – hushed_voice Feb 02 '22 at 17:09
  • Sorry if I am being naive. It's not clear for me from the documentation. I created the `sourcesets` as mentioned in the documentation – hushed_voice Feb 02 '22 at 17:15
  • @hushed_voice I hope the above steps will help you. – Taranmeet Singh Feb 09 '22 at 05:14