How to setup different configuration(awsconfiguration.json) with AWSMobileClient for debug and release build types

Viewed 5526

I want to have different configuration for debug and release builds. All the configuration is stored inside awsconfiguration.json, for example I have two different config files how can I set which file should be used.

When using AWSMobileClient.getInstance() it gets default configuration from file awsconfiguration.json

Configuration file example:

{
  "Version": "1.0",
  "CredentialsProvider": {
    "CognitoIdentity": {
      "Default": {
        "PoolId": "DIFFERENT_VALUES",
        "Region": "DIFFERENT_VALUES"
      }
    }
  },
  "IdentityManager": {
    "Default": {}
  },
  "CognitoUserPool": {
    "Default": {
      "AppClientSecret": "DIFFERENT_VALUES",
      "AppClientId": "DIFFERENT_VALUES",
      "PoolId": "DIFFERENT_VALUES",
      "Region": "DIFFERENT_VALUES"
    }
  }
}

Update There is option to use different awsconfiguration.json by puting different files in main\res\raw and release\res\raw, for example by following this answer and it works. But I'm wondering whether there is an option to do it programmatically.

4 Answers

This can also be acheived by setting the configuration value in AWSConfiguration and then initializing the AWSMobileClient.

    AWSConfiguration awsConfiguration = new AWSConfiguration(context);
    awsConfiguration.setConfiguration("Stage"); // BuildConfig can be used here.

    AWSMobileClient.getInstance().initialize(context, awsConfiguration,  new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails userStateDetails) {
        }

        @Override
        public void onError(Exception e) {
        }
    });

And the awsconfiguration.json file can be updated as below

{
 "Version": "1.0",
 "CredentialsProvider": {
    "CognitoIdentity": {
        "Default": {
            "PoolId": "DIFFERENT_VALUES",
            "Region": "DIFFERENT_VALUES"
        },
        "Stage": {
            "PoolId": "STAGE_VALUES",
            "Region": "STAGE_VALUES"
        }
    }
 },
 "IdentityManager": {
    "Default": {},
    "Stage": {}
 },
 "CognitoUserPool": {
    "Default": {
        "AppClientSecret": "DIFFERENT_VALUES",
        "AppClientId": "DIFFERENT_VALUES",
        "PoolId": "DIFFERENT_VALUES",
        "Region": "DIFFERENT_VALUES"
    },
    "Stage": {
        "AppClientSecret": "STAGE_VALUES",
        "AppClientId": "STAGE_VALUES",
        "PoolId": "STAGE_VALUES",
        "Region": "STAGE_VALUES"
    }
 }
}

I've been trying to achieve something similar; selecting an AWS configuration at runtime based on a selected profile. I got it partially working by hacking the AWS SDK but then stumbled across release notes for AWS SDK version 2.11.0. Quoting:

Added the option of passing the configuration as an in-memory object (i.e. [String: Any]/NSDictionary) instead of the default awsconfiguration.json through the new API

I've also found it documented(!) in the amplify getting started guide here.

So since 9th September 2019 it IS possible to select an AWS configuration at runtime.


Edit: Just noticed that this question is for Android rather than iOS. I'm not an Android developer but a quick searched revealed something similar in AWS Android SDK release 2.13.6 (7th June 2019). Quoting the release notes:

Add AWSConfiguration(JSONObject) constructor to construct a AWSConfiguration object from the configuration passed via a JSONObject

... which looks promising.

This can be done with source sets; eg. directories main & debug or directories debug & release, where res/raw or assets are not being processed by AAPT2. Adding credentials alike that is only suggested for internal use, because they can be easily extracted from the package.

Abhishek's answer is my favorite of these. But if you're using Amplify (as I am), while it's possible to put multiple configurations into a single file, I've not found a way of selecting between them.

So, although it's not an exact answer to the question, in Amplify you can select between multiple, self-contained configuration files like this:

When you set up Amplify:

Amplify.addPlugin(AWSCognitoAuthPlugin())
// and whatever other plugins you'll need...
Amplify.configure(AmplifyConfiguration.fromConfigFile(applicationContext, getConfigResourceId(applicationContext)), applicationContext)

And also add:

private fun getConfigResourceId(context: Context): Int = context.resources.getIdentifier("YourConfigFileName", "raw", context.packageName)
Related