Feature flags on Android without an official SDK
dif does not ship an Android SDK. The supported runtime is JavaScript and TypeScript, with adapters for React and Svelte: no Kotlin package, no Java bindings, nothing to install from Maven. That rules out npm install, but it does not rule out a feature flag Android app, because dif’s assignment logic is not an API call. It is a hash function, and a hash function runs the same in Kotlin as it does in the browser.
Most teams hit this by reaching for a WebView bridge, or by standing up a proxy endpoint that forwards every flag check to a server. Both work. Both also add a round trip dif’s own SDK doesn’t have: there is no network call to decide a variant, because the variant is computed locally from a user id and a salt. Port that computation to Kotlin and an Android app gets the same deterministic assignment the web SDK gets, with no server in the loop.
This guide shows the exact algorithm, the one piece of real configuration you still need from the flag file, and a Kotlin port checked against dif’s own cross-language test fixture.
Key Takeaways
- dif’s official SDK covers JavaScript and TypeScript, with
@dif.sh/reactand@dif.sh/svelteadapters. There is no Android or Kotlin package.- Assignment is four steps: hash a namespace and the experiment id to get a salt, hash the salt and the user id to get a bucket, read the first four bytes as an unsigned 32-bit integer, and take it modulo 10,000.
- The salt is derivable from public inputs, so Kotlin doesn’t need to read it from anywhere. The one real piece of configuration is the variant weights, which come from the flag file.
- dif’s cross-language fixture pins real values, like user
u_1landing in bucket 7390 on the flagcheckout-cta-v2. The same numbers work as a Kotlin unit test.- For a flag on a payment path, or one whose weights change several times a day, proxy to a backend instead of porting the math. The algorithm never changes, but an app-store release cycle is slower than a merge.
No Android SDK, just JavaScript and TypeScript
@dif.sh/sdk is about 5 kB gzipped, zero dependencies, browser and server. @dif.sh/react and @dif.sh/svelte wrap it for those frameworks. That’s the full list. There is no @dif.sh/android, no Kotlin bindings, no native bridge dif maintains.
What makes porting worth attempting is what the SDK does not do: it never calls out to a server to get an answer. A flag’s variant is computed where the SDK runs, from a user id and a bit of math. An Android feature flag SDK built the same way, in Kotlin, is not a stretch. It’s the same four lines the JavaScript SDK already runs, in a different language.
Assignment is a hash, not an API
Every flag and experiment gets a per-experiment salt, derived once from a fixed namespace and the experiment’s id, hashed with SHA-256:
salt = SHA-256("dif.sh/v1" + experiment_id)[0:16]
A user’s bucket is a second hash, of that salt and their user id:
bucket = first 4 bytes of SHA-256(salt + user_id), read as an unsigned 32-bit integer, mod 10,000
Four bytes, not two. A 16-bit source would skew a nominal 50/50 split to about 53/47, because 2^16 isn’t evenly divisible by 10,000. A 32-bit source brings the residual bias down near zero. It’s a small detail, but it’s the kind of detail that tells you the math was checked, not guessed.
Once a user has a bucket, the variant is whichever one it falls under when you walk the variants in order, accumulating weight * 100 as you go. Nothing is stored. The same user id produces the same bucket every time, on any platform that runs the same four steps, which is the whole reason this ports.
Derive the salt in Kotlin
The namespace is a public constant, not a secret, so the salt doesn’t have to be copied from anywhere. It’s a pure function of the experiment id:
import java.security.MessageDigest
private const val NAMESPACE = "dif.sh/v1"
fun saltFor(experimentId: String): ByteArray {
val digest = MessageDigest.getInstance("SHA-256")
digest.update(NAMESPACE.toByteArray(Charsets.UTF_8))
digest.update(experimentId.toByteArray(Charsets.UTF_8))
return digest.digest().copyOfRange(0, 16)
}
MessageDigest is part of the Android platform, so this needs no extra dependency, the same way the web SDK needs none for its hashing.
Bucket the user and pick a variant
The second hash takes the salt and the user id, and the bucket picks a variant off a cumulative-weight walk:
import java.nio.ByteBuffer
fun bucketFor(salt: ByteArray, userId: String): Int {
val digest = MessageDigest.getInstance("SHA-256")
digest.update(salt)
digest.update(userId.toByteArray(Charsets.UTF_8))
val hash = digest.digest()
val quad = ByteBuffer.wrap(hash, 0, 4).int.toLong() and 0xFFFFFFFFL
return (quad % 10_000).toInt()
}
fun variantFor(bucket: Int, weights: List<Pair<String, Int>>): String? {
var cumulative = 0
for ((id, weight) in weights) {
cumulative += weight * 100
if (bucket < cumulative) return id
}
return null
}
weights is the one piece of real configuration in this whole port. It’s the same variants list an engineer writes into the flag’s frontmatter: an id and a weight from 0 to 100, in order. dif build compiles that list into dif/generated/client.ts alongside the salt, so a mobile team can read it straight out of the generated file and check it into a small JSON resource instead of retyping it:
// dif/generated/client.ts (generated by `dif build`, do not edit)
__register({
id: "checkout-cta-v2",
surface: "checkout",
variants: ["control", "variant_a"] as const,
salt: "<32 hex chars, derived from the experiment id>",
weights: { "control": 50, "variant_a": 50 },
exclusionGroup: null,
created: "2026-01-01",
audience: () => true,
});
That weights map is what moves to Kotlin. Nothing else in that file is logic the app needs to reimplement; salt is already derivable, and variants and audience are the same data a flag’s own .md file carries.
Verify the Android port against dif’s test vectors
dif keeps its Rust and TypeScript implementations in sync with a shared fixture: if the two ever drift by even one bucket, CI fails on both sides. The fixture is open source, so the same numbers work as a Kotlin test. On the flag checkout-cta-v2:
import org.junit.Test
import org.junit.Assert.assertEquals
class BucketingTest {
@Test
fun matchesDifFixture() {
val salt = saltFor("checkout-cta-v2")
assertEquals(7390, bucketFor(salt, "u_1"))
assertEquals(3919, bucketFor(salt, "u_2"))
assertEquals(9709, bucketFor(salt, "u_8131"))
}
}
If this test passes, the Kotlin port agrees with dif’s Rust and TypeScript SDKs on every input, not just the ones in a QA build. That’s a stronger guarantee than “it looked right on my phone.”
When a feature flag Android app should proxy instead
The math never changes. The weights do, and that’s the real cost of porting instead of calling a server. If a flag’s weights change in the repo today, a web app picks it up on the next deploy. An Android app picks it up on its next release, which can sit in app review for days. For a flag on a payment path that needs an instant kill, or one a growth team ramps several times a week, that lag is the wrong tradeoff, and a small backend endpoint that runs the real @dif.sh/sdk and returns the resolved variant is the safer call.
For most flags, where a day or two of staleness between releases is fine, porting the four lines above is enough, and it never drifts, because the algorithm itself is fixed. The feature flags in git post covers the other half of this: why the weights live in a reviewable file instead of a dashboard in the first place.
Getting started
Install the CLI and scaffold a workspace to see your own flag’s weights and salt before porting anything:
npm install -g @dif.sh/cli
dif init
dif build
dif build writes dif/generated/client.ts. Open it, copy the weights map for the flag you’re gating, and wire the two Kotlin functions above around it. The CLI reference covers the other verbs, and the .md format covers how a flag’s variants and weights are declared.
The SDK docs cover the JavaScript version this port is based on.
A feature flag Android app doesn’t need an SDK dif hasn’t shipped. It needs four lines of SHA-256, a cumulative-weight walk, and the same weights the flag file already has. Verify the port once against a known bucket, and it stays correct, because the hash itself never changes.
FAQ
Does dif have an Android SDK? No. dif’s SDK is JavaScript and TypeScript, with @dif.sh/react and @dif.sh/svelte adapters. There’s no Kotlin or Java package.
Can I use feature flags in a Kotlin app without an SDK? Yes. Assignment is a SHA-256 hash of a salt and a user id, not an API call, so it ports to any language that has a SHA-256 implementation. java.security.MessageDigest covers it on Android with no added dependency.
How does dif decide which variant a user gets? It hashes a per-experiment salt with the user id, takes the first four bytes of the digest as an unsigned 32-bit integer, and takes that modulo 10,000 to get a bucket in the range 0 to 9,999. The bucket is matched against each variant’s cumulative weight to pick the variant.
Is dif’s bucketing algorithm open source? Yes. dif is MIT licensed, and the Rust implementation this Kotlin port is based on is in the public repo on GitHub, alongside the fixture that keeps the Rust and TypeScript SDKs in sync.
What if my Android app and my website disagree on a user’s variant? They shouldn’t, as long as both read the same weights for the same experiment id. If they ever disagree, the weights baked into the app are stale relative to the repo, usually because a flag changed after the app’s last build.
Do I need dif Cloud to run feature flags on Android? No. Assignment works entirely locally. dif Cloud is only for aggregating exposure and outcome events if you want hosted analysis on top.