Skip to main content

Overview

Android apps running Gemma 4 via Google AI Edge SDK (formerly MediaPipe LLM) can use Grantex consent bundles for offline authorization. This guide covers secure bundle storage with EncryptedSharedPreferences, JWT verification with Nimbus JOSE, and audit logging in Kotlin.

Prerequisites

  • Android Studio Ladybug or later
  • Android SDK 26+ (minSdk) — required for Jetpack Security
  • Kotlin 1.9+
  • A Grantex account with API key and registered agent

Step 1: Gradle Setup

Add the dependencies to your app’s build.gradle.kts:
Add the serialization plugin to your project-level build.gradle.kts:

Step 2: Define the ConsentBundle Model

Step 3: Secure Storage with EncryptedSharedPreferences

Use Jetpack Security to store the consent bundle. The encryption key is managed by Android Keystore, backed by hardware on supported devices.

Step 4: Offline JWT Verification

Use Nimbus JOSE to verify the grant token against the JWKS snapshot. This runs entirely on-device.

Step 5: Audit Logging

Implement a simplified audit log with hash chaining and Ed25519 signing.

Step 6: Putting It Together

Provisioning the Bundle

Call the Grantex API from your backend or a one-time setup activity. Do not embed your API key in the APK.

Security Considerations

  • Never embed API keys in the APK. Provision bundles from your backend.
  • EncryptedSharedPreferences uses AES-256-GCM with keys in Android Keystore (hardware-backed on most devices).
  • ProGuard / R8 should keep Nimbus JOSE and kotlinx-serialization classes. Add keep rules if verification fails in release builds.
  • Root detection is recommended for high-security use cases. A rooted device can extract the bundle from SharedPreferences.
  • Certificate pinning on OkHttp for the provisioning and sync calls prevents MITM attacks.

Next Steps

Last modified on April 3, 2026