> ## Documentation Index
> Fetch the complete documentation index at: https://docs.partnero.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Android SDK

> Link Android app users to the partner who referred them, from a typed code, a partner app link or a Google Play install.

Use the Partnero Android library to tell Partnero which partner referred an app user. After that, any purchase recorded for the user, through [RevenueCat](https://docs.partnero.com/integrations/revenuecat.md) or your backend, earns that partner a commission. The [Mobile apps](https://docs.partnero.com/guides/tracking/mobile-app.md) guide explains the whole flow.

* Android 5.0 (API 21)+, Kotlin coroutines
* Depends only on `kotlinx-coroutines` and the Play Install Referrer library. No analytics and no background work
* Uses only your **Program ID**, never your API key

Source: [github.com/partnero/partnero-kotlin](https://github.com/partnero/partnero-kotlin)

## Before you start

In Partnero, open **Program settings** and turn on **Enable mobile app claims** in the **Tracking for mobile apps** section. Until then, every claim fails with `CodeNotFound`.

Copy your **Program ID** from the program's **Integration** page. It's the same ID as in your PartneroJS snippet.

## Install

Add the library from Maven Central to your app module's `build.gradle.kts`:

```kotlin theme={"system"}
dependencies {
    implementation("com.partnero:partnero-android:0.1.0")
}
```

## Configure

Call `configure` once in your `Application`:

```kotlin theme={"system"}
import com.partnero.Partnero

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Partnero.configure(this, programId = "YOUR_PROGRAM_ID")
    }
}
```

The first call records when the app was first opened on this device, which the program's claim window counts from.

Referral links use `?aff=` by default. If yours use another parameter, such as `?ref=DANIEL20`, pass `referralParameter = "ref"`.

## Claim a code the user enters

Claim the code when the user enters one. Pass the ID your purchases are reported under, such as the RevenueCat app user ID. The claim functions suspend, so call them from a coroutine such as `lifecycleScope.launch`.

```kotlin theme={"system"}
try {
    val result = Partnero.claim(
        code = "DANIEL20",
        appUserId = Purchases.sharedInstance.appUserID
    )
    when (result.status) {
        ClaimResult.Status.CLAIMED -> {}         // attributed to the code's partner
        ClaimResult.Status.ALREADY_CLAIMED -> {} // was attributed already; nothing changed
    }
} catch (e: PartneroException.CodeNotFound) {
    // show "That code isn't valid"
} catch (e: PartneroException) {
    // refused; see Claim results and failures
} catch (e: IOException) {
    // network error; let the user try again
}
```

Each user can be linked to one partner only, so the first successful claim is final. With RevenueCat, claim after `Purchases.sharedInstance.logIn(...)`, since anonymous IDs are rejected.

If a code is used as both a partner's link key and a promotion code, say which one the user entered:

```kotlin theme={"system"}
Partnero.claim(code = "DANIEL20", appUserId = user.id, codeType = CodeType.PROMOTION_CODE)
```

Save the code on the RevenueCat customer as well. Then a purchase can still be credited to the partner if the claim request was lost:

```kotlin theme={"system"}
Purchases.sharedInstance.setAttributes(
    Partnero.revenueCatAttributes("DANIEL20")
)
```

## Claim from an opened link

When a [partner app link](https://docs.partnero.com/guides/tracking/mobile-app.md#partner-app-links) or a referral link opens your app, pass the `Uri` on:

```kotlin theme={"system"}
override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    val uri = intent.data ?: return
    lifecycleScope.launch {
        Partnero.claim(uri, appUserId = Purchases.sharedInstance.appUserID)
    }
}
```

Do the same with `intent.data` in `onCreate` for links that start the app. The code is read from `partnero_code`, or from your referral parameter, such as `https://example.com/?aff=DANIEL20`. The click ID is read from `partnero_click`. It returns `null` without calling Partnero for links with neither.

If the user isn't signed in yet, store the values and claim after sign-in:

```kotlin theme={"system"}
val code = Partnero.code(uri)
val clickId = Partnero.clickId(uri)

// after sign-in
when {
    code != null -> Partnero.claim(code = code, appUserId = user.id, clickId = clickId)
    clickId != null -> Partnero.claimClick(clickId = clickId, appUserId = user.id)
}
```

With a deep linking provider such as AppsFlyer, Branch or Adjust, read the click from its callback and call `Partnero.claimClick(clickId, appUserId)`.

## Claim a Google Play install

When the program has no deep link and a **Google Play** link is set, app links send Android users to Google Play with the code and click ID in the install referrer. Partners can also share a Google Play link with their code in the `referrer` parameter, URL-encoded:

```text theme={"system"}
https://play.google.com/store/apps/details?id=com.example.app&referrer=aff%3DDANIEL20
```

Claim it once the user is signed in:

```kotlin theme={"system"}
lifecycleScope.launch {
    Partnero.claimFromInstallReferrer(
        appUserId = Purchases.sharedInstance.appUserID
    )
}
```

It returns `null` when the install didn't come from a partner link. Each install's referrer is claimed once. If Google Play can't be reached, or the claim fails because of a rate limit, a server error or the network, the next call tries again. Other refusals, such as an unknown code, are final for that install, so turn on mobile app claims before you release.

> [!TIP]
>
> Avoid `runCatching` around claims. It also swallows the `CancellationException` that coroutines rely on.

## Claim results and failures

`claim` returns a `ClaimResult` with a `status`:

| `status` | What it means |
| - | - |
| `CLAIMED` | Partnero linked the user to the partner and created the customer |
| `ALREADY_CLAIMED` | The user was linked to a partner before, and that link stays as it was |

Refusals are thrown as subclasses of `PartneroException`:

| Exception | When |
| - | - |
| `NotConfigured` | `configure` wasn't called |
| `InvalidInput` | A blank or malformed value. `field` says which one: `CODE`, `CLICK_ID`, `APP_USER_ID` or `PROGRAM_ID` |
| `CodeNotFound` | No active code or click matches, or mobile app claims are off |
| `CodeConflict` | The code matches more than one partner, or the claim window has closed |
| `RateLimited` | More than 30 claims a minute from one network. Try again in a minute |
| `Server` | Any other response, with the HTTP `status` |

When the request can't reach Partnero, the SDK throws `IOException`. It never retries on its own.

## Privacy

The SDK sends Partnero the code, the click ID when there is one, the app user ID, the platform and the first launch date. It stores the first launch date in `SharedPreferences`, and whether the install referrer was claimed. It doesn't read device identifiers. In your Google Play **Data safety** form, list the user ID as collected for **App functionality** if you don't already.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.