---
metadata:
  - name: generator
    content: Diplodoc Platform v5.55.3
alternate:
  - https://yandex.com/support/varioqub-app/en/sdk/android/integration.md
  - https://yandex.com/support/varioqub-app/ru/sdk/android/integration.md
  - href: https://yandex.com/support/varioqub-app/en/sdk/android/integration.md
    type: text/markdown
    title: Markdown version
  - href: https://yandex.com/support/varioqub-app/en/llms.txt?revision=r20853974
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/support/varioqub-app/en/llms.txt?revision=r20853974

# SDK integration

To use the Varioqub SDK, add and initialize the AppMetrica Android library. For more information on how to do this, see the [AppMetrica SDK documentation](https://appmetrica.yandex.com/docs/en/sdk/android/analytics/quick-start).

**App requirements:**

- Android version 5 or later.
- Android Studio 2021 or later.
- Make sure that your app's build file has the following values:

    - minSdkVersion 21 or later;
    - compileSdkVersion 31 or later.

## General integration recommendations

1. When starting a new session, call the `activateConfig()` method to get the stored flag values.

2. Run `fetchConfig` in the background to export the new flag values from the server and activate them when the next session starts.

3. We don't recommend calling the `activateConfig()` method mid-session because it can cause inconsistent app behavior.

## Step 1. Add the library to your project

To integrate the Varioqub SDK, add the following dependencies to the module-level Gradle file of your app (usually _app/build.gradle.kts_ or _app/build.gradle_):

{% list tabs %}

- build.gradle.kts

  ```kotlin
  dependencies {
      implementation("com.yandex.varioqub:config:0.7.0")
      implementation("com.yandex.varioqub:appmetrica-adapter:0.7.0")
      implementation("io.appmetrica.analytics:analytics:7.10.0")
  }
  ```

- build.gradle

  ```groovy
  dependencies {
      implementation 'com.yandex.varioqub:config:0.7.0'
      implementation 'com.yandex.varioqub:appmetrica-adapter:0.7.0'
      implementation 'io.appmetrica.analytics:analytics:7.10.0'
  }
  ```

{% endlist %}

## Step 2. Initialize the library {#shag-2-inicializirujte-biblioteku}

To initialize the library, use the `init(settings: VarioqubSettings, adapter: VarioqubConfigAdapter, context: Context)` method. It takes the following parameters:

- `settings: VarioqubSettings`. It contains required and optional settings.

    In the settings, specify your project ID as `appmetrica.XXXXXX`, where _XXXXXX_ is the app ID from the AppMetrica interface. For example, `VarioqubSettings.Builder("appmetrica.1234567")`.

    <!-- source: en/sdk/_includes/application-id.md -->
    {% note tip %}

    You can find the app ID on the **Settings** page in AppMetrica: copy it from **General settings** → **App ID**.

    {% endnote %}
    <!-- endsource: en/sdk/_includes/application-id.md -->

    You can also pass a list of custom parameters with `withClientFeature`. For example, you can pass a flag indicating if the user is subscribed to newsletters.

- `adapter: VarioqubConfigAdapter`. Adapter for collecting statistics and sending them to an analytics system, such as AppMetrica. If you don't want to collect statistics, use a [null adapter](https://yandex.com/support/varioqub-app/en/sdk/android/nullconnect.md).

- `context: Context`.

{% list tabs %}

- Kotlin

  ```kotlin
  import com.yandex.varioqub.appmetricaadapter.AppMetricaAdapter
  import com.yandex.varioqub.config.FetchError
  import com.yandex.varioqub.config.OnFetchCompleteListener
  import com.yandex.varioqub.config.Varioqub
  import com.yandex.varioqub.config.VarioqubSettings

  class YourApplication : Application() {
      override fun onCreate() {
          super.onCreate()

          val settings = VarioqubSettings.Builder("appmetrica.1234567")
              .withClientFeature("my_cool_feature", "true")
              .build()

          Varioqub.init(settings, AppMetricaAdapter(this), this)
      }
  }
  ```

- Java

  ```java
  import com.yandex.varioqub.appmetricaadapter.AppMetricaAdapter;
  import com.yandex.varioqub.config.FetchError;
  import com.yandex.varioqub.config.OnFetchCompleteListener;
  import com.yandex.varioqub.config.Varioqub;
  import com.yandex.varioqub.config.VarioqubSettings;

  public class YourApplication extends Application {
      @Override
      public void onCreate() {
          super.onCreate();

          VarioqubSettings settings = new VarioqubSettings.Builder("appmetrica.1234567")
              .withClientFeature("my_cool_feature", "true")
              .build();

          Varioqub.init(settings, new AppMetricaAdapter(this), this);
      }
  }
  ```

{% endlist %}

### VarioqubSettings parameters

Contains required and optional settings:

- `VarioqubSettings.Builder("client_id")`: Builder with a project ID specified as `appmetrica.XXXXXX`, where _XXXXXX_ is the app ID from the AppMetrica interface. For example, `VarioqubSettings.Builder("appmetrica.1234567")`.

    <!-- source: en/sdk/_includes/application-id.md -->
    {% note tip %}

    You can find the app ID on the **Settings** page in AppMetrica: copy it from **General settings** → **App ID**.

    {% endnote %}
    <!-- endsource: en/sdk/_includes/application-id.md -->

- `withClientFeature("myCoolFeature", "LovelyFeatureValue")`: Function containing a list of custom parameters. For example, you can pass a flag indicating if the user is subscribed to newsletters.

- `withThrottleInterval(2)`: Limit on the config refresh frequency in seconds. This is only recommended for testing purposes.

- `withUrl(url: String)`: Changes the URL for server responses. Only used for testing.

- `withLogs()`: Enables internal logging. To help us diagnose your problem, please enable logging and attach the log file with a description of the problem when submitting your support request.

- `withActivateEvent(enabled: Boolean)`: Sends an event when the flag configuration is activated. Event sending is enabled by default.

{% list tabs %}

- Kotlin

  Single custom parameter

  ```kotlin
  val settings = VarioqubSettings.Builder("appmetrica.1234567")
      .withClientFeature("my_cool_feature", "true")
      .withThrottleInterval(2)
      .build()
  ```

  Multiple custom parameters

  ```kotlin
  val settings = VarioqubSettings.Builder("appmetrica.1234567")
      .withClientFeature("my_cool_feature", "true")
      .withClientFeature("my_best_feature", "false")
      .withClientFeature("mode", "night")
      .withThrottleInterval(2)
      .build()
  ```

- Java

  Single custom parameter

  ```java
  VarioqubSettings settings = new VarioqubSettings.Builder("appmetrica.1234567")
      .withClientFeature("my_cool_feature", "true")
      .withThrottleInterval(2)
      .build();
  ```

  Multiple custom parameters

  ```java
  VarioqubSettings settings = new VarioqubSettings.Builder("appmetrica.1234567")
      .withClientFeature("my_cool_feature", "true")
      .withClientFeature("my_best_feature", "false")
      .withClientFeature("mode", "night")
      .withThrottleInterval(2)
      .build();
  ```

{% endlist %}

## Step 3. Set the default configuration using an XML file

You can set a default configuration so your app uses the required parameter values before it connects to a server with remote configuration.

1. Download the XML configuration file from the [Flags configuration](https://yandex.com/support/varioqub-app/en/web/config.md#create) page and save it to your app's `res/xml` folder.

2. Pass the loaded XML configuration file using the `setDefaults(int)` method.

    {% list tabs %}

    - Kotlin

      ```kotlin
      Varioqub.setDefaults(R.xml.ab_defaults)
      ```

    - Java

      ```java
      Varioqub.setDefaults(R.xml.ab_defaults);
      ```

    {% endlist %}

    {% note info "Note" %}

    The file name `ab_defaults.xml` is a placeholder used for demonstration purposes.

    {% endnote %}

## Step 4. Run a background update of the flag configuration {#shag-4-zapustite-obnovlenie-konfiguracii-flagov-v-fone}

To use the most recent flag configuration from the interface, run a background configuration update using the `fetchConfig` method.

{% note info "Note" %}

Each time you receive the latest version of the configuration (most often this occurs when starting a new session), you need to activate it using the `activateConfig()` method.

{% endnote %}

{% list tabs %}

- Kotlin

  ```kotlin
  Varioqub.fetchConfig(object : OnFetchCompleteListener {
      override fun onSuccess() {
          Log.i("VARIOQUB", "FETCH SUCCESS")

          // you can use the device ID while testing the experiment
          // to do this, specify it in the corresponding A/B experiments field
          Log.i("VARIOQUB", Varioqub.getId())
      }
      override fun onError(message: String, error: FetchError) {
          Log.i("VARIOQUB", "FETCH ERROR: $message")
      }
  })
  ```

- Java

  ```java
  Varioqub.fetchConfig(new OnFetchCompleteListener() {
      @Override
      public void onSuccess() {
          Log.i("VARIOQUB", "FETCH SUCCESS");

          // you can use the device ID while testing the experiment
          // to do this, specify it in the corresponding A/B experiments field
          Log.i("VARIOQUB", Varioqub.getId());
      }
      @Override
      public void onError(String message, FetchError error) {
          Log.i("VARIOQUB", "FETCH ERROR: " + message);
      }
  });
  ```

{% endlist %}

## Step 5. Activate the configuration {#shag-5-aktivirujte-konfiguraciyu}

<!-- source: en/sdk/_includes/config-note.md -->
{% note tip %}

Main use case:

Regularly download the configuration and activate it when the app is launched. That guarantees your flags won't change during user sessions. We recommend launching activation as soon as possible.

{% endnote %}
<!-- endsource: en/sdk/_includes/config-note.md -->

Activate the configuration using the `activateConfig()` method.

{% list tabs %}

- Kotlin

  ```kotlin
  Varioqub.activateConfig()
  ```

- Java

  ```java
  Varioqub.activateConfig();
  ```

{% endlist %}

Use the `activateConfig()` method every time you want to activate a received/loaded configuration (for example, when starting a new session).

## Getting flags from the interface

Flag values are retrieved and returned in the following order:

1. Flag is present in the experiment.
2. Flag is present in the loaded configuration.
3. Flag is present in the default configuration.

Flag getters:

 <!-- - `Varioqub.getDouble(key, default)`; -->
 <!-- - `Varioqub.getLong(key, default)`; -->
- `Varioqub.getString(key, default)`.
 <!-- - `Varioqub.getBoolean(key, default)`.-->

Code example:

{% list tabs %}

- Kotlin

  ```kotlin
  val flag = Varioqub.getString("flag", "defaultValue")
  Log.i("VARIOQUB", flag)
  ```

- Java

  ```java
  String flag = Varioqub.getString("flag", "defaultValue");
  Log.i("VARIOQUB", flag);
  ```

{% endlist %}

## Obtaining the device ID to test the experiment {#get-id}

To get the device ID, use the `Varioqub.getId()` method:

{% list tabs %}

- Kotlin

  ```kotlin
  Log.i("VARIOQUB", Varioqub.getId())
  ```

- Java

  ```java
  Log.i("VARIOQUB", Varioqub.getId());
  ```

{% endlist %}

<!-- source: en/sdk/_includes/getid-empty.md -->
{% note info %}

Before the first successful `fetchConfig` call, the `Varioqub.getId()` method may return empty responses.

{% endnote %}
<!-- endsource: en/sdk/_includes/getid-empty.md -->

To learn more about creating and testing experiments, see [Creating an experiment](https://yandex.com/support/varioqub-app/en/web/create.md).


<!-- source: en/_includes/feedback.md -->
<details>
    <summary class="button">Contact support</summary>
    <div style="padding: 15px;
     margin: 10px 0;
     background: #FFFFFF;
     border-radius: 10px;
     border: 1px solid var(--yc-color-line-generic);">
     <iframe style="background: #FFFFFF;"
        height="700"
        width="100%"
        frameborder="0"
        src="https://forms.yandex.com/surveys/2617/?iframe=1&lang=en">
    </iframe>
</details>


<!-- endsource: en/_includes/feedback.md -->

