Getting started

You are currently viewing the AWS SDK for Mobile documentation which is a collection of low-level libraries. Use the Amplify libraries for all new app development. Learn more

Install Dependencies

After initialization in your project directory with amplify init, update your App build.gradle with the below:

1//For AWSMobileClient only:
2implementation 'com.amazonaws:aws-android-sdk-mobile-client:ANDROID_SDK_VERSION'
3
4//For the drop-in UI also:
5implementation 'com.amazonaws:aws-android-sdk-auth-userpools:ANDROID_SDK_VERSION'
6implementation 'com.amazonaws:aws-android-sdk-auth-ui:ANDROID_SDK_VERSION'
7
8//For hosted UI also:
9implementation 'com.amazonaws:aws-android-sdk-cognitoauth:ANDROID_SDK_VERSION'

For the AWSMobileClient alone you can have a minimum SDK version of 15, but for the drop-in UI you will need a minimum of 23 set in your build.gradle:

1minSdkVersion 15

Add the following permissions to the AndroidManifest.xml file:

1<uses-permission android:name="android.permission.INTERNET"/>
2<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Build your Android Studio project.

Automated Setup

Run the following command in your project's root folder:

1amplify add auth

If you have previously enabled an Amplify category that uses Auth behind the scenes, e.g. API category, you may already have an Auth configuration. In such a case, run amplify auth update command to edit your configuration.

The CLI prompts will help you to customize your auth flow for your app. With the provided options, you can:

  • Customize sign-in/registration flow
  • Customize email and SMS messages for Multi-Factor Authentication
  • Customize attributes for your users, e.g. name, email
  • Enable 3rd party authentication providers, e.g. Facebook, Twitter, Google and Amazon

After configuring your Authentication options, update your backend:

1amplify push

A configuration file called awsconfiguration.json will be copied to your project ./app/src/main/res/raw directory. The AWSMobileClient will leverage this for communicating with backend services.

Lambda Triggers

The CLI allows you to configure Lambda Triggers for your Amazon Cognito User Pool. These enable you to add custom functionality to your registration and authentication flows.

Manual Setup

For manual configuration without the CLI, you must have an awsconfiguration.json file with the following:

  • Cognito User Pools: CognitoUserPool : { Default: ...}
  • Cognito Identity Pools: IdentityManager and CredentialsProvider: {CognitoIdentity: ...}
1{
2 "IdentityManager": {
3 "Default": {}
4 },
5 "CredentialsProvider": {
6 "CognitoIdentity": {
7 "Default": {
8 "PoolId": "XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab",
9 "Region": "XX-XXXX-X"
10 }
11 }
12 },
13 "CognitoUserPool": {
14 "Default": {
15 "PoolId": "XX-XXXX-X_abcd1234",
16 "AppClientId": "XXXXXXXX",
17 "Region": "XX-XXXX-X"
18 }
19 }
20 }

If you are using both Cognito User Pools and Identity Pools, such as in Federated scenarios, you will need all of the keys mentioned above.

Initialization

Go to your MainActivity and inside the onCreate() run the initialize() routine:

1AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
2 @Override
3 public void onResult(UserStateDetails userStateDetails) {
4 Log.i("INIT", "onResult: " + userStateDetails.getUserState());
5 }
6
7 @Override
8 public void onError(Exception e) {
9 Log.e("INIT", "Initialization error.", e);
10 }
11 }
12);

Build and run your program to see the initialized client in LOGCAT messages. Since you haven't logged in yet it will print a state of SIGNED_OUT. The getUserState() function returns an ENUM which you can perform different actions in your workflow. For example:

1AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
2 @Override
3 public void onResult(UserStateDetails userStateDetails) {
4 switch (userStateDetails.getUserState()){
5 case SIGNED_IN:
6 runOnUiThread(new Runnable() {
7 @Override
8 public void run() {
9 TextView textView = (TextView) findViewById(R.id.text);
10 textView.setText("Logged IN");
11 }
12 });
13 break;
14 case SIGNED_OUT:
15 runOnUiThread(new Runnable() {
16 @Override
17 public void run() {
18 TextView textView = (TextView) findViewById(R.id.text);
19 textView.setText("Logged OUT");
20 }
21 });
22 break;
23 default:
24 AWSMobileClient.getInstance().signOut();
25 break;
26 }
27 }
28
29 @Override
30 public void onError(Exception e) {
31 Log.e("INIT", e.toString());
32 }
33});

You might leverage the above workflow to perform other actions in the SIGNED_IN case, such as calling GraphQL or REST APIs with AWS AppSync or Amazon API Gateway or uploading content with Amazon S3.