Skip to content

Experiment Android SDK

Official documentation for Amplitude Experiment's Client-side Android SDK implementation.

SDK Resources

GitHub · Releases · API Reference

Install

Maven Central

Add to the dependencies in your Android project's build.gradle file.

dependencies {
  implementation 'com.amplitude:experiment-android-client:<VERSION>'
}

Quick Start

  1. Initialize the experiment client
  2. Fetch variants for the user
  3. Access a flag's variant
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        // (1) Initialize the experiment client
        ExperimentConfig config = new ExperimentConfig();
        ExperimentClient client = Experiment.initialize(this, "<DEPLOYMENT_KEY>", config);

        // (2) Fetch variants for a user
        ExperimentUser user = ExperimentUser.builder()
            .userId("user@company.com")
            .deviceId("abcdefg")
            .userProperty("premium", true)
            .build();
        try {
            client.fetch(user).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // (3) Lookup a flag's variant
        Variant variant = client.variant("<FLAG_KEY>");
        if (variant.is("on")) {
            // Flag is on
        } else {
            // Flag is off
        }
    }
}
class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        // (1) Initialize the experiment client
        val config = ExperimentConfig()
        val client = initialize(this, "<DEPLOYMENT_KEY>", config)

        // (2) Fetch variants for a user
        val user = builder()
            .userId("user@company.com")
            .deviceId("abcdefg")
            .userProperty("premium", true)
            .build()
        try {
            client.fetch(user).get()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        // (3) Lookup a flag's variant
        val variant = client.variant("<FLAG_KEY>")
        if (variant.value == "on") {
            // Flag is on
        } else {
            // Flag is off
        }
    }
}

Not getting the expected variant result for your flag? Make sure your flag is activated, has a deployment set, and has users allocated.

Core functions

The following functions make up the core of the Experiment client-side SDK.


Initialize

The SDK client should be initialized in your application on startup. The deployment key argument passed into the apiKey parameter must live within the same project that you are sending analytics events to.

fun initialize(application: Application, apiKey: String, config: ExperimentConfig)
Parameter
Requirement Description
application required The Android Application context. Used to persist variants across sessions.
apiKey required The deployment key which authorizes fetch requests and determines which flags should be evaluated for the user.
config optional The client configuration used to customize SDK client behavior.

The initializer returns a singleton instance, so subsequent initializations for the same instance name will always return the initial instance. To create multiple instances, use the instanceName configuration.

ExperimentClient experiment = Experiment.initialize(
    context, "<DEPLOYMENT_KEY>", new ExperimentConfig());
val experiment = Experiment.initialize(context, "<DEPLOYMENT_KEY>", ExperimentConfig())

Integrations

If you use either Amplitude or Segment Analytics SDKs to track events into Amplitude, you'll want to set up an integration on initialization. Integrations automatically implement provider interfaces to enable a more streamlined developer experience by making it easier to manage user identity and track exposures events.

Amplitude Integration

The Amplitude Experiment SDK is set up to integrate seamlessly with the Amplitude Analytics SDK. All you need to do is update your SDK versions to the latest, and use the special integration initialization function.

Amplitude.getInstance().init("<API_KEY>");
ExperimentClient experiment = Experiment.initializeWithAmplitudeAnalytics(
    context, "<DEPLOYMENT_KEY>", new ExperimentConfig());
Amplitude.getInstance().init("<API_KEY>")
val experiment = Experiment.initializeWithAmplitudeAnalytics(
    context,
    "<DEPLOYMENT_KEY>",
    ExperimentConfig(),
)

Note that, if you are using a custom instance name for analytics, you will need to set the same value in the instanceName configuration option in the experiment SDK.

Using the integration initializer will automatically configure implementations of the user provider and exposure tracking provider interfaces to pull user data from the Amplitude Analytics SDK and track exposure events.

Supported Versions

Analytics SDK Version Experiment SDK Version
2.36.0+ 1.5.1+
Segment Integration

Experiment's integration with Segment Analytics is still a manual implementation at this point. Copy the exposure tracking provider implementation into your app code base and initialize the Experiment SDK with the provider instances in the configuration.

SegmentExposureTrackingProvider.java
class SegmentExposureTrackingProvider implements ExposureTrackingProvider {
    private Analytics analytics;
    public SegmentExposureTrackingProvider(Analytics analytics) {
        this.analytics = analytics;
    }
    @Override
    public void track(Exposure exposure) {
        analytics.track(
                "$exposure",
                new Properties()
                    .putValue("flag_key", exposure.flagKey)
                    .putValue("variant", exposure.variant));
    }
}
class SegmentExposureTrackingProvider(
    private val analytics: Analytics
): ExposureTrackingProvider {

    override fun track(exposure: Exposure) {
        analytics.track(
            "\$exposure",
            Properties()
                .putValue("flag_key", exposure.flagKey)
                .putValue("variant", exposure.variant),
        )
    }
}

The Experiment SDK must then be configured on initialization with an instance of the the exposure tracking provider.

Analytics analytics = // Initialize segment analytics
ExperimentConfig config = ExperimentConfig.builder()
    .exposureTrackingProvider(new SegmentExposureTrackingProvider(analytics))
    .build();
