---
title: Example usage of the Android SDK
slug: feature-experimentation/docs/example-usage-android
docTags: 
createdAt: 2026-08-05T20:40:03.000Z
---

After installing the Android SDK, import the Optimizely library, get your project's datafile, and create a client. Use the client to evaluate flag rules like A/B tests and flag deliveries.

This example walks through the following three key steps:

1. Evaluate a flag with the key `product_sort` using the `decide` method. This also sends a decision event to Optimizely to record that the user was exposed to the experiment.
2. Run code based on the flag result. The SDK evaluates your flag rules and determines which variation the user is in. You can either:
   - Check the flag's enabled state and read a configuration variable (`sort_method`) to determine which experience the user gets.
   - Check the flag variation directly and run the corresponding control or treatment code.
3. Track a conversion event called `purchased` to measure the experiment's impact. The `trackEvent` method ties the purchase back to the A/B test and sends it to Optimizely so it displays on your [Experiment Results page](docId:_K6n7h_MZ8b0xn6yWPrrq).

```kotlin
// Build a manager
val optimizelyManager = OptimizelyManager.builder()
        .withSDKKey("<Your_SDK_Key>")
        .build(context)
// Instantiate a client synchronously with a bundled datafile
// copy datafile JSON from URL accessible in app>settings
val datafile = "REPLACE_WITH_YOUR_DATAFILE"
val optimizelyClient = optimizelyManager.initialize(context, datafile)

// Create a user-context
val attributes: MutableMap<String, Any> = HashMap()
attributes["logged_in"] = true
val user = optimizelyClient.createUserContext("user123", attributes)!!

// Call the decide method
val decision = user.decide("product_sort")

// did the decision fail with a critical error?
val variationKey = decision.variationKey
if (variationKey == null) {
   val reasons = decision.reasons
   Log.d("Optimizely", "decision error: $reasons")
}

// execute code based on flag enabled state
val enabled = decision.enabled
val variables = decision.variables
if (enabled) {
   var vs: String? = null
   try {
      vs = variables.getValue("sort_method", String::class.java)
   } catch (e: JsonParseException) {
      e.printStackTrace()
   }
}

// or execute code based on flag variation:
if (variationKey == "control") {
   // Execute code for control variation
} else if (variationKey == "treatment") {
   // Execute code for treatment variation
}

// Track an event
user.trackEvent("purchased")
```

```java
// Build a manager
OptimizelyManager optimizelyManager = OptimizelyManager.builder()
       .withSDKKey("<Your_SDK_Key>")
       .build(context);
// Instantiate a client synchronously with a bundled datafile
// copy datafile JSON from URL accessible in app>settings
String datafile = "REPLACE_WITH_YOUR_DATAFILE";
OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, datafile);

// Create a user-context
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);

// Call the decide method
OptimizelyDecision decision = user.decide("product_sort");

// did the decision fail with a critical error?
String variationKey = decision.getVariationKey();
if (variationKey == null) {
   List<String> reasons = decision.getReasons();
   Log.d("Optimizely", "decision error: " + reasons);
}

// execute code based on flag enabled state
boolean enabled = decision.getEnabled();
OptimizelyJSON variables = decision.getVariables();
if (enabled) {
   String vs = null;
   try {
      vs = variables.getValue("sort_method", String.class);
   } catch (JsonParseException e) {
      e.printStackTrace();
   }
}

// or execute code based on flag variation:
if ("control".equals(variationKey)) {
   // Execute code for control variation
} else if ("treatment".equals(variationKey)) {
   // Execute code for treatment variation
}

// Track an event
user.trackEvent("purchased");
```
