AWS Amplify framework is a development framework for rapid web and mobile application development. It was meant for those beginners who might want to dive into the world of Amazon web services but might find it difficult due to its complexity. It has client libraries which give native and javascript support, prebuilt UI components, and a CLI for fast backend configuration and can be easily integrated with iOS, Android, Web, and React Native apps. It allows one to do a range of activities from authentication to creating APIs.
Before starting this, make sure you have an AWS account first. The first step to creating an android app project with AWS Amplify would be to download the AWS Amplify CLI. You can follow the steps in this link for a more detailed understanding: https://docs.amplify.aws/lib/getting-started/setup?platform=android
As shown in the link, you need to have Node (when I tried to run the first step it only accepted some versions of node, hence you might have to take note of it and change the version), Android Studio ver 3.6 and onwards, Android SDK minimum API level 16. The following command will allow installation of AWS Amplify CLI:
$ npm install -g @aws-amplify/cli
It might take some time for it to install but once it does, you would be good to go for the next step of adding authentication to an android application. To verify that it has been installed correctly just call the amplify command:
$ amplify
If you see the following on the terminal, you can be assured amplify was installed correctly:
Now that we have the Amplify CLI ready to go, we can start with the authentication part. Firstly, open up android studio and start a new project and go with the default values for the ‘Create Android Project’ section. Make sure to have the minimum API setting set to 16 or above. In my case, I would be using minimum API level 23.
In the ‘Add an Activity to Mobile’ section choose an empty activity and hit next:
Leave the default settings for configuration of the activity and click ‘Finish’. While Android studio is setting up the new project, go to the root directory of the project and run the following command on terminal:
$ amplify init
You will be prompted with the following:
You can just hit ‘Enter’ for choosing the default setting such as the project name and res directory. After the initial details, you will be asked to set up a new user if you have never done so before. At this just enter ‘Y’ and follow through the details the Amplify CLI will guide you through such as logging into your AWS account and setting up the new user:
After this, the console will prompt you enter the user key details which you have received from AWS Amplify:
After this, it will initialise the project on cloud and you can proceed with the next step. In this step, you will have to add authentication to the project by running this code:
$ amplify add auth
So, as seen above they will ask you for basic authentication configuration details, in which I chose phone number to be used for user sign in. After this run the following command to push all the required resources to cloud:
$ amplify push
This entire process could take up a few minutes so you can leave this alone and start editing the app project in Android Studio. All the following code is also found on the Amplify docs in the link mentioned above so if you need the detailed explanations please do visit it.The first step would be to add in all the required dependencies in the build.gradle file for the app:
//For AWSMobileClient only:
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.+'
//For the drop-in UI also:
implementation 'com.amazonaws:aws-android-sdk-auth-userpools:2.16.+'
implementation 'com.amazonaws:aws-android-sdk-auth-ui:2.16.+'
//For hosted UI also:
implementation 'com.amazonaws:aws-android-sdk-cognitoauth:2.16.+'
After this, you would have to create the Authentication activity by creating a new empty activity and naming it “AuthenticationActivity”.
The AuthenticationActivity would contain the following code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.Callback;
import com.amazonaws.mobile.client.SignInUIOptions;
import com.amazonaws.mobile.client.UserStateDetails;
public class AuthenticationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authentication);
AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
@Override
public void onResult(UserStateDetails userStateDetails) {
switch (userStateDetails.getUserState()){
case SIGNED_IN:
Log.i("INIT", "Logged in");
Intent i = new Intent(AuthenticationActivity.this, MainActivity.class);
startActivity(i);
break;
case SIGNED_OUT:
Log.i( "end", "Signed out");
signin();
break;
default:
AWSMobileClient.getInstance().signOut();
signin();
break;
}
}
@Override
public void onError(Exception e) {
Log.e("INIT", e.toString());
}
});
}
private void signin(){
try {
AWSMobileClient.getInstance().showSignIn(this, SignInUIOptions.builder().nextActivity(MainActivity.class).build());
}catch(Exception e){
Log.e("TAG", e.toString());
}
}
}
In the manifest file of the project you will have to make the following changes to let the launcher page be the authentication part:
<activity android:name=".MainActivity" android:label="@string/app_name">
</activity>
<activity android:name=".AuthenticationActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Now build the app and run it on your android device or emulator and you will see the following screen on the device:
