Skip to main content
This SDK supports both Java and Kotlin Android apps using Android SDK 21 and above.
Server/Backend Development?If you’re building JVM backend applications (servers, CLIs, workers), see the Kotlin (JVM) documentation instead, which provides JVM-optimized guidance without Android-specific dependencies.

Installation

Network Dispatchers

The SDK requires a network dispatcher for fetching feature definitions:
  • GBNetworkDispatcherKtor - Ktor-based implementation (Android-friendly default)
  • GBNetworkDispatcherOkHttp - OkHttp-based implementation (recommended for most Android apps)

Quick Start

To create a GrowthBook SDK instance, use GBSDKBuilder. The SDK uses coroutines, so initialize it from a coroutine scope.

Configuration Options

The GBSDKBuilder accepts several configuration options:
  • apiKey (String, required) - Your GrowthBook API key
  • apiHost (String, required) - API host URL (typically https://cdn.growthbook.io/)
  • streamingHost (String, optional) - Streaming host URL for SSE updates
  • attributes (Map<String, GBValue>) - User attributes for targeting
  • trackingCallback ((GBExperiment, GBExperimentResult) -> Unit) - Analytics tracking callback
  • networkDispatcher (NetworkDispatcher, required) - Network implementation
  • encryptionKey (String, optional) - Decryption key for encrypted features
  • cachingEnabled (Boolean, default: true) - Enable/disable local feature caching
  • remoteEval (Boolean, default: false) - Enable remote feature evaluation
  • enableLogging (Boolean, default: false) - Print debug logs to stdout
The following options are set via builder methods rather than constructor parameters:
  • .setEnabled(Boolean) - Enable/disable all experiments (default: true)
  • .setQAMode(Boolean) - Disable randomization for QA testing (default: false)
  • .setForcedVariations(Map<String, Int>) - Force specific variations for QA
  • .setRefreshHandler(GBCacheRefreshHandler) - Callback fired when features are refreshed
  • .setStickyBucketService(GBStickyBucketService) - Enable sticky bucketing
  • .setFeatureUsageCallback(GBFeatureUsageCallback) - Callback fired on every feature evaluation

Updating User Attributes

You can update user attributes at any time using setAttributes(). This completely replaces the attributes object:
Attribute ChangesBe aware that changing attributes may change the assigned feature values, which can be disorienting to users if not handled carefully.

Feature Refresh Handler

To access features as soon as they’re loaded from the backend, use setRefreshHandler():

Evaluating Features

Feature Result

The feature() method takes a feature key and returns a GBFeatureResult object with the following properties:
  • gbValue (GBValue) - The assigned value of the feature (typed wrapper)
  • on (Boolean) - The value cast to a boolean
  • off (Boolean) - The value cast to a boolean and then negated
  • source (GBFeatureSource) - Why the value was assigned: unknownFeature, defaultValue, force, experiment, prerequisite, cyclicPrerequisite, or override
When the source is experiment, there are additional properties:
  • experiment (GBExperiment) - The experiment configuration
  • experimentResult (GBExperimentResult) - The experiment evaluation result

Basic Usage

Working with GBValue

Starting with version 2.0.0, feature values use the GBValue type for better type safety:

Typed Feature Access

For convenience, you can directly get a typed feature value using featureValue<T>() (available in version 7.1.0+):

Feature Source and Experiments

Check why a feature value was assigned and access experiment details:

Running Inline Experiments

You can run experiments directly without defining them in the GrowthBook API. This is useful for programmatic experiments:

Experiment Configuration

The GBExperiment class accepts the following properties: Required:
  • key (String) - The unique identifier for this experiment
  • variations (Array<Any>) - Array of variations to choose between
Optional:
  • weights (FloatArray) - Traffic distribution across variations (must sum to 1.0)
  • active (Boolean, default: true) - If false, always return control (first variation)
  • coverage (Float, default: 1.0) - Percentage of users to include (0.0 to 1.0)
  • condition (GBCondition) - Targeting conditions for the experiment
  • namespace (GBNamespace) - Namespace for experiment isolation
  • force (Int) - Force all users to a specific variation index (for QA)
  • hashAttribute (String, default: “id”) - User attribute for variation assignment

Experiment Result

The GBExperimentResult object contains:
  • inExperiment (Boolean) - Whether the user is in the experiment
  • variationId (Int) - The index of the assigned variation
  • value (Any) - The value of the assigned variation
  • hashAttribute (String) - The attribute used for hashing
  • hashValue (String) - The value of the hash attribute
  • key (String) - The experiment key
  • bucket (Float) - The hash bucket value (0.0 to 1.0)
  • stickyBucketUsed (Boolean) - Whether sticky bucketing was used

Advanced Experiment Example

Sticky Bucketing

Sticky Bucketing ensures users see consistent experiment variations even when targeting conditions or user attributes change. This prevents jarring user experiences from switching variations mid-experiment.

How It Works

When sticky bucketing is enabled, the SDK persists experiment assignments in local storage. The next time the user is evaluated for that experiment, they’ll get the same variation they saw before.

Implementation

Implement the GBStickyBucketService interface to enable sticky bucketing:

Using Sticky Bucketing

Pass your sticky bucket service implementation when building the SDK:
Sticky Bucket DocumentsEach sticky bucket document contains:
  • attributeName - The attribute used to identify the user (e.g., “id”, “deviceId”)
  • attributeValue - The value of that attribute (e.g., “user-123”)
  • assignments - A map of experiment assignments (e.g., {"exp1__0": "control"})

Automatic Features Refresh

The GrowthBook SDK supports automatic feature refresh through multiple mechanisms to ensure your app always has the latest feature definitions.

Server-Sent Events (SSE)

Server-Sent Events provide real-time feature updates without polling. When enabled, the SDK maintains a persistent connection to receive live updates whenever features change.

Manual Refresh

You can manually trigger feature refresh at any time:

Cache Configuration

By default, caching is enabled. You can disable it via the cachingEnabled constructor parameter:
To be notified when features are refreshed from the server, use setRefreshHandler() on the builder:

Experiment Tracking and Feature Usage Callbacks

The SDK provides comprehensive tracking capabilities for experiments and feature usage.

Experiment Tracking Callback

The tracking callback is called whenever a user is included in an experiment. This is essential for analytics and experiment analysis.
Tracking TimingThe tracking callback is only called when the user is actually included in the experiment. If they’re not in the experiment, this callback won’t be triggered.

Feature Usage Callback

The feature usage callback is called every time a feature is evaluated, regardless of whether it’s part of an experiment or not.

Advanced Tracking Example

Combine both callbacks for comprehensive tracking:

Encrypted Features and Secure Attributes

GrowthBook supports encryption for sensitive feature definitions and secure attribute hashing for privacy protection.

Encrypted Features

Encrypted features ensure that sensitive feature configurations never reach the client in plain text.

Setup Encrypted Features

  1. Enable encryption in GrowthBook: Go to your SDK Connection settings and enable “Encrypt SDK Payload”
  2. Get your encryption key: Copy the encryption key from the SDK Connection settings
  3. Configure the SDK:

Working with Encrypted Features

Security Best Practices
  • Store encryption keys securely (use Android Keystore or environment variables)
  • Never commit encryption keys to version control
  • Rotate encryption keys regularly
  • Use different keys for different environments

Secure Attributes

Secure attributes allow you to target users based on sensitive information without exposing that information to the client.

Setup Secure Attributes

  1. Enable secure attribute hashing in your SDK Connection settings
  2. Hash sensitive attributes before passing them to the SDK:

Using Secure Attributes

Advanced Secure Attribute Example

Custom Attributes

You can define custom attributes for advanced targeting scenarios:

Remote Evaluation

See the Remote Evaluation overview for more information about what Remote Evaluation is, how it works, and deployment options.
Remote Evaluation evaluates feature flags on a secure server instead of the client, ensuring sensitive targeting rules and unused variations never reach the client.

When to Use

Use Remote Evaluation when you need to:
  • Keep targeting rules private
  • Hide unused feature variations
  • Prevent users from seeing all possible values
  • Add an extra layer of security
Backend RequiredRemote Evaluation requires either:
  • GrowthBook Cloud with Remote Evaluation enabled
  • A self-hosted GrowthBook Proxy Server
  • A custom remote evaluation backend

Setup

Enable Remote Evaluation in your SDK Connection settings in GrowthBook, then configure the SDK:
When Remote Evaluation is enabled, the SDK will make an API call to evaluate features whenever user attributes change.
Sticky Bucketing with Remote EvaluationIf using Sticky Bucketing with Remote Evaluation, configure sticky bucketing on your remote evaluation backend. You don’t need to provide a StickyBucketService to the client SDK.

Serialization Support

The optional GrowthBookKotlinxSerialization module provides helpers for working with complex feature values using kotlinx.serialization.

Installation

Usage

Define your data classes and use the serialization helpers:
When to UseYou only need the serialization module if you work with complex JSON feature values and want type-safe deserialization. For simple boolean, string, and number features, you can skip this dependency.

ProGuard Configuration (Android)

If you use ProGuard or R8 for code shrinking and obfuscation, add these rules to your proguard-rules.pro:
ImportantThese are baseline rules. Depending on your app’s configuration and the features you use, you may need additional rules. Test thoroughly with ProGuard enabled.

Version History and Breaking Changes

The Kotlin SDK has undergone several major version updates with breaking changes:

Recent Versions

  • v1.1.63 (2024-11-26) - Changed value field type to kotlinx.serialization.json.JsonElement
  • v2.0.0 (2025-01-10) - Renamed value to gbValue with GBValue type; added typed feature<T>() method
  • v3.0.0 (2025-01-27) - Changed user attributes to use GBValue types
  • v4.0.0 (2025-03-03) - Changed initialize() to suspend method
  • v5.0.0 (2025-05-22) - Moved GBValue to Core module
  • v6.0.0 (2025-05-22) - Renamed hostURL to apiHost, added streamingHost
  • v6.1.0 (2025-08-15) - Changed GBStickyBucketService methods to suspend, added coroutineScope
  • v7.1.0 (2026-04-07) - Added featureValue<T>() and hid reified typed APIs from Objective-C

Migration Guidance

When upgrading between major versions, review the changelog on the GitHub repository for detailed migration instructions.

Further Reading

The GitHub repository contains comprehensive documentation including:
  • Detailed API reference
  • Advanced usage examples
  • Integration guides
  • Contributing guidelines
GitHub Repository: https://github.com/growthbook/growthbook-kotlin

Supported Features