Create a Login with Amazon Project

In this section, you will learn how to create a new Android project for Login with Amazon, configure the project, and add code to the project to sign in a user with Login with Amazon. If you do not yet have an app project for using Login with Amazon, you should create one now using the instructions below for Android Studio. If you have an existing app, skip to Install the Login with Amazon Library.

Create a New Project in Android Studio

  1. Launch Android Studio.
  2. From the File menu, select New and Project.
  3. Enter an Application Name and Company Name for your app.
  4. Enter the Application and Company Name corresponding to the package name that you chose when you registered your app with Login with Amazon. If you haven't registered your app yet, choose a Package Name and then follow the instructions at Register with Login with Amazon after you create your project. If the package name of your app does not match the registered package name, your Login with Amazon calls will not succeed.
  5. Select a Minimum Required SDK of API 11: Android 3.0 (Honeycomb) or higher and click Next. You can alternatively use a Minimum Required SDK of API 8: Android 2.2 (Froyo) or higher when using the v4 Android Support Library.
  6. Select the type of activity you want to create and click Next.
  7. Fill the relevant details and click Finish.

You will now have a new project in your workspace that you can use to call Login with Amazon.

Install the Login with Amazon Library

If you have not yet downloaded the Login with Amazon SDK for Android, see Install the Login with Amazon SDK for Android.

  1. Using the file system on your computer, find the login-with-amazon-sdk.jar file within the Login with Amazon SDK for Android. Copy it to the clipboard.
  2. With your project open in Android Studio, open the Project View.
  3. Right-click on the parent directory for your project/app in the Project View and select Paste.
  4. Right-click login-with-amazon-sdk.jar in the Project View and select Add As Library.

Set Network Permissions for Your Project

In order for your app to use Login with Amazon, it must access the Internet and access network state information. Your app must assert these permissions in your Android manifest, if it doesn't already.

  1. From the Project View, double-click AndroidManifest.xml to open it.
  2. Copy the lines of code below and paste them into the file, outside of the application block:

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

Example:

edited androidmanifest.xml

Add Your API Key to Your Project

When you register your Android application with Login with Amazon, you are assigned an API key . This is an identifier that the Amazon Authorization Manager will use to identify your application to the Login with Amazon authorization service . If you are using the Amazon Appstore to sign your app, the Appstore will provide the API key automatically. If you are not using the Amazon Appstore, the Amazon Authorization Manager loads this value at runtime from the api_key.txt file in the assets directory.

  1. If you do not have your API Key yet, see Android App Signatures and API Keys and follow the instructions under Retrieving an Android API Key.
  2. From the Project View in Android Studio, right-click the assets folder, then click New > File. If you don't have an assets folder, right-click the parent directory for your project then select New > Folder > Assets Folder.
  3. Name the file api_key.txt.
  4. You should now have an editor window for a text file named api_key.txt. Add your API Key to the text file.
  5. In the File menu, click Save.

Handle Configuration Changes for Your Activity

If the user changes the screen orientation or changes the keyboard state of the device while they are logging in, it will prompt a restart of the current activity. This restart will dismiss the login screen unexpectedly. To prevent this, you should set the activity that uses the authorize method to handle those configuration changes manually. This will prevent a restart of the activity.

  1. In Project View, double-click AndroidManifest.xml to open the file.
  2. In the Application block, find the activity that will handle Login with Amazon (for example, MainActivity).
  3. Add the following attribute to the activity you located in Step 2:

    android:configChanges="keyboard|keyboardHidden|orientation"
    

    or for API 13 or greater:

    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    
  4. From the File menu, click Save.

Now, when a keyboard or device orientation change happens, Android will call the onConfigurationChanged method for your activity. You do not need to implement this function unless there is an aspect of these configuration changes you want to handle for your app.

Add a WorkflowActivity to Your Project

When the user clicks the Login with Amazon button, the API will launch a web browser to present a login and consent page to the user. In order for this browser activity to work, you must add the WorkflowActivity to your manifest.

If you have previously integrated with the Login with Amazon SDK or you have the com.amazon.identity.auth.device.authorization.AuthorizationActivity activity declared in your AndroidManifest.xml, it must be removed and replaced with the WorkflowActivity.

  1. In Project View, double-click AndroidManifest.xml to open the file.
  2. In the Application block, add the following code:

    <activity android:name="com.amazon.identity.auth.device.workflow.WorkflowActivity"
            android:theme="@android:style/Theme.NoDisplay"
            android:allowTaskReparenting="true"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <!-- android:host must use the full package name found in Manifest General Attributes -->
                <data android:host="${applicationId}" android:scheme="amzn"/>
            </intent-filter>
        </activity>