# Android (Kotlin)

> Add ScreenlyyID identity verification to an Android app: a back end that calls the API, and an app that opens the hosted page in a WebView.

Canonical URL: https://www.screenlyyid.com/docs/recipes/android/

The [ScreenlyyID Android example](https://github.com/ScreenlyyID/android-sdk-example) is the reference for a native Android app. It mirrors the [iOS example](https://www.screenlyyid.com/docs/recipes/ios/) and has the same two parts, and the split matters:

- **`backend/`**: a small Node server that holds the API key and the `CorrelationId` and makes every ScreenlyyID call. You can swap it for your own server, for instance the [Express](https://www.screenlyyid.com/docs/recipes/express/) or [FastAPI](https://www.screenlyyid.com/docs/recipes/fastapi/) recipes, which have the same two routes.
- **`app/`**: the Kotlin app. It contains no API key and no third-party libraries. It asks the back end to create a verification, asks for camera permission, opens the `webEnrolmentUrl` in a `WebView`, listens for `IDV_SESSION_COMPLETE` and `ERROR`, then asks the back end for the result.

Follow the steps and the camera-permission notes on the [Mobile app](https://www.screenlyyid.com/docs/integrations/mobile-app/) page. The example's README explains how to run both parts on an emulator or a real phone.

## The two Android details

These are the usual reasons capture "does nothing" on Android:

1. **Grant the page's camera request.** The capture page asks the `WebView` for the camera, and the `WebView` only provides it if your `WebChromeClient.onPermissionRequest` grants it. Holding the `CAMERA` permission in your app is not enough. Grant it only to ScreenlyyID pages.
2. **Forward the page's events yourself.** The hosted page sends `IDV_SESSION_COMPLETE` and `ERROR` with `window.parent.postMessage`, and a `WebView` has no outer page to receive them. Register a JavaScript interface with `addJavascriptInterface`, then inject a `message` listener that calls it when the page has loaded.

## The pieces to copy

| What | File in the example |
|---|---|
| Ask the back end for a session and for the result (`HttpURLConnection`, no libraries) | `app/src/main/java/com/screenlyyid/democamera/Backend.kt` |
| Ask for camera permission before loading the page, send the user to Settings if they chose "Don't ask again" | `.../MainActivity.kt` |
| `WebView` setup, the camera grant, the event bridge, and only loading ScreenlyyID pages | `.../VerificationActivity.kt` |
| One request for the result after `IDV_SESSION_COMPLETE` | `.../ResultActivity.kt` |
| Manifest permissions and the demo-only HTTP allowance for the local back end | `app/src/main/AndroidManifest.xml`, `app/src/main/res/xml/network_security_config.xml` |
| The server routes | `backend/server.mjs` |

## Run it

1. Create a `test_` API key in the dashboard under **Integrations**.
2. Start the back end: `cd backend && SCREENLYYID_API_KEY=test_... node server.mjs`.
3. In the emulator, the app reaches the back end at `http://10.0.2.2:3000` (the default). On a real phone, run `adb reverse tcp:3000 tcp:3000` and set `DemoConfig.BACKEND_URL` to `http://localhost:3000`.
4. Run the app from Android Studio, or `./gradlew installDebug`.

The `network_security_config.xml` allows plain HTTP to those two local addresses for the demo only. A real back end uses HTTPS: remove the file and the manifest line that refers to it.
