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.
1import com.amazonaws.auth.AWSCredentials;2import com.amazonaws.auth.AWSCredentialsProvider;3import com.amazonaws.auth.BasicSessionCredentials;4import com.amplifyframework.auth.AWSTemporaryCredentials;5import com.amplifyframework.auth.cognito.AWSCognitoAuthSession;6import com.amplifyframework.auth.options.AuthFetchSessionOptions;7import com.amplifyframework.core.Amplify;8
9import java.util.concurrent.CompletableFuture;10
11class AmplifyCredentialsProvider implements AWSCredentialsProvider {12
13 @Override14 public AWSCredentials getCredentials() {15 CompletableFuture<AWSCredentials> sdkCredentials = new CompletableFuture<>();16
17 Amplify.Auth.fetchAuthSession((authSession) -> {18 BasicSessionCredentials credentials = null;19 if (authSession instanceof AWSCognitoAuthSession) {20 AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) authSession;21 com.amplifyframework.auth.AWSCredentials awsCredentials =22 cognitoAuthSession.getAwsCredentialsResult().getValue();23 if (awsCredentials instanceof AWSTemporaryCredentials) {24 AWSTemporaryCredentials temporaryAwsCredentials =25 (AWSTemporaryCredentials) awsCredentials;26 credentials = new BasicSessionCredentials(27 temporaryAwsCredentials.getAccessKeyId(),28 temporaryAwsCredentials.getSecretAccessKey(),29 temporaryAwsCredentials.getSessionToken()30 );31 }32 }33
34 if (credentials != null) {35 sdkCredentials.complete(credentials);36 } else {37 sdkCredentials.completeExceptionally(38 new RuntimeException("Failed to get credentials")39 );40 }41 }, (exception) -> sdkCredentials.completeExceptionally(42 new RuntimeException("Failed to get credentials", exception)43 ));44
45 return sdkCredentials.join();46 }47
48 @Override49 public void refresh() {50 CompletableFuture<Void> result = new CompletableFuture<>();51 Amplify.Auth.fetchAuthSession(52 AuthFetchSessionOptions.builder().forceRefresh(true).build(),53 // We do not need to capture value if refresh succeeds54 (authSession) -> result.complete(null),55 // We do not need to throw if refresh fails56 (exception) -> result.complete(null)57 );58
59 result.join();60 }61}
1import com.amazonaws.auth.AWSCredentials2import com.amazonaws.auth.AWSCredentialsProvider3import com.amazonaws.auth.BasicSessionCredentials4import com.amplifyframework.auth.AWSTemporaryCredentials5import com.amplifyframework.auth.cognito.AWSCognitoAuthSession6import com.amplifyframework.auth.options.AuthFetchSessionOptions7import com.amplifyframework.core.Amplify8import java.lang.RuntimeException9import kotlin.coroutines.resume10import kotlin.coroutines.resumeWithException11import kotlin.coroutines.suspendCoroutine12import kotlinx.coroutines.runBlocking13
14class AmplifyCredentialsProvider : AWSCredentialsProvider {15
16 override fun getCredentials(): AWSCredentials = runBlocking {17 suspendCoroutine { continuation ->18 Amplify.Auth.fetchAuthSession(19 { authSession ->20 val awsTemporaryCredentials = (authSession as? AWSCognitoAuthSession)21 ?.awsCredentialsResult?.value as? AWSTemporaryCredentials22
23 val sdkCredentials = awsTemporaryCredentials?.let {24 BasicSessionCredentials(it.accessKeyId, it.secretAccessKey, it.sessionToken)25 }26
27 if (sdkCredentials != null) {28 continuation.resume(sdkCredentials)29 } else {30 val authException = RuntimeException("Failed to get credentials")31 continuation.resumeWithException(authException)32 }33 },34 {35 continuation.resumeWithException(36 RuntimeException("Failed to get credentials. See exception.", it)37 )38 }39 )40 }41 }42
43 override fun refresh() = runBlocking {44 suspendCoroutine { continuation ->45 Amplify.Auth.fetchAuthSession(46 AuthFetchSessionOptions.builder().forceRefresh(true).build(),47 // We do not need to capture value if refresh succeeds48 { continuation.resume(Unit) },49 // We do not need to throw if refresh fails50 { continuation.resume(Unit) }51 )52 }53 }54}
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.
1AWSConfiguration awsConfiguration = new AWSConfiguration(context);
1val 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)
1AWSConfiguration awsConfiguration = new AWSConfiguration(context);2TransferUtility transferUtility = TransferUtility.builder()3 .context(context)4 .awsConfiguration(awsConfig)5 .s3Client(6 new AmazonS3Client(7 new AmplifyCredentialsProvider(),8 Region.getRegion(Regions.US_EAST_1)9 )10 )11 .build();
1val awsConfiguration = AWSConfiguration(context)2val transferUtility = TransferUtility.builder()3 .context(context)4 .awsConfiguration(awsConfiguration)5 .s3Client(6 AmazonS3Client(7 AmplifyCredentialsProvider(),8 Region.getRegion(Regions.US_EAST_1)9 )10 )11 .build()
IoT (com.amazonaws:aws-android-sdk-iot)
1AWSIotClient client = new AWSIotClient(new AmplifyCredentialsProvider());
1val client = AWSIotClient(AmplifyCredentialsProvider())
Android SDK Generated by API Gateway (aws-android-sdk-apigateway-core)
1ApiClientFactory clientFactory = new ApiClientFactory();2clientFactory.credentialsProvider(new AmplifyCredentialsProvider());
1val clientFactory = ApiClientFactory()2clientFactory.credentialsProvider(AmplifyCredentialsProvider())