# Kotlin SDK for Android

The FusionAuth Android SDK allows you to add different OAuth2 and OpenID Connect functionalities to your Android application. It's a highly standardized and simplified starting point for developers to easily integrate FusionAuth in their own custom Android mobile apps by taking care of all the dependencies.

> For the index of this section of the site, see [llms.txt](https://fusionauth.io/docs/llms.txt)

This SDK allows you to use OAuth 2.0 and OpenId Connect functionality in an Android app with FusionAuth as the authorization server. It also provides a Token Manager to store, refresh, and retrieve tokens.

It's a highly standardized and simplified starting point for developers to easily integrate FusionAuth into their own custom mobile apps by taking care of all the dependencies.

The following OAuth 2.0 and OpenID Connect functionality are covered:

*   OAuth 2.0 Authorization Code Grant
*   OAuth 2.0 Refresh Token Grant
*   OpenID Connect UserInfo
*   OpenID Connect End Session

[AppAuth-Android](https://github.com/openid/AppAuth-Android) is used for the OAuth 2.0 Authorization Code Grant flow and OpenID Connect functionality.

The SDK is written in Kotlin and is compatible with Java.

## Getting Started

If you are new to Android development, you may want to start with the QuickStart guide. If you are already familiar with Android development, skip to the Configuration section.

### QuickStart

See the [FusionAuth Android Quickstart](https://fusionauth.io/docs/quickstarts/quickstart-kotlin-android-native.md) for a full tutorial on using FusionAuth and Android.

### Configuration

To use the FusionAuth Android SDK, add the following dependency to your `build.gradle.kts` file:

```kotlin
dependencies {
    implementation('io.fusionauth:fusionauth-android-sdk:<latest-version>')
}
```

After adding the dependency, you will need to initialize the `AuthorizationManager` with the `AuthorizationConfiguration`:

```kotlin
AuthorizationManager.initialize(
    AuthorizationConfiguration(
        fusionAuthUrl = "http://10.0.2.2:9011",
        clientId = "e9fdb985-9173-4e01-9d73-ac2d60d1dc8e",
        allowUnsecureConnection = true
    )
)
```

This will initialize the `AuthorizationManager` with the provided `AuthorizationConfiguration`. The `AuthorizationManager` is a singleton and can be accessed from anywhere in your app. The example configuration uses the IP address for your local machine, which is the default for the Android Emulator. If you are running the FusionAuth server on a different machine, you will need to replace the `fusionAuthUrl` with the correct URL.

Instead of specifying the `AuthorizationConfiguration` in code, you could also read it from a resource file:

```kotlin
AuthorizationManager.initialize(
    AuthorizationConfiguration.fromResources(this, R.raw.fusionauth_config)
)
```

The `fusionauth_config.json` file should be placed in the `res/raw` directory and should look like this:

```json
{
  "fusionAuthUrl": "http://10.0.2.2:9011",
  "clientId": "e9fdb985-9173-4e01-9d73-ac2d60d1dc8e",
  "allowUnsecureConnection": true
}
```

By default, the SDK uses the `MemoryStorage` for storing tokens. This means that tokens will be lost when the app is closed. To persist tokens, you can use the `DataStoreStorage` or implement your own `TokenStorage`.

## Usage

To start the OAuth 2.0 Authorization Code Grant, you can use the `oAuth()` function on the `AuthorizationManager` to retrieve the `OAuthAuthorizationService`:

```kotlin
AuthorizationManager
    .oAuth(this@LoginActivity)
    .authorize(
        Intent(this@LoginActivity, TokenActivity::class.java),
        OAuthAuthorizeOptions(
            cancelIntent = Intent(this@LoginActivity, LoginActivity::class.java)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
            state = "state-${System.currentTimeMillis()}"
        )
    )
```

The `authorize` function will start the OAuth 2.0 Authorization Code Grant flow and open the provided `Intent` when the flow is completed. The `OAuthAuthorizeOptions` allows you to specify additional options for the flow, such as the `cancelIntent` and the `state`.

If the user completes the flow, the `TokenActivity` will be opened, and you are required to handle the redirect:

```kotlin
AuthorizationManager.oAuth(this@TokenActivity)
    .handleRedirect(intent)
```

This will retrieve the authorization response, validates the `state` if it was provided, and exchanges the authorization code for an access token. The result of the exchange will be stored in the `TokenManager`.

After the user is authorized, you can use `getUserInfo()` to retrieve the [User Info](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo):

```kotlin
AuthorizationManager.oAuth(this@TokenActivity).getUserInfo()
```

To call your API with an access token, you can use the `AuthorizationManager` to retrieve a valid access token:

```kotlin
val accessToken = AuthorizationManager.freshAccessToken(this@TokenActivity)
```

This will retrieve a fresh access token from the `TokenManager` and return it. If the access token is expired, the `TokenManager` will refresh it automatically.

Finally, you can use the `AuthorizationManager` to sign out the user and remove the tokens from the `TokenManager`:

```kotlin
AuthorizationManager
    .oAuth(this@TokenActivity)
    .logout(
        Intent(this@TokenActivity, LoginActivity::class.java)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    )
```

If the user is signed out, the `LoginActivity` will be opened.

## Example App

See the [FusionAuth Android SDK Example](https://github.com/FusionAuth/fusionauth-quickstart-kotlin-android-native) for a functional example of an Android client that uses the SDK.

## Documentation

The latest full library documentation of the SDK is available here: [https://fusionauth.github.io/fusionauth-android-sdk/](https://fusionauth.github.io/fusionauth-android-sdk/).

## Source Code

The source code is available here: [https://github.com/FusionAuth/fusionauth-android-sdk/](https://github.com/FusionAuth/fusionauth-android-sdk/)
---

## Other pages in SDKs

> For the full index of this section, see [SDKs](https://fusionauth.io/docs/llms-sdks.txt).

- [Angular SDK](https://fusionauth.io/docs/sdks/angular-sdk.md): The FusionAuth Angular SDK allows you to add login, logout and registration functionality to your Angular application.
- [Example Apps](https://fusionauth.io/docs/sdks/examples.md): Learn about example applications using FusionAuth.
- [Go Client Library](https://fusionauth.io/docs/sdks/go.md): The FusionAuth Go Client library allows you to call FusionAuth from Go applications.
- [Java Client Library](https://fusionauth.io/docs/sdks/java.md): The FusionAuth Java Client library allows you to call FusionAuth from Java applications.
- [.NET Core Client Library](https://fusionauth.io/docs/sdks/netcore.md): The FusionAuth .NET Core Client library allows you to call FusionAuth from a .NET Core application.
- [OpenAPI Specification](https://fusionauth.io/docs/sdks/openapi.md): Learn about the FusionAuth OpenAPI Specification, which allows you to use OpenAPI tooling to interact with FusionAuth.
- [PHP Client Library](https://fusionauth.io/docs/sdks/php.md): The FusionAuth PHP Client library allows you to call FusionAuth from PHP applications.
- [Python Client Library](https://fusionauth.io/docs/sdks/python.md): The FusionAuth Python Client library allows you to call FusionAuth from Python applications.
- [React SDK](https://fusionauth.io/docs/sdks/react-sdk.md): The FusionAuth React SDK allows you to add login, logout and registration functionality to your React application.
- [Ruby Client Library](https://fusionauth.io/docs/sdks/ruby.md): The FusionAuth Ruby Client library allows you to call FusionAuth from Ruby applications.
- [Swift SDK for iOS](https://fusionauth.io/docs/sdks/swift-sdk.md): Create iOS mobile apps with FusionAuth-based authentication using the Swift SDK.
- [TypeScript Client Library](https://fusionauth.io/docs/sdks/typescript.md): The FusionAuth Typescript Client library allows you to call FusionAuth from JavaScript and Typescript applications.
- [Vue SDK](https://fusionauth.io/docs/sdks/vue-sdk.md): The FusionAuth Vue SDK allows you to add login, logout and registration functionality to your Vue application.
