Supported SDKs
Android SDK Integration Guide
The Opacity Android SDK allows you to integrate Opacity functionality into your Android app. Follow the steps below to install and set up the SDK in your project.
Setup
This tutorial is written with Android Studio using the Empty Activity template in Kotlin with version 35 of the Android SDK. Other systems may require slightly different configuration.
Select the Android view.
Add two bottom two repositories to the Gradle Scripts > settings.gradle.kts file.
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url = uri("https://maven.mozilla.org/maven2/") } maven { url = uri("https://jitpack.io") } } }
In Gradle Scripts > build.gradle.kts (Module :app), add a dependency on the Opacity.
dependencies { . . . implementation("com.github.OpacityLabs:opacity-android:+") }
+ means the latest version. Alternatively, you can find the latest release version on the OpacityLabs GitHub releases page.
You need to add an activity to app > manifests > AndroidManifest.xml. Place this code inside the <application> tag:
<activity android:name="com.opacitylabs.opacitycore.InAppBrowserActivity" android:theme="@style/Theme.AppCompat.DayNight" />
This activity will be used to launch the in-app web browser when needed.
Click Sync Now (above the text editor portion of Android Studio) to process the Gradle changes.
Note that it may take a few minutes for the synchronization.
SDK initialization
Once the dependencies are set up, you can initialize the Opacity SDK and call its functions in your MainActivity.
In your MainActivity.kt, import the Opacity SDK class:
import com.opacitylabs.opacitycore.OpacityCore
In the onCreate function of your MainActivity class definition, before the setContent call, add this code:
// Initialize the Opacity Core SDK with your API key and environment OpacityCore.initialize("[Your OPACITY API KEY]", false, OpacityCore.Environment.PRODUCTION, true) OpacityCore.setContext(this) // Pass the current activity to launch the in-app browser
Make sure to replace [Your OPACITY API KEY] with your actual API key.
Using the SDK
This is a sample component that uses the Opacity SDK.
@Composable
fun OpacityUsage() {
val opacityResult = remember { mutableStateOf("Opacity Output Here") }
val coroutineScope = rememberCoroutineScope()
Button(
onClick = {
coroutineScope.launch {
val res: Map<String, Any> = OpacityCore.get("flow:github:profile", null)
opacityResult.value = res.toString()
}
}
) { Text(text = "Use Opacity") }
Text (
text = opacityResult.value
)
}
The part that actually uses Opacity is simple:
val res: OpacityResponse = OpacityCore.getGithubProfile()
opacityResult.value = res.json.toString()
We call the SDK and then write the result. However, the code around it is necessary because calling the SDK is a slow activity (it requires traversing the network) and therefore has to be in a coroutine.
You can see the list of SDK calls available here.
Complete example
Here is the app > kotlin+java > <package name> > MainActivity.kt from a complete working example:
package com.example.sample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.example.sample.ui.theme.SampleTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import kotlinx.coroutines.launch
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import com.opacitylabs.opacitycore.OpacityCore
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
// Initialize the Opacity Core SDK with your API key and environment
OpacityCore.initialize("[Your OPACITY API KEY]", false, OpacityCore.Environment.PRODUCTION)
OpacityCore.setContext(this) // Pass the current activity to launch the in-app browser
setContent{
SampleTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Column(modifier = Modifier.padding(innerPadding), content = {
Text( text = "Opacity sample program" )
OpacityUsage()
})
}
}
}
}
}
@Composable
fun OpacityUsage() {
val opacityResult = remember { mutableStateOf("Opacity Output Here") }
val coroutineScope = rememberCoroutineScope()
Button(
onClick = {
coroutineScope.launch {
val res: Map<String, Any> = OpacityCore.get("flow:github:profile", null)
opacityResult.value = res.toString()
}
}
) { Text(text = "Use Opacity") }
Text (
text = opacityResult.value
)
}
How to use this example
Start Android Studio.
Create a new Empty Activity project.
Name the project sample. For the minimum SDK, select API 35 ("VanillaIceCream"; Android 15).
Add the Opacity SDK as explained above.
Click Sync Now to synchronize the new Gradle settings.
Replace MainActivity.kt with the code above.
On line 30, enter your own API key.
Click the green triangle icon at the top to run the application. You might need to click the device manager to configure a virtual device to run the application.
Note that the build process is slow, especially the first time. CLick the hammer icon on the left to see the build process.
Wait until the app preview opens and click Use Opacity.
Wait until the GitHub logon page open, and log on to GitHub.
Wait until you see your profile in JSON format.
Detailed explanation
package com.example.sample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.example.sample.ui.theme.SampleTheme
These are standard boilerplate created by Android Studio when you use the empty activity template.
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import kotlinx.coroutines.launch
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
These are packages added for this example. The need for them will be explained when we reach it in the code.
import com.opacitylabs.opacitycore.OpacityCore
The Opacity library.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
This function initializes the application.
super.onCreate(savedInstanceState)
enableEdgeToEdge()
// Initialize the Opacity Core SDK with your API key and environment
OpacityCore.initialize("[Your Opacity API Key Here]", false, OpacityCore.Environment.PRODUCTION)
OpacityCore.setContext(this) // Pass the current activity to launch the in-app browser
Initialize the Opacity SDK. The OpacityCore.setContext(this) call lets us tell Opacity in which activity it is located, which lets it open a browser when needed to log on to a platform.
setContent {
SampleTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Column(modifier = Modifier.padding(innerPadding), content = {
Text( text = "Opacity sample program" )
OpacityUsage()
})
}
}
}
}
}
A Column() layout lets us put widgets in a column, so they'll all be visible.
@Composable
The component that uses Opacity needs to be Composable because it needs a state variable to communicate between the coroutine that runs the Opacity call, and the main thread, which displays the result.
fun OpacityUsage() {
val opacityResult = remember { mutableStateOf("Opacity Output Here") }
Use remember to create a state variable, and specify the initial value.
val coroutineScope = rememberCoroutineScope()
rememberCoroutineScope() lets us create coroutines from event handlers.
Button(
Compose widgets, such as a Button have their parameters in the function call.
onClick = {
coroutineScope.launch {
Create a coroutine to run the Opacity SDK call. This call requires use of the network, so it's too slow to do synchronously.
val res: Map<String, Any> = OpacityCore.get("flow:github:profile", null)
opacityResult.value = res.toString()
This is the actual call that gets the user's GitHub profile.
opacityResult.value = res.json.toString()
}
Here we convert the value into a JSON encoded string.
}
) { Text(text = "Use Opacity") }
The final expression, called a trailing lambda , is a function send as an argument to the preceeding function (in this case, Button(...)). By convention, this function can be outside the parameter list. Trailing lambdas are often used in Kotlin Android development for display settings, such as the text in a button.
Text (
text = opacityResult.value
)
}
This text area shows opacityResult.value. When that value changes, the component is redrawn and you get the new value.
Conclusion
By following these steps, you will successfully integrate the Opacity Android SDK into your Android project. You can now initialize the SDK, interact with Opacity features, and launch the in-app web browser for your users.