Developer Console

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 16: Android 4.1 (JellyBean) or higher and click Next.
  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.

Login with Amazon and In-App Purchasing integrated apps

Apps that use the Appstore SDK or In-App Purchasing SDK (IAP SDK) have separate requirements for API keys.

Debug apps

For a pre-release or "debug" version of your app, you must create an API key and store it in your project.

  1. Create a file called api_key.txt located inside your project's assets folder. Placing the file in this specific directory is required.
  2. Insert your API key as the only data in this api_key.txt file.

Production apps

For a release or "production" version of your app, if your app uses the Appstore SDK, you must create an additional API key for the release version of your app. If using the older IAP SDK v2.0 and you sign your app using your own certificate, you must also create an API key for the release version of your app. In contrast, if using the IAP SDK v2.0 and you allow Amazon to sign your app on your behalf, you do not need to create an additional API key. For a summary, see the following table.

You can find your AppStore certificate hash values in the Developer Console to create the API keys for existing apps. Go to My apps > select your app > APK Files > Appstore Certificate Hashes.

Determine if you're required to create an API key for your app.
Uses Appstore SDK Self-signs release app Production or debug version How to sign your app
Production The API key is automatically generated and injected for release apps, no need to do anything else.
Production Developer must create API key using their own release certificate hashes and add it to assets.
Production Developer must create API key using release certificate hashes from Developer Console and add it to assets
Production Developer must create API key using their own release certificate hashes and add it to assets.
any any Debug Developer must create API key using their own release certificate hashes and add it to assets.

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>
    

Add Amazon Shopping app to your project

If the Amazon Shopping app is installed on the Android device, clicking the "Login with Amazon" button triggers the API to launch the Amazon Shopping app, presenting a login or consent page. If your user is already signed in to the Amazon Shopping app, they don't need to log in again.

To ensure visibility of the Amazon Shopping app, if your project targets Android 11 (API level 30) or higher, please include the following section in your AndroidManifest.xml file.

  1. In Project View, double-click AndroidManifest.xml to open the file.
  2. Copy the lines of code below and paste them into the file, outside of the application block:
    <queries >
        <intent>
           <action android:name="com.amazon.identity.auth.device.authorization.MapAuthorizationService"/>
       </intent>
    </queries>
    

Last updated: Jan 11, 2024