Skip to main content

Android SDK: Using Kotlin, SDK always returns the control treatment

Issue

When using Android App with Kotlin language, the code below always returns contro" treatment from Android SDK

val apiKey = "API KEY"
val config = SplitClientConfig.builder().enableDebug().build()
val matchingKey = "userxx"
val bucketKey = null
val key = Key(matchingKey, bucketKey)
val splitFactory = SplitFactoryBuilder.build(apiKey, key, config, applicationContext)
val splitClient = splitFactory.client()

splitClient.on(SplitEvent.SDK_READY, object : SplitEventTask() {
var treatment = splitClient.getTreatment("split-name")
})

Root Cause

While this code works fine using Swift language based Projects, in Kotlin the code does not listen to the SDK_READY event if used as is.

Solution

Based on the Advanced Section of Android SDK documentation, we can override onPostExecution function, which will be only called when the SDK_READY event fires.

splitClient.on(SplitEvent.SDK_READY, object : SplitEventTask() {
override fun onPostExecution(client: SplitClient) {
var treatment = splitClient.getTreatment("split-name")
}
})