Guide
How to Add License Keys to an Android or Kotlin App
The Keyfern Kotlin SDK is one file with no dependencies. It runs on Android 8+ (API 26) and on any JVM 11+, from Kotlin or Java.
Every answer from Keyfern is signed with your product’s Ed25519 key. The app embeds only the public key, checks the signature and a random nonce on every answer, and keeps the last signed answer so it can start offline during the lease you set. For the full API see the Keyfern documentation.
Step 1: Add the SDK
Download KeyfernClient.kt into your app module (package com.keyfern). On Android, the app needs the internet permission:
<uses-permission android:name="android.permission.INTERNET" />
Step 2: Activate and verify
The calls block while they talk to the server, so run them off the main thread:
import com.keyfern.*
import java.io.File
val kf = Keyfern(
product = "prod_xxx",
publicKey = "<public key from the dashboard>", // safe to embed in your app
storage = FileStorage(File(context.filesDir, "keyfern.license")),
)
val machine = Keyfern.installId(File(context.filesDir, "keyfern.id")) // or Settings.Secure.ANDROID_ID
val lic = withContext(Dispatchers.IO) {
try { kf.activateOrCached(userKey, machine) } // online check, verified cache when offline
catch (e: KeyfernException) { null } // e.code: blocked, expired, invalid_key, machine_limit, lease_expired...
}
if (lic != null && lic.has(1)) unlockPro() // feature flag F1
The cached license is a signed answer, so it needs no encryption: editing it breaks the signature. Offline starts are refused once lease_until has passed, or when the device clock was set back more than one hour.
Android 8 to 12 (API 26 to 32)
Android has Ed25519 built in from Android 13 (API 33). For older versions, add BouncyCastle and pass it as the verifier:
// build.gradle.kts: implementation("org.bouncycastle:bcprov-jdk18on:1.78.1")
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters
import org.bouncycastle.crypto.signers.Ed25519Signer
val bcVerifier = { pub: ByteArray, sig: ByteArray, msg: ByteArray ->
Ed25519Signer().run { init(false, Ed25519PublicKeyParameters(pub, 0)); update(msg, 0, msg.size); verifySignature(sig) }
}
val kf = Keyfern("prod_xxx", "<public key>", storage = storage,
verifier = if (Build.VERSION.SDK_INT >= 33) null else bcVerifier)
Desktop JVM apps
On Windows, macOS and Linux, Keyfern.machineId() returns a stable hashed id of the computer, the same value the Python and JavaScript SDKs compute, so a key activated by one of your apps is the same machine for the others. Ed25519 is built into Java 15 and newer.
App Store and Google Play rules
Apps sold through the App Store or Google Play must sell digital content and subscriptions inside the app with Apple’s in-app purchase (StoreKit) or Google Play Billing. Keyfern keys on mobile are for licenses bought where the store rules allow it: business and enterprise apps, companion apps of software the customer already bought on the web, and licenses that cover several devices and platforms. Check the App Store Review Guidelines (payments) and the Google Play payments policy for your case before you ship.