Android SDK Reference
This topic explains how to use the Harness Feature Flags SDK in your Android application.
This topic describes how to use the Harness Feature Flags Android SDK for your Android application.
For getting started quickly, you can use our sample code from the SDK README. You can also clone and run a sample application from the Android SDK GitHub Repository.
The SDK caches your Feature Flags. If the cache can't be accessed, the defaultValue is used.
Before you begin
Make sure you read and understand:
Version
Latest SDK version can be found on GitHub Release Page
To use this version of the SDK, you also need to use Android API level 21 or higher.
Requirements
To use this SDK, make sure you:
Installed Android Studio or the Android SDK for CLI only
If building the SDK you will require at least Android Studio Hedgehog 2023.1.1
Installed Java 11 or newer
Installed Gradle 8.3 or newer
Use Android API level 21 or higher.
Then ensure you:
Create an Android application to use with the SDK, or clone our sample application to use.
Download the SDK from our GitHub Repository.
Create a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode.
Install the SDK
To add the Android SDK to your application, add the following snippet to the root project's build.gradle file:
Then, in your app module's build.gradle file, add the following dependency for the SDK:
Release builds and ProGuard
For release builds, Android uses ProGuard to apply optimizations that can affect the behavior of the SDK.
Please add the following rule to your ProGuard configuration to ensure proper functionality when running your Android app from a release build
Initialize the SDK
To initialize the Android SDK, you need to:
Add your Client SDK key to connect to your Harness Environment.
Add a Target that you want to Evaluate against a Feature Flag.
Configure the SDK options, if needed. For more details on what features you can configure for this SDK, go to Configure the SDK.
Complete the initialization with the SDK using your Client SDK Key, Target, and Configuration parameters that you set.
Add a Target
For more information about Targets, go to Targeting Users With Flags.
To add a Target, build it and pass in arguments for the following:
Parameter
Description
Required?
Example
identifier
ID of the Target.Read Regex requirements for Target names and identifiers below for accepted characters.
Required
.identifier("HT_1")
name
Name for this Target. This does not have to be unique. Note: If you don’t provide a value, the name will be the same as the identifier.Read Regex requirements for Target names and identifiers below for accepted characters.
OptionalNote: If you don't want to send a name, don't send the parameter. Sending an empty argument will cause an error.
.name("Harness_Target_1")
attributes
Additional data you can store for a Target, such as email addresses or location.
Optional
target.attributes["email"] = "demo@harness.io"
For example:
Configure the SDK
You can configure the following features of the SDK:
Name
Configuration Option
Description
Default
baseUrl
baseUrl("https://config.ff.harness.io/api/1.0")
The URL used to fetch Feature Flag Evaluations. When using the Relay Proxy, change this to: http://localhost:7000
https://config.ff.harness.io/api/1.0
eventUrl
eventUrl("https://events.ff.harness.io/api/1.0")
The URL for posting metrics data to the Feature Flag service. When using the Relay Proxy, change this to: http://localhost:7000
https://events.ff.harness.io/api/1.0
pollInterval
pollingInterval(60)
The interval in seconds that we poll for changes when you are using stream mode.
60 (seconds)
enableStream
enableStream(true)
Set to true to enable streaming mode.Set to false to disable streaming mode.
true
enableAnalytics
enableAnalytics(true)
Set to true to enable analytics.Set to false to disable analytics.
true
For example:
Complete the initialization
CfClient is a base class that provides all the features of the SDK, which can be accessed with CfClient.getInstance().
To initialize the SDK, you must pass in the following:
YOUR_API_KEY- The client SDK key you created when creating the Feature Flag.Any configuration options you want to use.
The Target you want to evaluate.
Sample of initializing the SDK
Evaluate a Flag
Evaluating a Flag is when the SDK processes all Flag rules and returns the correct Variation of that Flag for the Target you provide.
If a matching Flag can’t be found, or the SDK can’t remotely fetch flags, the default value is returned.
There are different methods for the different Variation types and for each method you need to pass in:
Identifier of the Flag you want to evaluate
The default Variation
Evaluate a boolean Variation
boolVariation(String evaluationId, boolean defaultValue)
Evaluate a number Variation
numberVariation(String evaluationId, double defaultValue)
Evaluate a string Variation
stringVariation(String evaluationId, String defaultValue)
Evaluate a JSON Variation
jsonVariation(String evaluationId, JSONObject defaultValue)
These methods must not be executed on the application's main thread as they could trigger network operations.
Listen for events
Register the event listener
The eventsListener method provides a way to register a listener for different events that might be triggered by SDK.
The possible events, reasons for being emitted, and their responses are outlined in the following table:
EVENT_TYPE
Reason
Response
SSE_START
The SDK has successfully opened a streaming connection to the Feature Flags service, and is listening for events. No further action required from your application code.
-
SSE_END
The SDK has closed the streaming connection to the Feature Flags service and is no longer listening for events.
This can happen if the SDK is shutdown, of if there is a transient networking issue, and the SDK will attempt to reconnect. In both cases, no further action required from your application code.
-
EVALUATION_CHANGE
The SDK has streamed a new evaluation after a flag has been changed using the Harness web application. You should evaluate a flag to get the latest variation.
Note this is for individual flag changes only, and this event is not applicable to target group changes.
See EVALUATION_RELOAD for streaming target group changes.
Evaluation
EVALUATION_RELOAD
This event can be emitted for 3 reasons. For all 3 reasons, you should evaluate any flags to get the latest variation.
A target group change has been made using the Harness web application, and all evaluations has been reloaded.
Streaming has been disabled and the SDK is in polling mode, and a poll has completed which has reloaded all evaluations.
The SDK has disconnected from the stream and has entered polling mode, and a poll has completed which has reloaded all evaluations. |
List<Evaluation>|
For example:
Close the event listener
To avoid unexpected behavior, when the listener isn't needed, turn it off by calling CfClient.getInstance().unregisterEventsListener(eventsListener), for example:
Test your app is connected to Harness
When you receive a response showing the current status of your Feature Flag, go to the Harness Platform and toggle the Flag on and off. Then, check your app to verify if the Flag Variation displayed is updated with the Variation you toggled.

