# Variation Distribution

When running an A/B Test, you need to distribute users across variations and write logic for each variation. You can use the Hackle SDK to handle this distribution.

## variation

Pass the **Experiment Key** to the `variation()` method to distribute the user and receive the result.

<table><thead><tr><th width="150">Parameter</th><th width="120">Type</th><th>Required</th></tr></thead><tbody><tr><td>Experiment Key (key)</td><td><code>int</code></td><td>Required</td></tr></tbody></table>

#### Example

The example code below passes Experiment Key 42, where there are two variations: A and B.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.android.HackleApp
import io.hackle.sdk.common.Variation

// 실험 키가 42인 A/B 테스트에서 사용자에게 노출할 테스트 그룹을 결정합니다.
// 결정하지 못하는 상황인 경우 테스트 그룹 A를 반환합니다.
val variation = hackleApp.variation(42)

// 할당받은 그룹에 대한 로직
if (variation == Variation.A) {
  // 그룹 A 로직
} else if (variation == Variation.B) {
  // 그룹 B 로직
}
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.android.HackleApp;
import io.hackle.sdk.common.Variation;

// 실험 키가 42인 A/B 테스트에서 사용자에게 노출할 테스트 그룹을 결정합니다.
// 결정하지 못하는 상황인 경우 테스트 그룹 A를 반환합니다.
Variation variation = hackleApp.variation(42);

// 할당받은 그룹에 대한 로직
if (variation == Variation.A) {
  // 그룹 A 로직
} else if (variation == Variation.B) {
  // 그룹 B 로직
}
```

{% endtab %}
{% endtabs %}

## variationDetail

The `variationDetail()` method works the same as `variation()` but also provides the reason for the distribution decision. This method is useful for verifying that distribution is working correctly.

<table><thead><tr><th width="150">Parameter</th><th width="120">Type</th><th>Required</th></tr></thead><tbody><tr><td>Experiment Key (key)</td><td><code>int</code></td><td>Required</td></tr></tbody></table>

#### Example

The example code below passes Experiment Key 42.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.android.HackleApp
import io.hackle.sdk.common.Variation
import io.hackle.sdk.common.decision.Decision
import io.hackle.sdk.common.decision.DecisionReason

// 분배 결정 상세
val decision = hackleApp.variationDetail(42)

// 분배 그룹
val variation = decision.variation

// 분배 결정 사유
val reason = decision.reason
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.android.HackleApp;
import io.hackle.sdk.common.Variation;
import io.hackle.sdk.common.decision.Decision;
import io.hackle.sdk.common.decision.DecisionReason;

// 분배 결정 상세
Decision decision = hackleApp.variationDetail(42);

// 분배 그룹
Variation variation = decision.getVariation();

// 분배 결정 사유
DecisionReason reason = decision.getReason();
```

{% endtab %}
{% endtabs %}

### Distribution Reason

The distribution reason is returned in the format **`SDK_NOT_READY`**. See the table below for details.

<table><thead><tr><th width="319.12109375">Distribution Reason</th><th width="305.35546875">Description</th><th>Distribution Result</th></tr></thead><tbody><tr><td><code>SDK_NOT_READY</code></td><td><p>The SDK is not ready to use.</p><p>(e.g., initialization attempted with an invalid SDK Key)</p></td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_NOT_FOUND</code></td><td>No A/B Test was found for the provided Experiment Key. The Experiment Key may be incorrect, or the experiment may be archived.</td><td>A (Control Group)</td></tr><tr><td><code>NOT_IN_MUTUAL_EXCLUSION_EXPERIMENT</code></td><td>The experiment is included in Mutually Exclusive Settings, but<br>the user was not assigned to that Mutually Exclusive Group.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_DRAFT</code></td><td>The A/B Test is in draft state.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_PAUSED</code></td><td>The A/B Test is paused.</td><td>A (Control Group)</td></tr><tr><td><code>EXPERIMENT_COMPLETED</code></td><td>The A/B Test has ended.</td><td>Winning Variation selected at completion</td></tr><tr><td><code>OVERRIDDEN</code></td><td>The user was assigned to a specific group<br>by Override.</td><td>Override-assigned<br>group</td></tr><tr><td><code>NOT_IN_EXPERIMENT_TARGET</code></td><td>The user is not an A/B Test target.</td><td>A (Control Group)</td></tr><tr><td><code>TRAFFIC_NOT_ALLOCATED</code></td><td>The A/B Test is running, but<br>the user was not allocated to the test.</td><td>A (Control Group)</td></tr><tr><td><code>TRAFFIC_ALLOCATED</code></td><td>The user has been allocated to the A/B Test.</td><td>Allocated group</td></tr><tr><td><code>VARIATION_DROPPED</code></td><td>The originally allocated group has been removed from the test.</td><td>A (Control Group)</td></tr><tr><td><code>INVALID_INPUT</code></td><td>The input value is invalid.</td><td>A (Control Group)</td></tr><tr><td><code>EXCEPTION</code></td><td>An unknown error occurred.</td><td>A (Control Group)</td></tr></tbody></table>

