Skip to content

Commit 0b31efc

Browse files
authored
chore:Update docs for Android SDK (#490)
* update docs for Android SDK
1 parent d0f5e14 commit 0b31efc

File tree

1 file changed

+71
-27
lines changed

1 file changed

+71
-27
lines changed

docs/sdks/client-sdks/android.md

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,83 @@ Eppo's open source Android SDK can be used for both feature flagging and experim
1313
You can install the SDK using Gradle by adding to your `build.gradle` file:
1414

1515
```groovy
16-
implementation 'cloud.eppo:android-sdk:3.3.0'
16+
implementation 'cloud.eppo:android-sdk:4.1.0'
1717
```
1818

1919
## 2. Initialize the SDK
2020

21-
Initialize the SDK with an SDK key, which can be created in the Eppo web interface:
21+
Initialize the SDK with the Android Application and an SDK key, which can be created in the Eppo web interface:
2222

2323
```java
2424
import cloud.eppo.android.EppoClient;
2525

26-
EppoClient eppoClient = EppoClient.init("YOUR_SDK_KEY");
26+
EppoClient eppoClient = new EppoClient.Builder("YOUR_SDK_KEY", getApplication())
27+
.buildAndInit();
2728
```
2829

30+
There are **blocking** and **non-blocking** initialization methods:
31+
32+
```java
33+
CompletableFuture<EppoClient> eppoClientFuture = new EppoClient.Builder("YOUR_SDK_KEY", getApplication())
34+
.buildAndInitAsync();
35+
```
36+
2937
During initialization, the SDK sends an API request to a CDN to retrieve the most recent experiment configurations from Eppo,
3038
such as variation values and traffic allocation. The SDK stores these configurations in memory so that assignments are effectively instant.
3139
For more information, see the [architecture overview](/sdks/architecture) page.
3240

3341
This SDK also leverages cached configurations from previous fetches. During initialization, if a previously-cached configuration
3442
is successfully loaded, it will complete initialization with that configuration. Updates will take effect once the fetch from CDN completes.
35-
When initialization completes from either source, it will call the optionally-provided `InitializationCallback`.
43+
44+
The initialization methods, `buildAndInit` and `buildAndInitAsync` return or resolve to an `EppoClient` instance with a loaded configuration.
45+
46+
### Initial Configuration
47+
48+
You can pass an initial configuration payload (of type `byte[]`) to the `EppoClient.Builder` class. This prevents the `EppoClient` from loading its initial configuration from a cached source, but **does not prevent an initial fetch**. You must use `offlineMode` to prevent fetching upon initialization.
49+
50+
```java
51+
// Example app configuration payload
52+
interface MyAppConfig {
53+
String getEppoConfig();
54+
}
55+
56+
// Initialize the Eppo Client with the async result of the initial client configuration.
57+
58+
// 1. Fetch your app's initial configuration
59+
CompletableFuture<MyAppConfig> initialconfigFuture = ...;
60+
61+
// 2. Transform the result into just the Eppo Configuration string (byte array).
62+
CompletableFuture<byte[]> eppoConfigBytes = initialconfigFuture.thenApply(
63+
ic-> ic.getEppoConfig().getBytes());
64+
65+
// 3. Build the client with the asynchronous initial config
66+
CompletableFuture<EppoClient> clientFuture = new EppoClient.Builder(API_KEY, getApplication())
67+
.initialConfiguration(eppoConfigBytes)
68+
.buildAndInitAsync();
69+
70+
// 4. Start assigning when the client is ready
71+
EppoClient eppoClient = clientFuture.get();
72+
String variation = eppoClient.getStringAssignment(...);
73+
```
74+
75+
### Offline Mode
76+
Using `offlineMode` mode prevents the `EppoClient` from sending an API request upon initialization. When set to `true`, the `EppoClient` will attempt to load configuration from a cache on the device, or an `initialConfiguration`, if provided. The latest configuration can be pulled from the API server at any time using the `EppoClient.loadConfiguration` and `EppoClient.loadConfigurationAsync` methods.
77+
78+
```java
79+
EppoClient eppoClient = new EppoClient.Builder("YOUR_SDK_KEY", getApplication())
80+
.initialConfiguration(initialConfigurationPayload)
81+
.offlineMode(true)
82+
.buildAndInit();
83+
84+
// Get assignments using initial/cached configuration
85+
String variation = eppoClient.getStringAssignment(...);
86+
87+
// Load the latest (also saves this to the local cache).
88+
eppoClient.loadConfiguration();
89+
90+
// Get assignments using the latest
91+
String variation = eppoClient.getStringAssignment(...);
92+
```
3693

3794
If you are using the SDK for experiment assignments, make sure to pass in an assignment logging callback (see [section](#define-an-assignment-logger-experiment-assignment-only) below).
3895

@@ -59,31 +116,18 @@ AssignmentLogger assignmentLogger = new AssignmentLogger() {
59116
}
60117
};
61118

62-
CountDownLatch lock = new CountDownLatch(1);
63119

64-
InitializationCallback initializationCallback = new InitializationCallback() {
65-
@Override
66-
public void onCompleted() {
67-
Log.w(TAG, "Eppo client successfully initialized");
68-
lock.countDown();
69-
}
70-
71-
@Override
72-
public void onError(String errorMessage) {
73-
Log.w(TAG, "Eppo client encountered an error initializing: " + errorMessage);
74-
Log.w(TAG, "Eppo client will serve default values for assignments");
75-
lock.countDown();
76-
}
77-
};
78-
79-
EppoClient eppoClient = new EppoClient.Builder()
80-
.application(application)
81-
.apiKey("YOUR_SDK_KEY")
120+
CompletableFuture<EppoClient> eppoClientFuture = new EppoClient.Builder("YOUR_SDK_KEY", getApplication())
82121
.assignmentLogger(assignmentLogger)
83-
.callback(initializationCallback)
84-
.buildAndInit();
85-
86-
lock.await(5000, TimeUnit.MILLISECONDS);
122+
.buildAndInitAsync();
123+
124+
EppoClient eppoClient;
125+
try {
126+
eppoClient = eppoClientFuture.get(5000, TimeUnit.MILLISECONDS);
127+
} catch (ExecutionException | TimeoutException | InterruptedException e) {
128+
// Eppo Client failed to initialize within 5 seconds.
129+
throw new RuntimeException(e);
130+
}
87131
```
88132

89133
The SDK will invoke the `logAssignment` function with an `Assignment` object that contains the following fields:

0 commit comments

Comments
 (0)