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, useGBSDKBuilder. The SDK uses coroutines, so initialize it from a coroutine scope.
Configuration Options
TheGBSDKBuilder accepts several configuration options:
- apiKey (
String, required) - Your GrowthBook API key - apiHost (
String, required) - API host URL (typicallyhttps://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
.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 usingsetAttributes(). This completely replaces the attributes object:
Feature Refresh Handler
To access features as soon as they’re loaded from the backend, usesetRefreshHandler():
Evaluating Features
Feature Result
Thefeature() 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, oroverride
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 theGBValue type for better type safety:
Typed Feature Access
For convenience, you can directly get a typed feature value usingfeatureValue<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
TheGBExperiment class accepts the following properties:
Required:
- key (
String) - The unique identifier for this experiment - variations (
Array<Any>) - Array of variations to choose between
- 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
TheGBExperimentResult 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 theGBStickyBucketService 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 thecachingEnabled constructor parameter:
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
- Enable encryption in GrowthBook: Go to your SDK Connection settings and enable “Encrypt SDK Payload”
- Get your encryption key: Copy the encryption key from the SDK Connection settings
- Configure the SDK:
Working with Encrypted Features
Secure Attributes
Secure attributes allow you to target users based on sensitive information without exposing that information to the client.Setup Secure Attributes
- Enable secure attribute hashing in your SDK Connection settings
- 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
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
Setup
Enable Remote Evaluation in your SDK Connection settings in GrowthBook, then configure the SDK: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 optionalGrowthBookKotlinxSerialization 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 yourproguard-rules.pro:
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
valuefield type tokotlinx.serialization.json.JsonElement - v2.0.0 (2025-01-10) - Renamed
valuetogbValuewithGBValuetype; added typedfeature<T>()method - v3.0.0 (2025-01-27) - Changed user attributes to use
GBValuetypes - 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
hostURLtoapiHost, addedstreamingHost - v6.1.0 (2025-08-15) - Changed
GBStickyBucketServicemethods to suspend, addedcoroutineScope - 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

