Featurevisor

Evaluate in your application

Kotlin SDK

Kotlin and Android applications use the Java SDK, which evaluates flags, variations and variables in memory from a datafile, so no decision waits on the network. Every Java method works from Kotlin, and this page shows the Kotlin forms of the common ones.

Installation

The Featurevisor Java SDK is published through GitHub Packages. Add its repository to settings.gradle.kts:

dependencyResolutionManagement {
repositories {
mavenCentral()
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/featurevisor/featurevisor-java")
credentials {
username = providers.gradleProperty("gpr.user")
.orElse(providers.environmentVariable("GITHUB_ACTOR"))
.getOrElse("")
password = providers.gradleProperty("gpr.key")
.orElse(providers.environmentVariable("GITHUB_TOKEN"))
.getOrElse("")
}
}
}
}

GitHub Packages requires authentication. Add a GitHub username and a personal access token with the read:packages scope to ~/.gradle/gradle.properties:

gpr.user=YOUR_GITHUB_USERNAME
gpr.key=YOUR_GITHUB_TOKEN

Keep these credentials outside your project repository.

Add the SDK dependency to your application module's build.gradle.kts:

dependencies {
implementation("com.featurevisor:featurevisor-java:5.0.0")
}

Find the latest published version in Featurevisor Java packages.

Create an instance

Load a datafile, parse it and create a Featurevisor instance:

import com.featurevisor.sdk.DatafileContent
import com.featurevisor.sdk.Featurevisor
val datafileJson = "..." // load from your deployed datafile URL
val datafile = DatafileContent.fromJson(datafileJson)
val f = Featurevisor.createFeaturevisor(
Featurevisor.FeaturevisorOptions()
.datafile(datafile),
)

In an Android application, fetch and parse the datafile away from the main thread. Keep one Featurevisor instance for the appropriate application lifecycle instead of creating one for every evaluation.

Evaluate features

Kotlin calls the same flag, variation and variable methods as Java:

val context = mapOf<String, Any>(
"userId" to "user-4",
"country" to "nl",
)
val enabled: Boolean = f.isEnabled("checkout", context)
val variation: String? = f.getVariation("pricing", context)
val label: String? = f.getVariableString(
"buyButton",
"label",
context,
)
val email: String? = f.getVariableString(
"supportEmail",
context,
null,
)

For user-4, checkout is enabled, pricing gives control, the label is Nu kopen and the support address is [email protected]. A global variable is read with one key, and its context comes with an options argument, null when you have none.

On a device with one user, set the context on the instance once and leave it out of each call:

f.setContext(context)
val enabled = f.isEnabled("checkout")
val label = f.getVariableString("buyButton", "label")

Call close() when the application lifecycle that owns the instance ends:

f.close()

See the Java SDK documentation for the complete API, including typed variables, datafile updates, diagnostics, events, modules, sticky values and child instances. All of them work from Kotlin.

OpenFeature

Kotlin applications can use the same OpenFeature provider as Java through normal JVM interoperability. Add the provider artifact with the same version as the Java SDK:

dependencies {
implementation("com.featurevisor:featurevisor-openfeature:5.0.0")
}

The provider artifact includes the matching Featurevisor Java SDK and compatible official OpenFeature SDK dependencies.

import com.featurevisor.openfeature.FeaturevisorOpenFeatureProvider
import dev.openfeature.sdk.OpenFeatureAPI
val provider = FeaturevisorOpenFeatureProvider(f)
val api = OpenFeatureAPI.getInstance()
api.setProviderAndWait(provider)
val client = api.getClient()
val enabled = client.getBooleanValue("checkout", false)

Use checkout for a flag, pricing:variation for a variation, buyButton:label for a feature variable and variable:supportEmail for a global variable. The OpenFeature provider guide covers the shared key convention, result mapping, instance ownership and providers for other platforms.

Android example

The Featurevisor Android example is written entirely in Kotlin. It fetches a deployed datafile, creates the SDK instance, evaluates a flag, a variation and a variable, and closes the instance with the Android application lifecycle.

Previous
Java
Next
Swift