This Provider is designed to enable the use of OpenFeature in Android, with Split as the backing feature flag & experimentation platform.
The Split OpenFeature Provider supports:
- Android API Level 21 (Android 5.0 Lollipop) and higher.
Below is a simple example that describes the instantiation of the Split Provider. Please see the OpenFeature Documentation for details on how to use the OpenFeature SDK.
Add the Split OpenFeature Provider dependency to your app's build.gradle.kts file:
dependencies {
implementation("io.split.openfeature:split-openfeature-android:1.1.0")
}Or for Groovy DSL (build.gradle):
dependencies {
implementation 'io.split.openfeature:split-openfeature-android:1.1.0'
}The Split OpenFeature Provider requires an Android Context and your Split SDK key. You must also provide an evaluation context with a targeting key when initializing the provider.
import dev.openfeature.kotlin.sdk.OpenFeatureAPI
import dev.openfeature.kotlin.sdk.ImmutableContext
import io.split.openfeature.android.provider.SplitProvider
// Create provider configuration
val config = SplitProvider.Config(
applicationContext = applicationContext,
sdkKey = "YOUR_SDK_KEY"
)
// Create the Split provider
val provider = SplitProvider(config = config)
// Set the provider with an initial context containing a targeting key
val initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)
// Get a client and evaluate flags
val client = OpenFeatureAPI.getClient()
val showNewFeature = client.getBooleanValue("new-feature", false)The Split OpenFeature Provider requires a targeting key to be set in the evaluation context. This key identifies the user or entity for which you are evaluating feature flags.
val initialContext = ImmutableContext(targetingKey = "user-123")
OpenFeatureAPI.setProvider(provider, initialContext = initialContext)val newContext = ImmutableContext(targetingKey = "user-456")
OpenFeatureAPI.setEvaluationContext(newContext)val context = ImmutableContext(
targetingKey = "user-123",
attributes = mapOf(
"email" to Value.String("user@example.com"),
"age" to Value.Integer(30),
"plan" to Value.String("premium")
)
)
val client = OpenFeatureAPI.getClient()
val result = client.getBooleanDetails("premium-feature", false, context)Split can bucket on a key other than the targeting key (Split "bucketing key"). Set the reserved bucketingKey attribute (a non-blank string) next to the targeting key; the provider then binds the Split client to Key(targetingKey, bucketingKey). The targeting key is still used for targeting rules, segments and individual targets, and track keeps using the targeting key only.
val context = ImmutableContext(
targetingKey = "user-123",
attributes = mapOf("bucketingKey" to Value.String("account-42"))
)
OpenFeatureAPI.setEvaluationContext(context)bucketingKeyrequires a targeting key: without one (or with an empty one) it is ignored with a warning and the current client is kept.- Changing only
bucketingKeyswitches to a new Split client, like changing the targeting key. Set it through the evaluation context (setProviderinitial context orsetEvaluationContext); an invocation context with a differentbucketingKeyreturnsPROVIDER_NOT_READY, like a different targeting key. bucketingKeyis reserved: it is not forwarded to Split as an attribute.- Leading/trailing whitespace is trimmed. A blank or non-string value is ignored with a warning and the targeting key is used alone. Warnings go to the Split SDK logger, so they are visible only with a Split log level of
WARNINGor lower (SplitClientConfig.builder().logLevel(...)). - The length is not checked by the provider: a value longer than 250 characters is rejected by the Split SDK, and evaluations return the default value (
control, error codeFLAG_NOT_FOUND).
The Split OpenFeature Provider emits events when the provider state changes. You can observe these events to react to provider readiness, configuration changes, or errors.
import dev.openfeature.kotlin.sdk.events.OpenFeatureProviderEvents
import kotlinx.coroutines.launch
// Observe provider events
lifecycleScope.launch {
provider.observe().collect { event ->
when (event) {
is OpenFeatureProviderEvents.ProviderReady -> {
// Provider is ready to evaluate flags
Log.d("Split", "Provider is ready")
}
is OpenFeatureProviderEvents.ProviderConfigurationChanged -> {
// Flag configuration has been updated
Log.d("Split", "Configuration changed")
}
is OpenFeatureProviderEvents.ProviderStale -> {
// Provider is serving cached data
Log.d("Split", "Provider is stale")
}
is OpenFeatureProviderEvents.ProviderError -> {
// An error occurred
Log.e("Split", "Provider error: ${event.error}")
}
}
}
}The Split OpenFeature Provider supports tracking events. To track events, you must set a trafficType in the evaluation context.
// Set context with trafficType
val context = ImmutableContext(targetingKey = "user-123")
.withTrafficType("user")
OpenFeatureAPI.setEvaluationContext(context)
// Track an event
val client = OpenFeatureAPI.getClient()
client.track("button_clicked")
// Track with a value
client.track(
"purchase_completed",
TrackingEventDetails(value = 99.99)
)
// Track with properties
client.track(
"page_viewed",
TrackingEventDetails(
structure = ImmutableStructure(
mapOf(
"page" to Value.String("home"),
"referrer" to Value.String("google")
)
)
)
)Please see Contributors Guide to find all you need to submit a Pull Request (PR).
Licensed under the Apache License, Version 2.0. See: Apache License.