--- description: How to use authgear android SDK --- # Android SDK [![LLM | View as markdown](https://img.shields.io/badge/LLM-View%20as%20markdown-blue)](https://raw.githubusercontent.com/authgear/docs/refs/heads/main/get-started/native-mobile-app/android/README.md) This guide provides instructions on integrating Authgear with an Android app. Supported platforms include: * Android 5.0 (API 21) or higher Follow this guide to add Authgear to your Android app in 🕐 10 minutes. {% hint style="info" %} You can find the full code for the demo app for this tutorial in [this Github repo](https://github.com/authgear/authgear-example-android) {% endhint %} ## Setup Application in Authgear Sign up for an Authgear Portal account at [https://portal.authgear.com](https://portal.authgear.com/). Or you can use your self-deployed Authgear. From the Project listing, create a new Project or select an existing Project. After that, we will need to create an application in the project. ### **Step 1: Create an application in the Portal** Go to **Applications** on the left menu bar.
Click **⊕Add Application** in the top toolbar. Input the name of your application and select **Native App** as the application type. Click "Save". You will see a list of guides that can help you for setting up, then click "Next". ![Create an application](<../../../.gitbook/assets/authgear-new-app-native-2 (1).png>) ### **Step 2: Configure the application** Define a custom URI scheme that Authgear will use to redirect users back to your app after they have authenticated. The scheme should be based on the package name for your Android app. For the demo app, we'll be creating in this guide the scheme is: `com.example.authgeardemo://host/path`. To learn more about setting up a custom URI scheme in Android, see the official documentation [here](https://developer.android.com/training/app-links/deep-linking). Head back to Authgear Portal, and add the URL scheme you have defined as a Redirect URI. For our demo app, add the following URI: ``` com.example.authgeardemo://host/path ``` Click "Save" in the top toolbar and note the **Client ID** as you'll use it later in your Android app. You can also obtain it again from the Applications list later. ![Fill in the Redirect URI](<../../../.gitbook/assets/authgear-app-config-android (1).png>) ## Add Authgear to an Android Application In this step, we'll add user authentication to an Android application using the Authgear client application we set up in the previous steps. ### Pre-requisites To follow along, you need to have the following: * Android Studio installed on your computer * Basic knowledge of Kotlin or Java ### Step 1: Create an Android App project For the purpose of this guide, we'll be creating a new simple Android app project. Feel free to skip this step if you are adding Authgear to your existing app. Open Android Studio and create a new project with the following details: * Select **Empty View Activity** on the Activity selection screen. * **Name**: My Dem App * **Build configuration language**: Groovy DSL {% hint style="info" %} The reason for recommending you use Groovy DSL as **Build configuration language** for this guide is to make it easier to copy and paste the Gradle configurations we've provided without having to make many rewrites. {% endhint %} ### Step 2: Add Authgear SDK to your project The Authgear Android SDK makes it easier to interact with Authgear endpoints and services from your Android app. To add the SDK to your app, first, add the `jitpack.io` repository to your project by adding the following to your project's `settings.gradle` file: ```groovy dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { mavenCentral() maven { url 'https://jitpack.io' } } } ``` Next, add `authgear` in the `dependencies` section of your app-level (`/app/build.gradle`) `build.gradle`. Use `$branch-SNAPSHOT` (e.g. `main-SNAPSHOT`) for the latest version in a branch or a release tag/git commit hash of the desired version. ```groovy dependencies { // Other implementations implementation 'com.github.authgear:authgear-sdk-android:SNAPSHOT' } ``` Replace `SNAPSHOT` with the latest version of the Authgear SDK from: [https://github.com/authgear/authgear-sdk-android/tags](https://github.com/authgear/authgear-sdk-android/tags). For example, replacing SNAPSHOT with `2024-12-11.0` to use the latest version at the time of writing this. #### Enable Java 8+ API desugaring support To enable Java 8+ API desugaring support for your project, make the following changes to the app-level `build.gradle` file. 1. Add `coreLibraryDesugaringEnabled true` to the `android` > `compileOptions` section: ```gradle compileOptions { coreLibraryDesugaringEnabled true } ``` 2. Then add the coreLibraryDesugaring to the dependencies section: ```gradle dependencies { // Other implementations coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3' } ``` Learn more about Java 8+ API desugaring support [here](https://developer.android.com/studio/write/java8-support#library-desugaring). Sync Gradle to continue. ### Step 3: Initialize Authgear In this step, we'll initialize Authgear in the onCreate method of our app's `MainActivity.kt` class using a member variable. Alternatively, you can initialize Authgear in any class that's the entry point for your app: First, declare a member variable `authgear` like this: ```kotlin class MainActivity : AppCompatActivity() { private lateinit var authgear: Authgear override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... } } ``` Next, we'll initialize a new instance of the Authgear SDK and call the `configure()` method in the onCreate function. ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) authgear = Authgear(application, "", "") authgear.configure(object : OnConfigureListener { override fun onConfigured() { // Authgear can be used. } override fun onConfigurationFailed(throwable: Throwable) { Log.d("TAG", throwable.toString()) // Something went wrong, check the client ID or endpoint. } }) } ``` Replace `` and `` with the values from the configuration page of your Authgear client application. The complete code for `MainActivity.kt` at this point should look like this: ```kotlin class MainActivity : AppCompatActivity() { private lateinit var authgear: Authgear override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) authgear = Authgear(application, "", "") authgear.configure(object : OnConfigureListener { override fun onConfigured() { // Authgear can be used. } override fun onConfigurationFailed(throwable: Throwable) { Log.d("TAG", throwable.toString()) // Something went wrong, check the client ID or endpoint. } }) } } ``` {% hint style="info" %} Import any class that shows as unresolved. {% endhint %} ### Step 4: Add Login Button In this step, we'll add a login button that when the user taps on will open the login/sign-up page. Open `res/layout/activity_main.xml` and delete the default "Hello World!" TextView. Switch to the code view of `activity_main.xml` and add the login button and a TextView inside the root view (ConstraintsLayout). ```xml