Page updated Jan 23, 2024

Create your application

Goal

Setup a skeleton project so that Amplify categories can be added to it

Project Setup

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 24: Android 7.0 (Nougat) 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:

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_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

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:

Prerequisites: Install and configure the Amplify CLI.

1amplify init

Enter the following when prompted:

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 a class by extending Application and override its onCreate() to initialize Amplify 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:

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:

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:

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:

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.
  • Push Notifications - Add push notifications to your app to keep your users engaged and informed.