ExperimentClient experiment = Experiment.initialize(context, "<DEPLOYMENT_KEY>", config);
val analytics = // Initialize segment analytics
val config = ExperimentConfig.builder()
    .exposureTrackingProvider(SegmentExposureTrackingProvider(analytics))
    .build()
val experiment = Experiment.initialize(context, "<DEPLOYMENT_KEY>", config)

When fetching variants, pass the segment anonymous ID and user ID for the device ID and user ID, respectively.

String userId = analytics.getAnalyticsContext().traits().userId();
String deviceId = analytics.getAnalyticsContext().traits().anonymousId();
try {
    ExperimentUser user = ExperimentUser.builder()
        .userId(userId)
        .deviceId(deviceId)
        .build();
    experiment.fetch(user).get();
} catch (Exception e) {
    e.printStackTrace();
}
val userId = analytics.analyticsContext.traits().userId()
val deviceId = analytics.analyticsContext.traits().anonymousId()
try {
    val user = ExperimentUser.builder()
        .userId(userId)
        .deviceId(deviceId)
        .build()
    experiment.fetch(user).get()
} catch (e: Exception) {
    e.printStackTrace()
}

Configuration

The SDK client can be configured once on initialization.

Configuration Options
Name
Description Default Value
debug Enable additional debug logging within the SDK. Should be set to false in production builds. false
fallbackVariant The default variant to fall back if a variant for the provided key doesn't exist. {}
initialVariants An initial set of variants to access. This field is valuable for bootstrapping the client SDK with values rendered by the server using server-side rendering (SSR). {}
serverUrl The host to fetch variants from. https://api.lab.amplitude.com
fetchTimeoutMillis The timeout for fetching variants in milliseconds. 10000
retryFetchOnFailure Whether to retry variant fetches in the background if the request doesn't succeed. true
automaticExposureTracking If true, calling variant() will track an exposure event through the configured exposureTrackingProvider. If no exposure tracking provider is set, this configuration option does nothing. true
automaticFetchOnAmplitudeIdentityChange Only matters if you use the initializeWithAmplitudeAnalytics initialization function to seamlessly integrate with the Amplitude Analytics SDK. If true any change to the user ID, device ID or user properties from analytics will trigger the experiment SDK to fetch variants and update it's cache. false
userProvider An interface used to provide the user object to fetch() when called. See Experiment User for more information. null
exposureTrackingProvider Implement and configure this interface to track exposure events through the experiment SDK, either automatically or explicitly. null
instanceName Custom instance name for experiment SDK instance. The value of this field is case-sensitive. null

EU Data Center

If you're using Amplitude's EU data center, configure the serverUrl option on initialization to https://api.lab.eu.amplitude.com

Fetch

Fetches variants for a user and store the results in the client for fast access. This function remote evaluates the user for flags associated with the deployment used to initialize the SDK client.

fun fetch(user: ExperimentUser? = null, options: FetchOptions? = null): Future<ExperimentClient>
Parameter Requirement Description
user optional Explicit user information to pass with the request to evaluate. This user information is merged with user information provided from integrations via the user provider, preferring properties passed explicitly to fetch() over provided properties.
options optional Explicit flag keys to fetch.

Amplitude Experiment recommends calling fetch() during application start up so that the user gets the most up-to-date variants for the application session. Furthermore, you'll need to wait for the fetch request to return a result before rendering the user experience to avoid the interface "flickering".

try {
    ExperimentUser user = ExperimentUser.builder()
        .userId("user@company.com")
        .userProperty("premium", true)
        .build();
    experiment.fetch(user).get();
} catch (Exception e) {
    e.printStackTrace();
}
try {
    val user = ExperimentUser.builder()
        .userId("user@company.com")
        .userProperty("premium", true)
        .build()
    experiment.fetch(user).get()
} catch (e: Exception) {
    e.printStackTrace()
}

If you're using an integration or a custom user provider then you can fetch without inputting the user.

experiment.fetch(null);
experiment.fetch()
Fetch When User Identity Changes

If you want the most up-to-date variants for the user, it's recommended that you call fetch() whenever the user state changes in a meaningful way. For example, if the user logs in and receives a user ID, or has a user property set which may effect flag or experiment targeting rules.

In the case of user properties, Amplitude recommends passing new user properties explicitly to fetch() instead of relying on user enrichment prior to remote evaluation. This is because user properties that are synced remotely through a separate system have no timing guarantees with respect to fetch()--i.e. a race.

Timeout & Retries

If fetch() times out (default 10 seconds) or fails for any reason, the SDK client will return and retry in the background with back-off. You may configure the timeout or disable retries in the configuration options when the SDK client is initialized.


Variant

Access a variant for a flag or experiment from the SDK client's local store.

Automatic Exposure Tracking

When an integration is used or a custom exposure tracking provider is set, variant() will automatically track an exposure event through the tracking provider. To disable this functionality, configure automaticExposureTracking to be false, and track exposures manually using exposure().

fun variant(key: String, fallback: Variant? = null): Variant
Parameter Requirement Description
key required The flag key to identify the flag or experiment to access the variant for.
fallback optional The value to return if no variant was found for the given flagKey.

