Android SDK Reference

Updated 3 months ago by Archana Singh

This topic explains how to use the Harness Feature Flags SDK in your Android application. To learn more about using a sample Feature Flags SDK code with an Android application, see the Android GitHub repository.

Before You Begin

Use Harness Feature Flags SDKs with Android Applications

Perform the following steps to get started with using the Feature Flags SDK in your Android application:

Step 1: Declare the Repository

Add the following to your script in build.gradle file to declare the Maven Central repository for your build.

buildscript {
repositories {
mavenCentral()
}

Step 2: Add the Dependency to Your Application

Add the Harness Feature Flags Flutter SDK dependency in the build.gradle file.

Refer to the Harness Feature Flag Android client SDK to identify the latest version for your build automation tool.

This section lists dependencies for Gradle (Short) and uses the 0.0.4 version:

implementation 'io.harness:ff-android-client-sdk:0.0.4'

Step 3: Authorize your Application to Connect to the Feature Flags Client

CfClient  is a base class that provides all features of SDK. This is a singleton and it is accessed with CfClient.getInstance().

val sdkConfiguration = CfConfiguration.builder()
.baseUrl("BASE_API_URL")
.pollingInterval(30) //time in seconds
.enableStream(true)
.streamUrl("STREAM_URL")
.build()

val target = Target().identifier("target")

CfClient.getInstance().initialize(context, "YOUR_API_KEY", sdkConfiguration, target)

  • YOUR_API_KEY: The SDK keys authorize your application to connect to the CF client. See Step 3: Create an SDK Key.
  • target: Represents the desired target for which the feature needs to be evaluated.

Step 4: Evaluate Target for Your Feature Flag

Once you have initialized the Harness Feature Flags client for a target, evaluate it for your feature flag. A feature flag is evaluated for a particular target.

Evaluation is performed based on the variation types. In case there is no evaluation with the provided ID, the default value is returned.

Boolean Variation

//get boolean evaluation
val evaluation: Boolean = CfClient.getInstance().boolVariation("demo_evaluation", false)

Number Variation

//get number evaluation
val numberEvaluation: Double = CfClient.getInstance().numberVariation("demo_number_evaluation", 0)

String Variation

//get string evaluaation
val stringEvaluation: String = CfClient.getInstance().stringVariation("demo_string_evaluation", "demo_value")

Step 5: Add the Method to Register for Events

This method provides a way to register a listener for different events that might be triggered by SDK, indicating a specific change in the SDK itself.

private final EventsListener eventsListener = statusEvent -> {
if (statusEvent.getEventType() == EVALUATION_CHANGE) {
Evaluation evaluation = statusEvent.extractPayload();
}
}

CfClient.getInstance().registerEventsListener(eventsListener)

The triggered event will have one of the following types:

public enum EVENT_TYPE {
SSE_START,
SSE_END,
EVALUATION_CHANGE,
EVALUATION_RELOAD
}

The following table provides a summary of the possible event types and corresponding responses.

EVENT_TYPE

Response

SSE_START

-

SSE_END

-

EVALUATION_CHANGE

Evaluation

EVALUATION_RELOAD

List<Evaluation>

When the listener is not needed, you can remove the desired listener from the internal list to avoid unexpected behavior.

CfClient.getInstance().unregisterEventsListener(eventsListener)

When SDK is not needed, for example, when the app is not running, you can shut down the SDK. This can avoid potential memory leaks.

CfClient.getInstance().destroy()

Step 6: Verify Your Feature Flag

Run the application from your IDE and verify whether the flag variation value displayed on your application page is consistent with the feature flag you created. Toggle the flag on and off to see whether your application is updated accordingly.

Sample Code for 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, see the Android GitHub repository.

package com.example.ffsdktryout
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import io.harness.cfsdk.*
import io.harness.cfsdk.cloud.model.Target
import io.harness.cfsdk.logging.CfLog
import io.harness.cfsdk.CfClient
import io.harness.cfsdk.cloud.oksse.model.StatusEvent
import io.harness.cfsdk.cloud.core.model.Evaluation
import io.harness.cfsdk.cloud.events.EvaluationListener
import io.harness.cfsdk.cloud.oksse.EventsListener
class MainActivity : AppCompatActivity() {
private val logTag = MainActivity::class.simpleName
private var eventsListener = EventsListener { event ->
if (event.eventType == StatusEvent.EVENT_TYPE.EVALUATION_CHANGE) {
// Do something
} else if (event.eventType == StatusEvent.EVENT_TYPE.EVALUATION_RELOAD) {
// Do something else
}
}
private val evaluationListener: EvaluationListener = EvaluationListener {
// Do something
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val target = Target().identifier("YOUT_TARGET_IDENTIFIER").name("YOUR_TARGET_NAME")
val remoteConfiguration = CfConfiguration.builder()
.enableStream(true)
.build()
io.harness.cfsdk.CfClient.getInstance()
.initialize(
this,
"YOUR_API_KEY",
remoteConfiguration,
target
) { _, result ->
if (result.isSuccess) {
val registerEventsOk = CfClient.getInstance().registerEventsListener(eventsListener)
var registerEvaluationsOk = CfClient.getInstance().registerEvaluationListener(
"YOUR_EVALUATION_IDENTIFIER",
evaluationListener
)
} else {
CfLog.OUT.e(logTag, "Init. error")
}
}
}
}


Please Provide Feedback