Android SDK Getting Started
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:
Method | Parameter | Description |
---|---|---|
withContext | Context | App context |
withSDKKey | String | DevCycle SDK Key |
withUser | DevCycleUser | DevCycle user object |
withOptions | DevCycleOptions | DevCycle options object |
withLogger | Timber.Tree | Logger override to replace default logger |
withLogLevel | LogLevel | Set log level of the default logger. Defaults to LogLevel.ERROR |
DevCycleUserBuilder
A DevCycleUser can be built using the following methods:
Method | Parameter | Description |
---|---|---|
withUserId | String | Unique user ID |
withIsAnonymous | Boolean | Boolean to indicate if the user is anonymous |
withEmail | String | User's email |
withName | String | User's name |
withCountry | String | User's country |
withCustomData | Map<String, Any> | Key/value map of properties to be used for targeting |
withPrivateCustomData | Map<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()
:
Method | Parameter | Default | Description |
---|---|---|---|
flushEventsIntervalMs | Long | 10000 | Controls the interval between flushing events to the DevCycle servers in milliseconds, defaults to 10 seconds. |
disableCustomEventLogging | Boolean | false | Disables logging of custom events generated by calling .track() method to DevCycle. |
disableAutomaticEventLogging | Boolean | false | Disables logging of SDK generated events (e.g. variableEvaluated, variableDefaulted) to DevCycle. |
enableEdgeDB | Boolean | false | Enables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle. |
configCacheTTL | Long | 604800000 | The maximum allowed age of a cached config in milliseconds, defaults to 7 days |
disableConfigCache | Boolean | false | Disable the use of cached configs |
disableRealtimeUpdates | Boolean | false | Disable Realtime Updates |
apiProxyURL | String | null | Allows the SDK to communicate with a proxy of DevCycle Client SDK API. |
eventsApiProxyURL | String | null | Allows 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.
}
});