The flipt-client-android library contains the Kotlin for Android source code for the Flipt client-side evaluation client.
Add the dependency in your build.gradle:
dependencies {
implementation 'io.flipt:flipt-client-android:1.x.x'
}Add the dependency in your pom.xml:
<dependency>
<groupId>io.flipt</groupId>
<artifactId>flipt-client-android</artifactId>
<version>1.x.x</version>
</dependency>The flipt-client-android library is a wrapper around the flipt-engine-ffi library.
All evaluation happens within the SDK, using the shared library built from the flipt-engine-ffi library.
Because the evaluation happens within the SDK, the SDKs can be used in environments where the Flipt server is not available or reachable after the initial data is fetched.
Upon instantiation, the flipt-client-android library will fetch the flag state from the Flipt server and store it in memory. This means that the first time you use the SDK, it will make a request to the Flipt server.
By default, the SDK will poll the Flipt server for new flag state at a regular interval. This interval can be configured using the fetchMode option when constructing a client. The default interval is 120 seconds.
Flipt Cloud and Flipt v2 users can use the streaming fetch method to stream flag state changes from the Flipt server to the SDK.
When in streaming mode, the SDK will connect to the Flipt server and open a persistent connection that will remain open until the client is closed. The SDK will then receive flag state changes in real-time.
The SDK will automatically retry fetching (or initiating streaming) flag state if the client is unable to reach the Flipt server temporarily.
The SDK will retry up to 3 times with an exponential backoff interval between retries. The base delay is 1 second and the maximum delay is 30 seconds.
Retriable errors include:
429 Too Many Requests502 Bad Gateway503 Service Unavailable504 Gateway Timeout- Other potential transient network or DNS errors
This SDK currently supports the following OSes/architectures:
- Android x86_64
- Android arm64
FliptEvaluationClienthas been renamed toFliptClient. Update all usages and imports accordingly.- The builder and all usages should be updated to the new class name and builder pattern.
- All response models now use
Listinstead of arrays (e.g.,List<String>instead ofString[]). Update your code to useListand related collection APIs. - Exceptions are now unchecked (runtime) and use
EvaluationExceptionandValidationException. - The builder is more idiomatic and uses Kotlin DSL style, reducing boilerplate.
requestTimeoutandupdateIntervalnow usekotlin.time.Duration.
In your Kotlin Android code you can import this client and use it as so:
import io.flipt.client.FliptClient
import io.flipt.client.models.ClientTokenAuthentication
import io.flipt.client.models.VariantEvaluationResponse
import kotlin.time.Duration.Companion.seconds
fun main() {
val fliptClient = FliptClient.builder()
.url("http://localhost:8080")
.authentication(ClientTokenAuthentication("secret"))
.namespace("default")
.environment("default")
.requestTimeout(10.seconds)
.updateInterval(120.seconds)
.build()
val context = mapOf("fizz" to "buzz")
try {
val response: VariantEvaluationResponse =
fliptClient.evaluateVariant("flag1", "entity", context)
println(response)
} catch (e: Exception) {
e.printStackTrace()
} finally {
// Important: always close the client to release resources
fliptClient.close()
}
}This client is thread-safe and can be reused across your application.
The FliptClient.builder() method returns a FliptClientBuilder object that allows you to configure the client with the following methods:
environment: The environment (Flipt v2) to fetch flag state from. If not provided, the client will default to thedefaultenvironment.namespace: The namespace to fetch flag state from. If not provided, the client will default to thedefaultnamespace.url: The URL of the upstream Flipt instance. If not provided, the client will default tohttp://localhost:8080.requestTimeout: The timeout (kotlin.time.Duration) for requests to the upstream Flipt instance. If not provided, the client will default to no timeout. Note: this only affects polling mode. Streaming mode will have no timeout set.updateInterval: The interval (kotlin.time.Duration) in which to fetch new flag state. If not provided, the client will default to 120 seconds.authentication: The authentication strategy to use when communicating with the upstream Flipt instance. If not provided, the client will default to no authentication. See the Authentication section for more information.reference: The reference to use when fetching flag state. If not provided, reference will not be used.fetchMode: The fetch mode to use when fetching flag state. If not provided, the client will default to polling.errorStrategy: The error strategy to use when fetching flag state. If not provide, the client will be default to fail. See the Error Strategies section for more information.snapshot: The initial snapshot to use when instantiating the client. See the Snapshotting section for more information.
The FliptClient supports the following authentication strategies:
- No Authentication (default)
- Client Token Authentication
- JWT Authentication
The client errorStrategy method supports the following error strategies:
fail: The client will throw an error if the flag state cannot be fetched. This is the default behavior.fallback: The client will maintain the last known good state and use that state for evaluation in case of an error.
The client supports snapshotting of flag state as well as seeding the client with a snapshot for evaluation. This is helpful if you want to use the client in an environment where the Flipt server is not guaranteed to be available or reachable on startup.
To get the snapshot for the client, you can use the getSnapshot method. This returns a base64 encoded JSON string that represents the flag state for the client.
You can set the snapshot for the client using the snapshot builder method when constructing a client.
Note: You most likely will want to also set the errorStrategy to fallback when using snapshots. This will ensure that you wont get an error if the Flipt server is not available or reachable even on the initial fetch.
You also may want to store the snapshot in a local file so that you can use it to seed the client on startup.
Important
If the Flipt server becomes reachable after the setting the snapshot, the client will replace the snapshot with the new flag state from the Flipt server.
Contributions are welcome! Please feel free to open an issue or submit a Pull Request.
This project is licensed under the MIT License.