Skip to main content

Android SDK Getting Started

Maven GitHub

Initializing the SDK

We recommend initializing the SDK once in onCreate of your Application class or MainActivity to receive features as soon as possible, and to pass around the client instance around in your app.

Using the builder pattern we can initialize the DevCycle SDK by providing the applicationContext, DevCycleUser, and DevCycle mobile SDK key:

Kotlin example:

override fun onCreate(savedInstanceState: Bundle?) {

...

// NOTE: It is not recommended to hardcode SDK keys into your application.
// Consider storing keys securely and reading from secure storage.

val devcycleClient: DevCycleClient = DevCycleClient.builder()
.withContext(applicationContext)
.withUser(
DevCycleUser.builder()
.withUserId("test_user")
.build()
)
.withSDKKey("<DEVCYCLE_MOBILE_SDK_KEY>")
.build()

...
}

Java example:

@Override
protected void onCreate(Bundle savedInstanceState) {

...

// NOTE: It is not recommended to hardcode SDK keys into your application.
// Consider storing keys securely and reading from secure storage.

DevCycleClient devcycleClient = DevCycleClient.builder()
.withContext(getApplicationContext())
.withUser(
DevCycleUser.builder()
.withUserId("test_user")
.build()
)
.withSDKKey("<DEVCYCLE_MOBILE_SDK_KEY>")
.build();

...
}

DevCycleClientBuilder

The DevCycleClient can be built using the following methods:

DevCycleClientBuilder class

MethodParameterDescription
withContextContextApp context
withSDKKeyStringDevCycle SDK Key
withUserDevCycleUserDevCycle user object
withOptionsDevCycleOptionsDevCycle options object
withLoggerTimber.TreeLogger override to replace default logger
withLogLevelLogLevelSet log level of the default logger. Defaults to LogLevel.ERROR

DevCycleUserBuilder

A DevCycleUser can be built using the following methods:

DevCycleUser Builder class

MethodParameterDescription
withUserIdStringUnique user ID
withIsAnonymousBooleanBoolean to indicate if the user is anonymous
withEmailStringUser's email
withNameStringUser's name
withCountryStringUser's country
withCustomDataMap<String, Any>Key/value map of properties to be used for targeting
withPrivateCustomDataMap<String, Any>Key/value map of properties to be used for targeting. Private properties will not be included in event logging.

DevCycleOptions Builder

The SDK exposes various initialization options which can be used by passing a DevCycleOptions object to the withOptions method of DevCycleClient.builder():

DevCycleOptions builder class

MethodParameterDefaultDescription
flushEventsIntervalMsLong10000Controls the interval between flushing events to the DevCycle servers in milliseconds, defaults to 10 seconds.
disableCustomEventLoggingBooleanfalseDisables logging of custom events generated by calling .track() method to DevCycle.
disableAutomaticEventLoggingBooleanfalseDisables logging of SDK generated events (e.g. variableEvaluated, variableDefaulted) to DevCycle.
enableEdgeDBBooleanfalseEnables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle.
configCacheTTLLong604800000The maximum allowed age of a cached config in milliseconds, defaults to 7 days
disableConfigCacheBooleanfalseDisable the use of cached configs
disableRealtimeUpdatesBooleanfalseDisable Realtime Updates
apiProxyURLStringnullAllows the SDK to communicate with a proxy of DevCycle Client SDK API.
eventsApiProxyURLStringnullAllows the SDK to communicate with a proxy of DevCycle Events API.

Notifying when DevCycle features are available

You can attach a callback on the client to determine when your features have been loaded:

Kotlin

devcycleClient.onInitialized(object : DevCycleCallback<String> {
override fun onSuccess(result: String) {
// successfully initialized
}

override fun onError(t: Throwable) {
// there was an error
}
})

Java

devcycleClient.onInitialized(new DevCycleCallback<String>() {
@Override
public void onSuccess(String result) {
// user configuration loaded successfully from DevCycle
}

@Override
public void onError(@NonNull Throwable t) {
// user configuration failed to load from DevCycle, default values will be used for Variables.
}
});