[Android] 1. consentmanager SDK Integration
On this document, you'll find general information on how to integrate our SDK to your project. For further details, please refer to our API Reference documentation.
1. Installation
consentmanager SDK is a comprehensive solution for managing user consent in mobile applications. Designed to handle GDPR compliance, user privacy preferences, and ad tracking transparency, this SDK provides a seamless integration for iOS and Android platforms. Additionally, it offers wrapper plugins/bridges for React Native, Flutter, and Unity, making it versatile across various development environments.
Steps - High Level Description
-
Integration and Configuration:
- Integrate the SDK into your app.
- Configure the SDK settings according to your needs.
-
Creating an Instance and displaying the Consent Layer:
- On app startup, create an instance of the
CMPManager
class. This instance will handle the consent process. - The SDK will automatically display the consent screen if needed.
- On app startup, create an instance of the
-
Processing user's consent data:
- Once consents are collected, info is stored and is available for querying through different properties and methods exposed by our SDK. You'll have information about rejected or accepted consents, vendors and purposes.
1.1 Integration and Configuration
Adding dependency via Gradle
Add the following line to your build.gradle file:
dependencies {
implementation "net.consentmanager.sdkv3:cmsdkv3:3.0.0"
}
Then sync your project.
1.2 Creating an instance and displaying consent layer
Within the app-start (your onCreate
function), you must create an instance of class CMPManager
. You'll need to set up two objects that will be passed to the getInstance method: UrlConfig
, which handles your CMP configuration, like Code-ID and default language, and ConsentLayerUIConfig
. which will configure the looks of the WebView that will display the consent layer. After that, you'll pass the current Activity
using the method setActivity
, and attribute the delegate, like shown below. On the example below, you cand find both of the objects being passed. The checkWithServerAndOpenIfNecessary()
function will automatically fetch the necessary data from our server and determine if the consent screen needs to be shown or not. If so, the SDK will automatically show the consent screen at this point, via a WebView
created by our SDK, which will display the consent layer with the text and buttons according to your CMP configurations (chosen via the Code-ID of your CMP), collect the data and persist the consent information in the NSUserDefaults area of the device, so the app can display the targeted ads accordingly.
Please note that it is vital to declare and initialize the CMPManager
SDK in the onCreate
method, otherwise the view may not be ready to use and the SDK may fail. Also, please ensure you are using the correct configuration data. The configuration data can be found in your consentmanager account at Menu > CMPs > Get Code for Apps > Code-ID
Also, please note that the functionalities related to determining whether the consent is needed or not, as well as the display of the consent layer, depend on a reliable network connection. If there is no connection available or if the mechanism of retry fails to reach our server, the didReceiveError event will return a timeout error, and so the SDK will be totally unable to determine the need for a consent, as it will be totally unable to display the consent layer. Please ensure your logic takes this into account.
Example:
import net.consentmanager.cm_sdk_android_v3.CMPManager
import net.consentmanager.cm_sdk_android_v3.CMPManagerDelegate
import net.consentmanager.cm_sdk_android_v3.ConsentLayerUIConfig
import net.consentmanager.cm_sdk_android_v3.UrlConfig
class MainActivity : ComponentActivity(), CMPManagerDelegate {
private lateinit var cmpManager: CMPManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
val urlConfig = UrlConfig(
id = "YOUR_CODE_ID_HERE",
domain = "delivery.consentmanager.net",
language = "EN",
appName = "CMDemoAppKotlin"
)
val webViewConfig = ConsentLayerUIConfig(
position = ConsentLayerUIConfig.Position.FULL_SCREEN,
backgroundStyle = ConsentLayerUIConfig.BackgroundStyle.dimmed(Color.BLACK, 0.5f),
cornerRadius = 10f,
respectsSafeArea = true,
isCancelable = false
)
cmpManager = CMPManager.getInstance(
context = this,
urlConfig = urlConfig,
webViewConfig = webViewConfig,
delegate = this
)
cmpManager.setActivity(this)
checkAndOpenConsentLayer()
}
private fun checkAndOpenConsentLayer() {
cmpManager.checkWithServerAndOpenIfNecessary { result ->
result.onSuccess {
showCMPDemoScreen()
}.onFailure { error ->
Log.e("DemoApp", "Check and open consent layer failed with error: $error")
}
}
}
private fun showCMPDemoScreen() {
setContent {
MaterialTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
CMPDemoScreen(cmpManager)
}
}
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
Log.d("CMP DemoApp", "Configuration changed")
super.onConfigurationChanged(newConfig)
cmpManager.onApplicationResume()
}
override fun onPause() {
Log.d("CMP DemoApp", "Activity paused")
super.onPause()
cmpManager.onApplicationPause()
}
override fun onDestroy() {
Log.d("CMP DemoApp", "Activity destroyed")
super.onDestroy()
cmpManager.onActivityDestroyed()
}
override fun didReceiveConsent(consent: String, jsonObject: JsonObject) {
Log.d("CMP DemoApp", "Consent Layer successfully received consent message.")
runOnUiThread {
showCMPDemoScreen()
}
}
override fun didShowConsentLayer() {
Log.d("CMP DemoApp", "Consent Layer open message received.")
}
override fun didCloseConsentLayer() {
Log.d("CMP DemoApp", "Consent Layer close message received.")
runOnUiThread {
showCMPDemoScreen()
}
}
override fun didReceiveError(error: String) {
Log.e("CMP DemoApp", "SDK error: $error")
}
}
1.3 Processing users' consent data
Checking users's consents
Our SDK offer different methods to check and retrieve consent information. The main methods are displayed in the example below:
// On the example below retrieved from our Demo App, we have some examples
// of how to check consents from the user, either accepted or rejected.
val hasConsent = cmpManager.hasUserChoice() // checks if the user has already accepted/rejected consents
val hasPurposeC53 = cmpManager.hasPurposeConsent(id: "c53") // checks if the user accepted the purpose "c53"
val hasVendorS2790 = cmpManager.hasVendorConsent(id: "s2790") // checks if the user accepted the vendor "s2790"
For further information about the other methods, please refer to our full API Documentation.
Reopening the Consent Layer to check the users' choices
In order to allow the user to verify or change their choices, you can simply call openConsentLayer()
cmpManager.openConsentLayer()
This method will display the consent layer via the same WebView instance created in the previous steps.
Importing/Exporting consent information to other sources
In some cases an native app might contain webviews in order to display information, like advertising or content. In order to transmit the consent information from the SDK to the webview, you can retrieve the consent string using:
consentData = cmpManager.exportCMPInfo()
This will export the consent information and all further data that is needed by the CMP. You can then pass this information to the CMP that is in your webview by adding it to the URL that is called in the webview.
If, otherwise, you need to import the consent string using the SDK, you can use the example below:
val consentStringToImport = "Q1FERkg3QVFERkg3QUFmR01CSVRCQkVnQUFBQUFBQUFBQWlnQUFBQUFBQUEjXzUxXzUyXzUzXzU0XzU1XzU2XyNfczI3ODlfczI3OTBfczI3OTFfczI2OTdfczk3MV9VXyMxLS0tIw"
cmpManager.importCMPInfo(consentStringToImport)
Creating a custom layout
To create a customized view of the WKWebView, you can create a wrapper for the ComponentActivity that needs to be passed to the CMP SDK, so you have full control over the appearance and the lifecycle of it. For further information, please refer to the official documentation.
Logging
When using our iOS SDK, you may find the need to debug or analyze log information for various purposes. The logs generated by our SDK are tagged under "CMP", allowing you to easily filter and view only the relevant logs. For further information, please refer to this section of our documentation.
Troubleshooting
Class Not Found or NoSuchMethodException:
ProGuard can sometimes obfuscate class names or remove methods that are dynamically referenced via reflection. To fix this, you need to specify the classes and methods that should be kept intact in the ProGuard configuration file using the -keep
directive.
Example ProGuard configuration to keep a specific class and its methods:
# Kotlin serialization looks up the generated serializer classes through a function on companion
# objects. The companions are looked up reflectively so we need to explicitly keep these functions.
-keepclasseswithmembers class **.*$Companion {
kotlinx.serialization.KSerializer serializer(...);
}
# If a companion has the serializer function, keep the companion field on the original type so that
# the reflective lookup succeeds.
-if class **.*$Companion {
kotlinx.serialization.KSerializer serializer(...);
}
-keepclassmembers class <1>.<2> {
<1>.<2>$Companion Companion;
}
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
-keepattributes JavascriptInterface
-keepclassmembers class net.consentmanager.sdk.common.callbacks.* {
public *;
}
-keepclassmembers class net.consentmanager.sdk.consentlayer.ui.consentLayer.CmpWebView {
public *;
}
-keepclassmembers class net.consentmanager.sdk.consentlayer.ui.CmpLayerAppInterface {
public *;
}
-keep class net.consentmanager.sdk.CMPConsentTool {
*;
}
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
-keepattributes JavascriptInterface
# Serializer for classes with named companion objects are retrieved using `getDeclaredClasses`.
# If you have any, uncomment and replace classes with those containing named companion objects.
#-keepattributes InnerClasses # Needed for `getDeclaredClasses`.
#-if @kotlinx.serialization.Serializable class
#com.example.myapplication.HasNamedCompanion, # <-- List serializable classes with named companions.
#com.example.myapplication.HasNamedCompanion2
#{
# static **$* *;
#}
#-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
# static <1>$$serializer INSTANCE;
#}