Amplify v2 Compatibility
Using Amplify V2 Auth with AWS Android SDK Plugin
We recommend using Amplify v2 with the AWS Kotlin SDK, rather than the AWS Android SDK. In order to better support existing implementations, this guide demonstrates how to continue using AWS Android SDK plugins with Amplify v2.
Creating an AmplifyCredentialsProvider
Many of the AWS Android SDK plugins accept a custom AWSCredentialsProvider
implementation. You can implement your own AWSCredentialsProvider
that uses Amplify Android v2 to provide credentials.
import com.amazonaws.auth.AWSCredentials;import com.amazonaws.auth.AWSCredentialsProvider;import com.amazonaws.auth.BasicSessionCredentials;import com.amplifyframework.auth.AWSTemporaryCredentials;import com.amplifyframework.auth.cognito.AWSCognitoAuthSession;import com.amplifyframework.auth.options.AuthFetchSessionOptions;import com.amplifyframework.core.Amplify;
import java.util.concurrent.CompletableFuture;
class AmplifyCredentialsProvider implements AWSCredentialsProvider {
@Override public AWSCredentials getCredentials() { CompletableFuture<AWSCredentials> sdkCredentials = new CompletableFuture<>();
Amplify.Auth.fetchAuthSession((authSession) -> { BasicSessionCredentials credentials = null; if (authSession instanceof AWSCognitoAuthSession) { AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) authSession; com.amplifyframework.auth.AWSCredentials awsCredentials = cognitoAuthSession.getAwsCredentialsResult().getValue(); if (awsCredentials instanceof AWSTemporaryCredentials) { AWSTemporaryCredentials temporaryAwsCredentials = (AWSTemporaryCredentials) awsCredentials; credentials = new BasicSessionCredentials( temporaryAwsCredentials.getAccessKeyId(), temporaryAwsCredentials.getSecretAccessKey(), temporaryAwsCredentials.getSessionToken() ); } }
if (credentials != null) { sdkCredentials.complete(credentials); } else { sdkCredentials.completeExceptionally( new RuntimeException("Failed to get credentials") ); } }, (exception) -> sdkCredentials.completeExceptionally( new RuntimeException("Failed to get credentials", exception) ));
return sdkCredentials.join(); }
@Override public void refresh() { CompletableFuture<Void> result = new CompletableFuture<>(); Amplify.Auth.fetchAuthSession( AuthFetchSessionOptions.builder().forceRefresh(true).build(), // We do not need to capture value if refresh succeeds (authSession) -> result.complete(null), // We do not need to throw if refresh fails (exception) -> result.complete(null) );
result.join(); }}
import com.amazonaws.auth.AWSCredentialsimport com.amazonaws.auth.AWSCredentialsProviderimport com.amazonaws.auth.BasicSessionCredentialsimport com.amplifyframework.auth.AWSTemporaryCredentialsimport com.amplifyframework.auth.cognito.AWSCognitoAuthSessionimport com.amplifyframework.auth.options.AuthFetchSessionOptionsimport com.amplifyframework.core.Amplifyimport java.lang.RuntimeExceptionimport kotlin.coroutines.resumeimport kotlin.coroutines.resumeWithExceptionimport kotlin.coroutines.suspendCoroutineimport kotlinx.coroutines.runBlocking
class AmplifyCredentialsProvider : AWSCredentialsProvider {
override fun getCredentials(): AWSCredentials = runBlocking { suspendCoroutine { continuation -> Amplify.Auth.fetchAuthSession( { authSession -> val awsTemporaryCredentials = (authSession as? AWSCognitoAuthSession) ?.awsCredentialsResult?.value as? AWSTemporaryCredentials
val sdkCredentials = awsTemporaryCredentials?.let { BasicSessionCredentials(it.accessKeyId, it.secretAccessKey, it.sessionToken) }
if (sdkCredentials != null) { continuation.resume(sdkCredentials) } else { val authException = RuntimeException("Failed to get credentials") continuation.resumeWithException(authException) } }, { continuation.resumeWithException( RuntimeException("Failed to get credentials. See exception.", it) ) } ) } }
override fun refresh() = runBlocking { suspendCoroutine { continuation -> Amplify.Auth.fetchAuthSession( AuthFetchSessionOptions.builder().forceRefresh(true).build(), // We do not need to capture value if refresh succeeds { continuation.resume(Unit) }, // We do not need to throw if refresh fails { continuation.resume(Unit) } ) } }}
You can now use your AmplifyCredentialsProvider
in any plugins that accept an AWSCredentialsProvider
, instead of using AWSMobileClient.getInstance()
as your AWSCredentialsProvider.
Providing AWS Configuration Information
Amplify v2 uses the amplifyconfiguration.json
file where AWS Android SDK uses the awsconfiguration.json
file. If you are using both Amplify v2 and AWS Android SDK in your project, it is important to ensure the resources are in sync. The Amplify CLI still generates and updates both of these file types, but any manual customizations should be applied to both files.
For AWS Android SDK plugins that require configuration information, you can continue to use the AWSConfiguration
class.
AWSConfiguration awsConfiguration = new AWSConfiguration(context);
val awsConfiguration = AWSConfiguration(context)
Example Usage of AWS Android SDK Plugins with Amplify v2
This is not an exhaustive list of supported plugins. Any plugins that accept an AWSCredentialsProvider and do not rely on AWS Mobile Client should work.
S3 Storage (com.amazonaws:aws-android-sdk-s3)
AWSConfiguration awsConfiguration = new AWSConfiguration(context);TransferUtility transferUtility = TransferUtility.builder() .context(context) .awsConfiguration(awsConfig) .s3Client( new AmazonS3Client( new AmplifyCredentialsProvider(), Region.getRegion(Regions.US_EAST_1) ) ) .build();
val awsConfiguration = AWSConfiguration(context)val transferUtility = TransferUtility.builder() .context(context) .awsConfiguration(awsConfiguration) .s3Client( AmazonS3Client( AmplifyCredentialsProvider(), Region.getRegion(Regions.US_EAST_1) ) ) .build()
IoT (com.amazonaws:aws-android-sdk-iot)
AWSIotClient client = new AWSIotClient(new AmplifyCredentialsProvider());
val client = AWSIotClient(AmplifyCredentialsProvider())
Android SDK Generated by API Gateway (aws-android-sdk-apigateway-core)
ApiClientFactory clientFactory = new ApiClientFactory();clientFactory.credentialsProvider(new AmplifyCredentialsProvider());
val clientFactory = ApiClientFactory()clientFactory.credentialsProvider(AmplifyCredentialsProvider())