When determining which variant a user has been bucketed into, you'll want to compare the variant value to a well-known string.

Variant variant = client.variant("<FLAG_KEY>");
if (variant.is("on")) {
    // Flag is on
} else {
    // Flag is off
}
Variant variant = client.variant("<FLAG_KEY>");
if (variant.value == "on") {
    // Flag is on
} else {
    // Flag is off
}
Accessing the variant's payload

A variant may also be configured with a dynamic payload of arbitrary data. Access the payload field from the variant object after checking the variant's value.

The payload on Android is of type Object (Any?) meaning you will need to cast the payload to the expected type. JSON object and array types need to be cast as org.json.JSONObject and org.json.JSONArray respectively.

For example, if the payload is {"key":"value"}:

Variant variant = experiment.variant("<FLAG_KEY>");
if (variant.is("on") && variant.payload != null) {
    try {
        String value = ((JSONObject) variant.payload).getString("key");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
val variant = experiment.variant("<FLAG_KEY>")
if (variant.value == "on") {
    try {
        val value = (variant.payload as JSONObject).getString("key")
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

A null variant value means that the user hasn't been bucketed into a variant. You may use the built in fallback parameter to provide a variant to return if the store doesn't contain a variant for the given flag key.

Variant variant = experiment.variant("<FLAG_KEY>", new Variant("control"));
if (variant.is("control")) {
    // Control
} else if (variant.is("treatment")) {
    // Treatment
}
val variant = experiment.variant("<FLAG_KEY>", Variant("control"))
if (variant.value == "control") {
    // Control
} else if (variant.value == "treatment") {
    // Treatment
}

All

Access all variants stored by the SDK client.

fun all(): Map<String, Variant>

Clear

Clear all variants in the cache and storage.

fun clear()

You can call clear after user logout to clear the variants in cache and storage.

experiment.clear();
experiment.clear()

Exposure

Manually track an exposure event for the current variant of the given flag key through configured integration or custom exposure tracking provider. Generally used in conjunction with setting the automaticExposureTracking configuration optional to false.

fun exposure(key: String)
Parameter Requirement Description
key required The flag key to identify the flag or experiment variant to track an exposure event for.
Variant variant = experiment.variant("<FLAG_KEY>");

// Do other things...

experiment.exposure("<FLAG_KEY>");
if (variant.is("control")) {
    // Control
} else if (variant.is("treatment")) {
    // Treatment
}
val variant = experiment.variant("<FLAG_KEY>")

// Do other things...

experiment.exposure("<FLAG_KEY>")
if (variant.value == "control") {
    // Control
} else if (variant.value == "treatment") {
    // Treatment
}

Providers

Integrations

If you use Amplitude or Segment analytics SDKs along side the Experiment Client SDK, Amplitude recommends you use an integration instead of implementing custom providers.

Provider implementations enable a more streamlined developer experience by making it easier to manage user identity and track exposures events.

User provider

The user provider is used by the SDK client to access the most up-to-date user information only when it's needed (for example, when fetch() is called). This provider is optional, but helps if you have a user information store already set up in your application. This way, you don't need to manage two separate user info stores in parallel, which may result in a divergent user state if the application user store is updated and experiment isn't (or vice versa).

ExperimentUserProvider
interface ExperimentUserProvider {
    fun getUser(): ExperimentUser
}

To use your custom user provider, set the userProvider configuration option with an instance of your custom implementation on SDK initialization.

ExperimentConfig config = ExperimentConfig.builder()
    .userProvider(new CustomUserProvider())
    .build();
ExperimentClient experiment = Experiment.initialize(
    context, "<DEPLOYMENT_KEY>", config);
val config = ExperimentConfig.builder()
    .userProvider(CustomUserProvider())
    .build()
val experiment = Experiment.initialize(context, "<DEPLOYMENT_KEY>", config)

Exposure tracking provider

Implementing an exposure tracking provider is highly recommended. Exposure tracking increases the accuracy and reliability of experiment results and improves visibility into which flags and experiments a user is exposed to.

ExposureTrackingProvider
interface ExposureTrackingProvider {
    fun track(exposure: Exposure)
}

The implementation of track() should track an event of type $exposure (a.k.a name) with two event properties, flag_key and variant, corresponding to the two fields on the Exposure object argument. Finally, the event tracked must eventually end up in Amplitude Analytics for the same project that the [deployment] used to initialize the SDK client lives within, and for the same user that variants were fetched for.

To use your custom user provider, set the exposureTrackingProvider configuration option with an instance of your custom implementation on SDK initialization.

ExperimentConfig config = ExperimentConfig.builder()
    .exposureTrackingProvider(new CustomExposureTrackingProvider())
    .build();
ExperimentClient experiment = Experiment.initialize(
    context, "<DEPLOYMENT_KEY>", config);
val config = ExperimentConfig.builder()
    .exposureTrackingProvider(CustomExposureTrackingProvider())
    .build()
val experiment = Experiment.initialize(context, "<DEPLOYMENT_KEY>", config)

Was this page helpful?