### Parameters

{% hint style="info" %}
This feature is supported in Android SDK 2.9.0 and above.
{% endhint %}

* The `variationDetail()` method also returns the parameter values for the assigned variation.
* The `Decision` instance returned by `variationDetail()` contains a `ParameterConfig` object with all parameter configuration.
* Since parameters set on the Hackle A/B Test screen exist as key-value pairs, you can use the methods below to retrieve parameter values according to the configured parameter type.
* You can query the configured values for the assigned variation using the functions below.

<table><thead><tr><th width="150">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>getString</code></td><td>Returns a parameter value configured as STRING or JSON type.</td></tr><tr><td><code>getInt</code></td><td>Returns a parameter value configured as NUMBER type as an int.</td></tr><tr><td><code>getDouble</code></td><td>Returns a parameter value configured as NUMBER type as a double.</td></tr><tr><td><code>getLong</code></td><td>Returns a parameter value configured as NUMBER type as a long.</td></tr><tr><td><code>getBoolean</code></td><td>Returns a parameter value configured as Boolean type.</td></tr></tbody></table>

#### Example

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
import io.hackle.android.HackleApp
import io.hackle.sdk.common.decision.Decision
import io.hackle.sdk.common.ParameterConfig

val decision: Decision = hackleApp.variationDetail(42)
val config: ParameterConfig = decision.config

val strValue: String = decision.getString("parameter_key_string_type", "defaultValue")
val strValueInConfig: String = config.getString("parameter_key_string_type", "defaultValue")

val jsonValue: String = decision.getString("parameter_key_json_type", "defaultValue")
val jsonValueInConfig: String = config.getString("parameter_key_json_type", "defaultValue")

val intValue: Int = decision.getInt("parameter_key_number_type", 0)
val intValueInConfig: Int = config.getInt("parameter_key_number_type", 0)

val doubleValue: Double = decision.getDouble("parameter_key_number_type", 0.0)
val doubleValueInConfig: Double = config.getDouble("parameter_key_number_type", 0.0)

val longValue: Long = decision.getLong("parameter_key_number_type", 0L)
val longValueInConfig: Long = config.getLong("parameter_key_number_type", 0L)

val booleanValue: Boolean = decision.getBoolean("parameter_key_boolean_type", false)
val booleanValueInConfig: Boolean = config.getBoolean("parameter_key_boolean_type", false)
```

{% endtab %}

{% tab title="Java" %}

```java
import io.hackle.android.HackleApp
import io.hackle.sdk.common.decision.Decision
import io.hackle.sdk.common.ParameterConfig

Decision decision = hackleApp.variationDetail(42);
ParameterConfig config = decision.getConfig();

String strValue = decision.getString("parameter_key_string_type", "defaultValue");
String strValueInConfig = config.getString("parameter_key_string_type", "defaultValue");

String jsonValue = decision.getString("parameter_key_json_type", "defaultValue");
String jsonValueInConfig = config.getString("parameter_key_json_type", "defaultValue");

int intValue: Int = decision.getInt("parameter_key_number_type", 0)
int intValueInConfig: Int = config.getInt("parameter_key_number_type", 0)

double doubleValue: Double = decision.getDouble("parameter_key_number_type", 0.0)
double doubleValueInConfig: Double = config.getDouble("parameter_key_number_type", 0.0)

long longValue: Long = decision.getLong("parameter_key_number_type", 0L)
long longValueInConfig: Long = config.getLong("parameter_key_number_type", 0L)

boolean booleanValue: Boolean = decision.getBoolean("parameter_key_boolean_type", false)
boolean booleanValueInConfig: Boolean = config.getBoolean("parameter_key_boolean_type", false)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hackle.io/en/development-guide/android/android-variation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
