Skip to content

docs: Android docs improvements #5741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ description: Manage your Feature Flags and Remote Config in your Android applica
slug: /clients/android
---

import CodeBlock from '@theme/CodeBlock'; import { AndroidVersion } from '@site/src/components/SdkVersions.js';
import CodeBlock from '@theme/CodeBlock';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import { AndroidVersion } from '@site/src/components/SdkVersions.js';

This SDK can be used for Android applications written in Kotlin. The source code for the client is available on
[GitHub](https://github.com/Flagsmith/flagsmith-kotlin-android-client/).
Expand All @@ -14,36 +17,49 @@ This SDK can be used for Android applications written in Kotlin. The source code

### Gradle

```groovy
repositories {
The Flagsmith Android SDK is available from [Maven Central](https://mvnrepository.com/artifact/com.flagsmith/flagsmith-kotlin-android-client).

<Tabs>
<TabItem value="kotlin" label="Kotlin">

<CodeBlock title="app/build.gradle.kts">
{`repositories {
google()
mavenCentral()
}
```

In your project path `app/build.gradle` add a new dependency:
dependencies {
implementation("com.flagsmith:flagsmith-kotlin-android-client:${AndroidVersion({})}")
}`}
</CodeBlock>

</TabItem>

<CodeBlock>{`implementation("com.flagsmith:flagsmith-kotlin-android-client:`}<AndroidVersion />"{`)`}</CodeBlock>
<TabItem value="groovy" label="Groovy">

<CodeBlock title="app/build.gradle">
{`repositories {
google()
mavenCentral()
}

## Basic Usage
dependencies {
implementation 'com.flagsmith:flagsmith-kotlin-android-client:${AndroidVersion({})}'
}`}
</CodeBlock>

The SDK is initialised against a single environment within a project on [https://flagsmith.com](https://flagsmith.com),
for example the Development or Production environment. You can find your Client-side Environment Key in the Environment
settings page.
</TabItem>
</Tabs>

## Initialization
## Initialisation

### Within your Activity inside `onCreate()`
Initialise the Flagsmith client in your Activity's `onCreate` method:

```kotlin
lateinit var flagsmith : Flagsmith

override fun onCreate(savedInstanceState: Bundle?) {
initFlagsmith();
}

private fun initFlagsmith() {
flagsmith = Flagsmith(environmentKey = FlagsmithConfigHelper.environmentDevelopmentKey, context = context)
flagsmith = Flagsmith(environmentKey = "your_client_side_environment_key", context = context)
}
```

Expand All @@ -52,14 +68,14 @@ private fun initFlagsmith() {
The Flagsmith SDK has various parameters for initialisation. Most of these are optional, and allow you to configure the
Flagsmith SDK to your specific needs:

- `environmentKey` Take this API key from the Flagsmith dashboard and pass here
- `baseUrl` By default we'll connect to the Flagsmith backend, but if you self-host you can configure here
- `environmentKey` The client-side SDK key for your current Flagsmith environment.
- `baseUrl` If not using Flagsmith SaaS, set this to your Flagsmith API URL. For example `"https://flagsmith.example.com/api/v1/"`
- `context` The current Context is required to use the Flagsmith Analytics functionality
- `enableAnalytics` Enable analytics - default true. Disable this if you'd like to avoid the use of Context
- `analyticsFlushPeriod` The period in seconds between attempts by the Flagsmith SDK to push analytic events to the
server
- `enableRealtimeUpdates` Enable the SDK to receive updates to features in real time while the app is running
- `defaultFlags` Provide default flags the the SDK to ensure values are availble when no network connection can be made
- `defaultFlags` Provide default flags to the SDK to ensure values are available when no network connection can be made
- `cacheConfig` Disabled by default, but when enabled will allow Flagsmith to fall back to cached values when no network
connection can be made
- `request / read / writeTimeoutSeconds` Fine-grained control of the HTTP timeouts used inside the Flagsmith SDK
Expand Down Expand Up @@ -167,34 +183,25 @@ the event that it cannot receive a response from our API.
val defaultFlags = listOf(
Flag(
feature = Feature(
id = 345345L,
name = "Flag 1",
createdDate = "2023‐07‐07T09:07:16Z",
description = "Flag 1 description",
type = "CONFIG",
defaultEnabled = true,
initialValue = "true"
), enabled = true, featureStateValue = "value1"
),
enabled = true,
featureStateValue = "value1",
),
Flag(
feature = Feature(
id = 34345L,
name = "Flag 2",
createdDate = "2023‐07‐07T09:07:16Z",
description = "Flag 2 description",
type = "CONFIG",
defaultEnabled = true,
initialValue = "true"
), enabled = true, featureStateValue = "value2"
),
enabled = true,
featureStateValue = "value2",
),
)

// Then pass these during initialisation:
flagsmith = Flagsmith(
environmentKey = FlagsmithConfigHelper environmentDevelopmentKey,
environmentKey = "your_client_side_environment_key",
defaultFlags = defaultFlags,
context = context)

context = context,
)
```

### Cache
Expand All @@ -206,8 +213,9 @@ initialisation:
```kotlin
flagsmith = Flagsmith(
environmentKey = FlagsmithConfigHelper environmentDevelopmentKey,
cacheConfig = FlagsmithCacheConfig(enableCache = true)
context = context)
cacheConfig = FlagsmithCacheConfig(enableCache = true),
context = context,
)
```

You can also set a TTL for the cache (in seconds) for finer control:
Expand All @@ -226,9 +234,10 @@ By default, the client uses a default configuration. You can override the config
realtime flag updates in your hosted environment you'll also need to pass the eventSourceUrl in a similar fashion:

```kotlin
flagsmith = Flagsmith(
environmentKey = Helper.environmentDevelopmentKey,
context = context,
baseUrl = "https://flagsmith.example.com/api/v1/"),
eventSourceUrl = "https://realtime.flagsmith.example.com/"
flagsmith = Flagsmith(
environmentKey = "your_client_side_environment_key",
context = context,
baseUrl = "https://flagsmith.example.com/api/v1/",
eventSourceUrl = "https://realtime.flagsmith.example.com/",
)
```