Skip to content

How to initialize Amplify Auth inside an SDK without access to the Application class? #3155

@Marwa-Dakdouk-FOO

Description

@Marwa-Dakdouk-FOO

I’m building an SDK (a wrapper around AWS Liveness) that internally uses Amplify Auth.
According to the documentation, Amplify initialization should be done in the Application class:

Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.configure(applicationContext)

However, since my SDK is meant to be integrated by other apps, I don’t have control over their Application class and I’d like to minimize the setup required by the client.

In my case, the SDK receives its configuration dynamically from the backend at runtime, so initialization cannot be done once at app startup. Because of this, I’m currently calling:

val amplifyConfJson = "{ ... }" // JSON config from BE

fun initAwsLiveness(context: Context) {
    try {
        Amplify.addPlugin(AWSCognitoAuthPlugin())
        Amplify.configure(AmplifyConfiguration.builder(JSONObject(amplifyConfJson)).build(), context)
    } catch (ex: Exception) {
        Log.e("AwsLivenessHelper", ex.message ?: "")
    }
}

This function may be called multiple times (e.g., on retries or new SDK sessions)

My questions are:

  1. Is it safe to call Amplify.addPlugin() and Amplify.configure() multiple times (on retries or new sessions)? Or could this cause internal issues such as duplicate plugin registration or exceptions?
  2. If not recommended, is it acceptable for an SDK to handle this internally, i.e., to call these methods from within the SDK but ensure they are executed only once (by tracking configuration state with a flag)
  3. More generally, what is the recommended pattern for initializing Amplify when:
  • The SDK cannot depend on the host app’s Application class, and
  • Configuration values are fetched dynamically from a backend?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions