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
- Install the Login with Amazon Library
- Set network permissions for your project
- Add your API key to your project
- Handle configuration changes for your activity
- Add a WorkflowActivity to your project
- Add Amazon Shopping app to your project
Create a new project in Android Studio
- Launch Android Studio.
- From the File menu, select New and Project.
- Enter an Application Name and Company Name for your app.
- 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.
- Select a Minimum Required SDK of
API 16: Android 4.1 (JellyBean)
or higher and click Next. - Select the type of activity you want to create and click Next.
- 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.
- 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.
- With your project open in Android Studio, open the Project View.
- Right-click on the parent directory for your project/app in the Project View and select Paste.
- 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.
- From the Project View, double-click AndroidManifest.xml to open it.
-
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:
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.
- 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.
- From the Project View in Android Studio, right-click the
assets
folder, then click New > File. If you don't have anassets
folder, right-click the parent directory for your project then select New > Folder > Assets Folder. - Name the file api_key.txt.
- You should now have an editor window for a text file named api_key.txt. Add your API Key to the text file.
- In the File menu, click Save.
api_key.txt
file (such as a Byte Order Mark), you may see ERROR_ACCESS_DENIED
when you try to connect to the Login with Amazon authorization service. If this occurs, try removing any leading or trailing spaces, line feeds, or suspicious characters. (For example, an editor using Byte Order Mark might add 0xEF 0xBB 0xBF
or other hexadecimal sequences to the start of your api_key.txt
file). You can also try retrieving a new API key.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.
- Create a file called
api_key.txt
located inside your project's assets folder. Placing the file in this specific directory is required. - 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.
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.
- In Project View, double-click AndroidManifest.xml to open the file.
- In the Application block, find the activity that will handle Login with Amazon (for example, MainActivity).
-
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"
- 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
.
- In Project View, double-click AndroidManifest.xml to open the file.
-
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>
${applicationId}
with your package name for this app.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.
- In Project View, double-click AndroidManifest.xml to open the file.
- 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