Android SDK

Add Sanad to your Android app.

The Android SDK opens a Sanad session from your app, reads the device's signals in the background and sends them to Lean, so your backend can read the session's assessment.

📘

Prerequisites

Before you add the SDK, complete the setup in Getting started:

  • Ask your Lean representative to enable Sanad for your application.
  • Get your SDK key from the Application Dashboard.
  • Authentication: the access token your backend uses to read assessments.

This page covers only the SDK integration in your app.

1. Add the dependency

Add Lean's Maven repository. It needs no credentials.

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://me-central2-maven.pkg.dev/global-fraud-prd-7b8a/maven-releases' }
    }
}

Then add the SDK to your app module:

dependencies {
    implementation 'me.leantech.device:lean-device-android:X.X.X'
}

2. Permissions

The SDK's manifest merges into yours, so you don't declare its permissions yourself. All of them are granted at install, and your user never sees a dialog for them.

PermissionWhy the SDK needs it
INTERNETTo open the session and send signals to Lean, and to list the network interfaces where VPN tunnels show up
ACCESS_NETWORK_STATETo read the active connection: Wi-Fi or cellular, VPN, metered, Data Saver
USE_BIOMETRIC, USE_FINGERPRINTTo tell whether the phone has a fingerprint sensor and whether a fingerprint is enrolled. The SDK never shows a biometric prompt and never reads biometric data
com.google.android.providers.gsf.permission.READ_GSERVICESTo read the Google Services Framework id, which helps recognise the phone after a reinstall

The SDK also adds a <queries> block that names known rooting, hooking, app-cloning and fake-GPS apps, so it can check whether they're installed.

Optional permissions

The SDK never declares a permission that needs your user's approval. If your app already holds one of these, the SDK uses it. Without it, the related signal is reported as unavailable, and nothing breaks.

PermissionWhat it addsWho asks the user
ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATIONThe device's location, used to check that location, time zone and IP agree, and to spot mock locationsYour app, or the SDK if you turn on allowPrompts
READ_PHONE_STATEMobile data state on Android 12 and higherYour app only
<!-- Only if you want location signals -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

3. Start the SDK

Call LeanDevice.init once. Start it as early as you can, for example on your splash screen or when your app launches, and at the latest before your login screen. The first signals then reach Lean straight away, so an assessment is ready by the time your user logs in. It's safe to call from the main thread: its work runs in the background.

Keep the SDK key out of your source code, for example in a BuildConfig field set per build type:

android {
    buildFeatures { buildConfig = true }
    defaultConfig {
        buildConfigField("String", "LEAN_SDK_KEY", "\"lean_device_…\"")
    }
}
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import me.leantech.device.LeanDevice
import me.leantech.device.LeanDeviceConfig

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            val sessionId: String? = LeanDevice.init(
                this@MainActivity,
                LeanDeviceConfig(sdkKey = BuildConfig.LEAN_SDK_KEY),
            )
            // Keep sessionId to send to your backend. Null means there is no session:
            // carry on without Sanad, and see LeanDevice.diagnostics().
        }
    }
}

init opens a session with Lean and returns its id. Then, by default, it reads the device's signals once in the background and sends them to Lean.

init never crashes your app. It throws only one exception, LeanDeviceException with the code missing_sdk_key, when the key is blank. Every other failure, such as being offline or a refused key, returns null, and LeanDevice.diagnostics().lastError tells you why.

Configuration

ParameterDefaultDescription
sdkKeyRequiredYour SDK key. It also selects the Lean environment
allowPromptsfalseLets the SDK show the system location dialog, once per install
collectOnInittrueRead and send signals as soon as the session opens. Turn it off only to delay the first collection until your first collect() call
onSessionChangenullReserved for a future release
The location prompt

With allowPrompts = true and an Activity passed to init, the SDK shows the system location dialog once per install, and only if your manifest declares a location permission and neither is granted yet. The dialog never delays init. If your user grants location, the SDK sends one more set of signals in the same session.

The SDK's request uses the request code LeanDeviceConfig.LOCATION_REQUEST_CODE (0x4C45). Your onRequestPermissionsResult will see it; ignore it, as you would any code you didn't send. If your app already asks for location during its own onboarding, leave allowPrompts off.

4. Collect again before sensitive actions

The device can change during a session: a VPN switched on, a hooking tool attached. Call collect() right before each later sensitive action, such as a payment or a change of payout details, so Lean assesses the device as it is at that moment.

fun onConfirmPaymentClicked() {
    LeanDevice.collect()   // returns at once; the work runs in the background
    // ... then send your payment request with the session_id
}
  • collect() works only once init has returned a session id. A call made earlier is dropped, not queued.
  • A call made while a collection is still running joins that collection.
  • A session sends at most 10 sets of signals, counting the one from init. After that, collect() does nothing and lastError is envelope_cap.

5. Send the session id to your backend

Send the session_id to your backend with the login request, so your backend can read the assessment before it decides on the login, and add friction such as an OTP if there are red flags. Your backend keeps it for the rest of the session, so later requests don't need to carry it again. For example, as a header:

val request = Request.Builder()
    .url("https://api.your-app.com/login")
    .header("X-Lean-Device-Session", sessionId)
    .post(body)
    .build()
📘

Tip: link the session to your customer

Once your user logs in, consider linking the session to their Lean customer. Sanad can then also compare the device with that customer's other devices and locations.

6. Check your integration

  1. Run a debug build on a real phone with a sandbox key. On an emulator, emulator_detected fires, as it should.

  2. After init, log the diagnostics:

    val d = LeanDevice.diagnostics()
    Log.d("LeanDevice", "state=${d.state} session=${d.sessionId} error=${d.lastError} sdk=${d.sdkVersion}")

    You should see state=ACTIVE, a session id and error=null.

  3. Send the session id to your backend and get its assessment. It reads PENDING for a moment, then COMPLETED.

diagnostics() holds codes only, never device data or credentials, so it's safe to log and to attach to a support ticket. The codes are explained in Troubleshooting.


Did this page help you?