Amplify project setup

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

For this section you will set up a skeleton project so that Amplify categories can be added to it.

1. Create a new project

Open Android Studio. Select + Create New Project.

Shows the Android studio welcome window

In Select a Project Template, select Empty Activity. Press Next.

Shows Android studio new project window

Next, configure your project:

  • Enter MyAmplifyApp in the Name field
  • Select either Java or Kotlin from the Language dropdown menu
  • Select API 16: Android 4.1 (Jelly Bean) from the Minimum SDK dropdown menu
  • Press Finish

Shows Android studio configure project window

Android Studio will open your project with a tab opened to either MainActivity.java or MainActivity.kt depending upon if you created a Java or Kotlin project respectively.

Shows Android studio successfully setup with the editor open

You now have an empty Android project into which you'll add Amplify in the next steps.

2. Install Amplify Libraries

Amplify for Android is distributed as Apache Maven packages. In this section, you'll add the packages and other required directives to your build configuration.

Under Gradle Scripts, open build.gradle (Module :app).

Add the following lines:

android { compileOptions { // Support for Java 8 features coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { // Amplify core dependency implementation 'com.amplifyframework:core:ANDROID_V1_VERSION' // Support for Java 8 features coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5' }
1android {
2 compileOptions {
3 // Support for Java 8 features
4 coreLibraryDesugaringEnabled true
5 sourceCompatibility JavaVersion.VERSION_1_8
6 targetCompatibility JavaVersion.VERSION_1_8
7 }
8}
9
10dependencies {
11 // Amplify core dependency
12 implementation 'com.amplifyframework:core:ANDROID_V1_VERSION'
13
14 // Support for Java 8 features
15 coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
16}
  • Set coreLibraryDesugaringEnabled, sourceCompatibility, and targetCompatibility to allow your application to make use of Java 8 features like Lambda expressions
  • Add Amplify Core and Desugaring libraries to the dependencies block

Amplify Android supports API levels 16 and higher. If your are supporting a min SDK less than 21, follow Android's documentation on adding multidex support.

Run Gradle Sync

Android Studio requires you to sync your project with your new configuration. To do this, click Sync Now in the notification bar above the file editor.

Screenshot with an arrow pointing to the Sync Now button in the file's notification bar

When complete, you will see CONFIGURE SUCCESSFUL in the output in the Build tab at the bottom of your screen.

Shows Android studio gradle sync successful

3. Provision the backend with Amplify CLI

To start provisioning resources in the backend, change directories to your project directory and run amplify init:

amplify init
1amplify init

Enter the following when prompted:

? Enter a name for the project `MyAmplifyApp` ? Initialize the project with the above configuration? `No` ? Enter a name for the environment `dev` ? Choose your default editor: `Android Studio` ? Choose the type of app that you're building `android` ? Where is your Res directory: `app/src/main/res` ? Select the authentication method you want to use: `AWS profile` ? Please choose the profile you want to use `default`
1? Enter a name for the project
2 `MyAmplifyApp`
3? Initialize the project with the above configuration?
4 `No`
5? Enter a name for the environment
6 `dev`
7? Choose your default editor:
8 `Android Studio`
9? Choose the type of app that you're building
10 `android`
11? Where is your Res directory:
12 `app/src/main/res`
13? Select the authentication method you want to use:
14 `AWS profile`
15? Please choose the profile you want to use
16 `default`

Upon successfully running amplify init, you will see a configuration file created in ./app/src/main/res/raw/ called amplifyconfiguration.json.

This file will be bundled into your application so that the Amplify libraries know how to reach your provisioned backend resources at runtime.

4. Initialize Amplify in the application

Create an Application class and add the Amplify initialization into its onCreate() to initialize Amplify once in your application.

Right-click on your namespace (e.g. com.example.MyAmplifyApp), click New, and click Java Class or Kotlin File/Class depending on which language you choose.

Configure the new class in New Java Class:

  • Enter MyAmplifyApp in the Name field
  • Press enter
  • Extend MyAmplifyApp from android.app.Application by adding extends Application to your class

Initialize Amplify by adding an onCreate method with the following code:

public void onCreate() { super.onCreate(); try { Amplify.configure(getApplicationContext()); Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }
1public void onCreate() {
2 super.onCreate();
3
4 try {
5 Amplify.configure(getApplicationContext());
6 Log.i("MyAmplifyApp", "Initialized Amplify");
7 } catch (AmplifyException error) {
8 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
9 }
10 }

Configure the new class in New Kotlin File/Class:

  • Enter MyAmplifyApp in the Name field
  • Press enter
  • Extend MyAmplifyApp from android.app.Application by adding : Application() to your class

Initialize Amplify by adding an onCreate method with the following code:

override fun onCreate() { super.onCreate() try { Amplify.configure(applicationContext) Log.i("MyAmplifyApp", "Initialized Amplify") } catch (error: AmplifyException) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error) } }
1override fun onCreate() {
2 super.onCreate()
3
4 try {
5 Amplify.configure(applicationContext)
6 Log.i("MyAmplifyApp", "Initialized Amplify")
7 } catch (error: AmplifyException) {
8 Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
9 }
10}

This overrides the onCreate() to initialize Amplify when your application is launched.

Next, configure your application to use your new custom Application class. Open manifests > AndroidManifest.xml, and add a android:name attribute with the value of your new class name:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.MyAmplifyApp"> <!-- Add the android:name attribute to the application node --> <application android:name=".MyAmplifyApp" ... </application> </manifest>
1<?xml version="1.0" encoding="utf-8"?>
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.MyAmplifyApp">
4
5 <!-- Add the android:name attribute to the application node -->
6 <application
7 android:name=".MyAmplifyApp"
8 ...
9 </application>
10</manifest>

Next, build and run the application. In logcat, you'll see a log line indicating success:

com.example.MyAmplifyApp I/MyAmplifyApp: Initialized Amplify
1com.example.MyAmplifyApp I/MyAmplifyApp: Initialized Amplify

Next steps

Congratulations! You've created a skeleton app and are ready to start adding Amplify categories to your application. The following are some categories that you can start to build into your application:

  • Analytics - for logging metrics and understanding your users
  • API (GraphQL) - for adding a GraphQL endpoint to your app
  • API (REST) - for adding a REST endpoint to your app
  • Authentication - for managing your users
  • DataStore - for making it easier to program for a distributed data store for offline and online scenarios
  • Geo - to use location data and map UI components.
  • Predictions - to detect text, images, and more!
  • Storage - store complex objects like pictures and videos to the cloud.