androidamazon-web-servicesandroid-gradle-pluginaws-amplifysource-sets

sourceSets with awsconfiguration.json


I am using AWS Amplify and have two environments that I am switching between, therefore I have two awsconfiguration.json files, but need a way to switch between the two at runtime. I've heard you can use sourceSets, but can't figure out how. I tried the following, but with no success. I believe I have to setup a productFlavor and/or buildType?

sourceSets {
    prod {
        res.srcDirs = ['res-prod']
    }

    dev {
        res.srcDirs = ['res-dev']
    }
}

Solution

  • Put your awsconfiguration.json under assets directory per each product flavor, see the directory structure as shown below:

    enter image description here

    For how to configure product flavor and build types, please refer to: https://developer.android.com/studio/build/build-variants for more details.

    Then inside your code refer to your assets using code like below:

    AssetManager am = context.getAssets();
    InputStream is = am.open("awsconfiguration.json");
    

    Reference:

    1. Android get file from asset/raw
    2. https://developer.android.com/reference/android/content/res/AssetManager