Close the SDK client
In most applications, you won't need to close the SDK client.
However, you should close the SDK client if:
Your application is about to terminate. Closing the client ensures that all associated resources are released.
You have determined that you do not need to evaluate flags again in your application lifecycle.
To close the SDK, call this method:
Additional options
Configure your logger
We use SLF4J for this SDK, you can configure any compatible logger. For example you can include logback via Gradle:
Using the following code you can configure it to write to logcat:
Change <PKGNAME> above to one of the following
-* io.harness.cfsdk.CfClient - Main client logs -* io.harness.cfsdk.cloud.analytics - Analytics logs -* io.harness.cfsdk.cloud.sse.EventSource - SEE stream logs
Or any other valid package name within the SDK.
Use our public API methods
Our public API exposes the following methods that you can use:
public boolean boolVariation(String evaluationId, boolean defaultValue)public String stringVariation(String evaluationId, String defaultValue)public double numberVariation(String evaluationId, double defaultValue)public JSONObject jsonVariation(String evaluationId, JSONObject defaultValue)public void registerEventsListener(EventsListener listener)public void unregisterEventsListener(EventsListener observer)public void refreshEvaluations()public void close()
Sample code for an Android application
Here is a sample code for using Harness Feature Flag SDKs with the Android application. To learn more about using the sample Android application, go to the Android GitHub repository.
Troubleshooting
The SDK logs the following codes for certain lifecycle events, for example authentication, which can aid troubleshooting.
Code
Description
Log Level
1000
Successfully initialized
Info
1001
Failed to initialize due to authentication error
Error
1002
Failed to initialize due to a missing or empty API key
Error
1003
WaitForInitialization configuration option was provided and the SDK is waiting for initialization to finish
Info
2000
Successfully authenticated
Info
2001
Authentication failed with a non-recoverable error
Error
2002
Authentication failed and is retrying
Warn
2003
Authentication failed and max retries have been exceeded
Error
3000
SDK closing
Info
3001
SDK closed successfully
Info
4000
Polling service started
Info
4001
Polling service stopped
Info
5000
Streaming connected
Info
5001
Streaming disconnected
Warn
5002
Streaming event received
Debug
5003
Streaming disconnected and is retrying to connect
Info
5004
Streaming service stopped
Info
5005
Stream is still retrying to connect after 4 attempts
Warn
6000
Evaluation was successful
Debug
6001
Evaluation failed and the default value was returned
Info
7000
Metrics service has started
Info
7001
Metrics service has stopped
Info
7002
Metrics posting failed
Warn
7003
Metrics posting success
Debug
Last updated
Was this helpful?