Launch Darkly Android SDK version migration 3.2.2 to 5.0

Manikandan K
2 min readNov 29, 2023

Android SDK 3.x to 4.0 migration :

  1. The use of LDUser is deprecated and in Version 4 of the Android SDK.
  2. LDUser are replaced with LDContext
val user: LDUser = LDUser("user-key-123abc")

TO

val context: LDContext = LDContext.create("user-key-123abc")

In previous SDK versions, the user object included several built-in attributes for describing the user. It also included optional custom attributes, which you could add to a custom object within the user object and then populate.

In version 4.0, the only built-in attributes are kind, key, name, and anonymous. Kind, key, and name are strings, and anonymous is a boolean.

val ldUser = LDUser.Builder("user-key-123abc")
.custom("app_version", "appVersion")
.custom("app_buildnumber", "1.0")
.custom("platform", "mobile")
.custom("platform-os", "Android")
.build()

to

LDContext context = LDContext.builder("user-key-123abc")
.set("app_version", "appVersion")
.set("app_buildnumber", "1.0")
.set("platform", "mobile")
.set("platform-os", "Android")
.build();

3. Updated the data source methods:
For each data source type, there is a factory method whose name ends in DataSource

// Setting custom options for streaming mode
LDConfig config = new LDConfig.Builder()
.stream(true)
.backgroundPollingIntervalMillis(120000)
.build();

To

// Setting custom options for streaming mode
LDConfig config = new LDConfig.Builder()
.dataSource(
Components.streamingDataSource()
)
.build();

4. It’s use the LDConfig builder’s .applicationInfo method to set the application ID and application version.

val ldConfig: LDConfig = LDConfig.Builder()
.mobileKey(config.launchDarklyKey)
.dataSource(Components.streamingDataSource())
.applicationInfo(
Components.applicationInfo()
.applicationId("authentication-service")
.applicationName("Authentication-Service")
)
.build()

Android SDK 4.x to 5.0 migration :

  1. LaunchDarkly should automatically provide data about the mobile environment where the application is running as part of each evaluation context. You must include this parameter when building your LDConfig
val ldConfig: LDConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)    
.mobileKey("mobile-key-123abc")
.build()

2. Starting with version 5, you can also set the application name and version name.

val ldConfig: LDConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
.applicationInfo(Components.applicationInfo()
.applicationId("authentication-service")
.applicationName("Authentication-Service")
.applicationVersion("1.0")
.applicationVersionName("v1")
.build()

3. Version 5.0 removes the deprecated LDUser. Version 4 of the Android SDK replaced users with contexts. Starting in version 5, the deprecated LDUser is removed.

LDUser user = new LDUser("user-key-123abc");

to

LDContext context = LDContext.create("context-key-123abc");

To learn more about : https://docs.launchdarkly.com/sdk/client-side/android

--

--

Manikandan K

Android app developer with 7+ years of experience building clear, high-quality apps and known for producing code that is efficient, maintainable and reusable.