Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Amplify v2 Compatibility

The AWS Mobile Client (com.amazonaws:aws-android-sdk-mobile-client) and Amplify Android v2 are not compatible with each other. Amplify v2 migrates the credentials from AWS Mobile Client into a different format, leaving AWS Mobile Client unable to read the credentials. If AWS Mobile Client is launched after this migration has taken place, the Amplify v2 credentials will also be cleared.

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.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.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import 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())