Android SDK
The Unleash Android SDK lets you evaluate feature flags in Android applications. It connects to Unleash or Unleash Edge to fetch evaluated flags for a given Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
Requirements
- Android API level 21 or higher (API level 23 recommended)
Installation
You will require the SDK on your classpath, so go ahead and add it to your dependency management file.
Gradle
Maven
Proguard
You shouldn’t have to configure proguard for this library, but if you do, you can use the following configuration:
Permissions
Your app will need internet permission in order to reach Unleash and access network state to be able to react to network changes. So in your manifest file add
Initialization
Configure your client instance (use a single instance to avoid file contention on cache directory).
You can pass event listeners either when constructing DefaultUnleash or when calling start(). By default the SDK uses delayed initialization (delayedInitialization = true). When delayed initialization is enabled, any listeners you pass to the constructor are stored and only registered when you call start(). This avoids emitting events before the SDK has finished its startup sequence. If you prefer to register and activate listeners immediately, set delayedInitialization to false in the UnleashConfig and the SDK will register constructor-provided listeners at construction time and start immediately.
Start Unleash
From the moment you call unleash.start(), it will immediately poll for the latest feature flags and then continue polling in the background.
Because this call is asynchronous, it will not block the main thread so you may want to check if the initial state is ready before proceeding.
You have a few options:
- Wait until Unleash is ready. This will make sure that your app is in sync with the feature toggles.
- Provide an initial state. This could make sense in cases where the network is not available and you want to provide a default state.
- None of the above. Unleash comes with a default behavior for feature flags with unknown state.
Check if the SDK is ready
You can check if the initial state is ready by checking unleash.isReady(). Normally, you would add an exit condition to avoid an infinite loop.
Alternatively, you can add an event listener to Unleash which is a more reactive approach. This way you can be notified when Unleash is ready:
Bootstrap
If you need to have a known state for your UnleashClient, you can provide the initial state as a list of toggles. This is useful when you have a known initial state for your feature toggles.
Alternatively, you can perform a query against the frontend API using your HTTP client of choice and save the output as a json file. Then you can tell Unleash to use this file to set up toggle states.
Default behavior
You can just start Unleash and it will use the default behavior for feature flags with unknown state until it has fetched the latest state from Unleash.
In some situations this could be a valid option. For example, if you don’t want the additional complexity of the option above and you are fine with the default behavior.
Check flags
After starting the Unleash instance, you can start using the feature toggles.
Events
This SDK exposes small focused listener interfaces. Below are short copy-paste examples showing how to register them.
- Register a heartbeat listener (fires on successful fetch, 304 and errors):
- Register a state listener (fires when in-memory state/cache changes):
- Ready listener (fires once when initial state arrives):
You can add or remove listeners at runtime with addUnleashEventListener/removeUnleashEventListener.
For a complete working example, see the sample app and its main activity, which demonstrates toggle state display and event listeners.
Default behavior
If you don’t provide an initial state, Unleash will use the default behavior for feature flags with unknown state. This means that if a feature flag is not found in the list of flags, it will be disabled by default.
Unleash context
The SDK uses UnleashContext as a Kotlin data class, so contexts are compared by value (all fields are compared). There are now two different fetch paths and it’s important to understand which one applies in each situation:
- Scheduled polling and manual “fetch” API:
refreshTogglesWithContext(internal fetch path) always attempts to fetch from upstream (subject to throttling and HTTP 304 handling). This is the path used by the scheduler so periodic polls will always check upstream for changes even when the context hasn’t changed. - Context-change-triggered fetch:
refreshTogglesIfContextChangedis used when the SDK is asked to update the context (setContext/setContextWithTimeout). That helper compares the new context to the last fetched context and will skip the network call if the values are identical.
Important details:
setContext— when called after the SDK has started, the SDK will attempt a context-change-triggered fetch viarefreshTogglesIfContextChanged. That helper will NOT perform a network request if the new context equals the last fetched context (prevents accidental redundant fetches when you update context with same values).setContextAsync— updates the internal context asynchronously without forcing an immediate fetch. The scheduled polling path still runs independently and will callrefreshTogglesWithContextperiodically to check upstream for changes.refreshTogglesNow/refreshTogglesNowAsync— these public methods force a fetch by callingrefreshTogglesand bypass the context equality check. They still respect throttling, so excessive forced fetches may be skipped due to throttling.