Kotlin SDK

Requirements, installation and client setup of the Voidhash Android SDK.

The Kotlin SDK adds purchases, access checks, paywalls, feature flags and analytics to native Android apps that sell through Google Play. Start with the quickstart. Options and methods are in the Kotlin SDK reference.

Compatibility

  • minSdk 23 and compileSdk 35. Play Billing 9 requires compileSdk 35 of your app too.
  • Android Gradle Plugin 8.9.0, Kotlin 2.0.21, Java 8 bytecode.
  • Play Billing 9.1.0, which the SDK brings in.

Install

The Kotlin SDK is on Maven Central as com.voidhash:voidhash. Make sure your build resolves dependencies from Maven Central, as new Android projects do:

settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}
app/build.gradle.kts
dependencies {
    implementation("com.voidhash:voidhash:0.3.0")
}

The quickstart lists the rest of the setup.

The SDK is published as three artifacts, which always share one version:

ArtifactContents
com.voidhash:voidhashThe SDK you call: com.voidhash.sdk.
com.voidhash:voidhash-coreThe shared engine the SDK depends on. You don't add it yourself.
com.voidhash:voidhash-uiThe flow renderer, which the SDK finds on its own once you depend on it, with a native library for arm64-v8a, armeabi-v7a and x86_64.

The @voidhash/android npm package, which earlier releases offered as source, is deprecated: install from Maven Central instead.

Data collection

Account for what the SDK sends when you fill in Google Play's Data safety form.

DataWhen the SDK collects itUsed for
Device IDAlways. The SDK creates an anonymous ID for each install.App functionality, analytics
User IDWhen you call identify with your user ID.App functionality, analytics
Email address and nameOnly when you pass them to identify or set them as attributes.App functionality
Purchase historyAlways. The SDK reports the Google Play purchases it observes.App functionality, analytics
Product interactionWhile analytics consent is granted, the default: captured events, lifecycle events and screen views.Analytics
Advertising dataWhile consent is granted: the campaign, ad and click IDs behind the install, from Google Play or Meta's apps, unless installAttribution = false.Analytics
Crash dataWhile consent is granted: that the app crashed, and in which version. No crash logs.Analytics
Coarse locationWhile consent is granted. Voidhash derives a city-level location from the request's IP address.Analytics

Events also carry the app name, version and build, package name, device brand and model, OS version, locale and time zone. The SDK reads no advertising ID or Android ID, and shares no data for tracking.

The SDK stores its state in the com.voidhash.sdk.cache shared preferences file and in files/voidhash/. Android Auto Backup includes both, so a restored device keeps the anonymous ID. To start restored installs with a new anonymous ID, exclude com.voidhash.sdk.cache.xml (domain sharedpref) in your backup rules. Keep files/voidhash/, which holds queued purchases. An anonymous buyer on a restored device then needs restorePurchases() to regain access.

Create the client

Configure one client when your app starts, then initialize it:

App.kt
val voidhash = Voidhash.configure(
    context = this,
    publishableKey = "vh_pk_...",
    options = VoidhashOptions(debug = BuildConfig.DEBUG),
)

ProcessLifecycleOwner.get().lifecycleScope.launch {
    voidhash.initialize()
}

Ship the publishable key, never a secret key. Release builds, including Play testing tracks, use the production key; debug builds in development mode use the test key. See API keys.

configure is synchronous and returns the client, also available as Voidhash.shared. Until initialize() runs, the client does no work, so you can defer it until consent or sign-in. initialize() loads stored state, then connects to Google Play, loads your project's configuration and resumes unfinished purchases in the background. It never fails because Voidhash is unreachable, and repeated calls are safe. Until the configuration has loaded, purchase throws CONFIGURATION_MISSING.

Voidhash runs only in your app's main process. In another process, such as a service with its own android:process, configure returns an inert client. If your UI runs in a named process, set runInSecondaryProcess = true and configure Voidhash in that process only.

If your app has a directBootAware component, Android can start it after a restart before the device is first unlocked. The app's storage isn't available then, so the client waits for the unlock. It keeps the newest 100 events and screens and up to 100 identify and attribute calls in memory, always keeps a reported purchase, and answers reads with no value, marked stale and expired. getDistinctId() and sessionId return temporary IDs until the unlock; don't store or send them. purchase, restorePurchases() and syncPurchases() throw DEVICE_LOCKED, and presentFlow throws FLOW_UNAVAILABLE.

Once the device is unlocked, the SDK starts on its own and makes the held calls in order, and initialize() returns once the client works. The client configure returned keeps working, so you don't need to read Voidhash.shared again.

Calling configure again replaces the client: the previous one stops, and its queued data moves to the new one when both use the same key and baseUrl. The previous client's identity, attribute and store methods throw CLIENT_SHUT_DOWN. For every option VoidhashOptions takes, see Client options.

Generated slug types

The Voidhash CLI can write your project's product, perk and location slugs to a Kotlin file as enum classes, so a mistyped slug fails to compile instead of at run time. The CLI is a single program that doesn't need Node.js: install it with Homebrew or the install script. Without the file, pass slugs as strings; everything else works the same.

List the file in voidhash.json in your project's root:

voidhash.json
{
  "team": "acme",
  "project": "my-app",
  "types": [{ "language": "kotlin", "output": "app/src/main/java/com/acme/app/VoidhashTypes.kt" }]
}

Run voidhash types generate after you change slugs in Studio, and commit the file. voidhash types check fails when the committed file is behind the project, which suits CI. voidhash init writes both files for you in a Gradle project.

The file's package follows from its path below a java or kotlin source directory, here com.acme.app. To choose another, add "package": "com.acme.billing" to the entry.

The file declares three enum classes with a constant per slug:

app/src/main/java/com/acme/app/VoidhashTypes.kt
package com.acme.app

/** The slugs of the project's products. */
enum class VoidhashProductSlug(val value: String) {
    PRO_ANNUAL("pro-annual"),
    PRO_MONTHLY("pro-monthly"),
    ;

    override fun toString(): String = value

    companion object {
        /** The constant of a slug, or null when the project had no such slug when this file was generated. */
        fun fromValue(value: String): VoidhashProductSlug? = values().firstOrNull { it.value == value }
    }
}

VoidhashLocationSlug and VoidhashPerkSlug have the same shape. A slug that isn't a Kotlin name is spelled as one, so pro-monthly is PRO_MONTHLY. The SDK takes slugs as String, so pass a constant's value:

val result = voidhash.presentFlow(activity, VoidhashLocationSlug.ONBOARDING.value)
val product = voidhash.getProducts().first { it.slug == VoidhashProductSlug.PRO_MONTHLY.value }
val hasPremium = voidhash.getCurrentPerson()?.hasActivePerk(VoidhashPerkSlug.PREMIUM.value) == true

Next steps

Something wrong or missing on this page? Tell us by email or on Discord.