[Android] 0. Migration Guide
This guide will help you migrate from the previous version of the CMP SDK to the current version. We'll cover the changes in method names, parameters, and new or deprecated functionality. For Demo Apps examples, please check this link. There are two folders, one with a Demo App completely developed in Java, inclusing a wrapper for Java called JavaCMPManager
another completely developed in Kotlin.
Please be aware that this version of the CMP SDK were completely rebuilt from scratch, and so it represents a major breaking change, as all the methods were renamed, as were renamed the signatures, also now offering callbacks to almost all of the methods. In all cases, you will need to modify your code and update your dependencies to ensure that your mobile app works as expected. We also strongly advise you to request your users to uninstall and reinstall your mobile app, as to avoid issues with the data persisted on the device.
Repositories
Please note that all of our repositories changed for the Android SDK. Follow instructions below to find out where to point your dependency manager.
Maven
On your Gradle file, replace the previous line with the one below:
implementation("net.consentmanager.sdkv3:cmsdkv3:3.0.0")
After changing, sync your project.
Key Migration Points
-
Delegate pattern: Instead of individual listeners, the new version uses a single delegate protocol (
CMPManagerDelegate
) for handling events. It contains 4 main events:-
didReceiveConsent(consent: String, jsonObject: [String : Any])
This is triggered when the consent layer was closed after the user updating his/her consents OR when invoking methods that cause changes in the consents, like acceptAll, rejectAll, acceptVendors, rejectVendors, etc. It means that the user accepted of rejected some of all of the consents, and that those were correctly saved in the device. -
didShowConsentLayer
This is triggered when the consent layer was actually displayed. It means that there was not a consent valid in the device, so a new one should be collected. -
didCloseConsentLayer
This is triggered when the SDK checked the need for a consent, but it was not needed and the layer was not displayed. It means that there is already a valid in the device, so a new one is not necessary and tue consent layer will not be displayed. -
didReceiveError
This is triggered when the SDK operation has thrown any error.
-
- Completion Handlers: Many methods now include completion handlers for asynchronous operations. Update your code to handle these callbacks appropriately.
-
Consent String: Use
exportCMPInfo()
instead ofgetConsentString()
to retrieve the consent information. - Vendor and Purpose Consents: The methods to get vendor and purpose consents now return arrays of IDs. You may need to adjust your logic to handle these arrays.
-
US Privacy String: The
getUSPrivacyString()
method has been deprecated. If you were using this for CCPA compliance, please note that this method is not available anymore. -
Consent Requirement Check: Use the new
checkIfConsentIsRequired(completion:)
method to automatically determine if consent is needed before showing the consent layer.
Methods and signatures changes
1. Initialization
- Old:
CmpManager.initialize(context: Context, cmpConfig: CmpConfig)
- New:
CmpManager.createInstance(context: Context, cmpConfig: CmpConfig)
2. Set UI Configuration
- Old:
.withCmpViewControllerConfigurationBlock { ... }
- New: The option is not provided, but you can create a wrapper activity and control the lifecycle and appearance on your end of the code.
3. Checking consent
- Old:
check({ ... }, isCached: Bool)
, - New:
checkIfConsentIsRequired(completion: (Error?) -> Void)
3. Checking and opening consent layer if needed
- Old:
checkAndOpenConsentLayer()
- New:
checkWithServerAndOpenIfNecessary(completion: (Error?) -> Void)
4. Open Consent Layer
- Old:
openConsentLayer()
- New:
openConsentLayer(completion: (Result<Unit>) -> Unit)
5. Accept All Consents
- Old:
acceptAll(callback: ConsentCallback)
- New:
cceptAll(completion: (Result<Unit>) -> Unit)
6. Reject All Consents
- Old:
rejectAll(onFinish: () -> Unit)
- New:
rejectAll(completion: (Result<Unit>) -> Unit)
7. Enable Purposes
- Old:
enablePurposeList(purposes: List<String>, updateVendor: Boolean, onConsentReceivedCallback: OnConsentReceivedCallback?)
- New:
acceptPurposes(purposes: List<String>, updatePurpose: Boolean, completion: (Result<Unit>) -> Unit)
8. Disable Purposes
- Old:
disablePurposeList(purposes: List<String>, updateVendor: Boolean, onConsentReceivedCallback: OnConsentReceivedCallback?)
- New:
rejectPurposes(purposes: List<String>, updateVendor: Boolean, completion: (Result<Unit>) -> Unit)
9. Enable Vendors
- Old:
enableVendorList(vendors: List<String>, onConsentReceivedCallback: OnConsentReceivedCallback?)
- New:
acceptVendors(vendors: List<String>, completion: (Result<Unit>) -> Unit)
10. Disable Vendors
- Old:
disableVendorList(vendors: List<String>, onConsentReceivedCallback: OnConsentReceivedCallback?)
- New:
rejectVendors(vendors: List<String>, completion: (Result<Unit>) -> Unit)
11. Get All Purposes
- Old:
getAllPurposes(): List<String>
- New:
getAllPurposesIDs(): List<String>
12. Get Enabled Purposes
- Old:
getEnabledPurposes(): List<String>
- New:
getEnabledPurposesIDs(): List<String>
13. Get All Vendors
- Old:
getAllVendors(): List<String>
- New:
getAllVendorsIDs(): List<String>
14. Get Enabled Vendors
- Old:
getEnabledVendors(): List<String>
- New:
getEnabledVendorsIDs(): List<String>
15. Check Purpose Consent
- Old:
hasPurposeConsent(String): Boolean
- New:
hasPurposeConsent(id: String): Boolean
16. Check Vendor Consent
- Old:
hasVendorConsent(String): Boolean
- New:
hasVendorConsent(id: String): Boolean
17. Export CMP String
- Old:
exportCmpString() : String
- New:
exportCMPInfo(): String
18. Import CMP String
- Old:
importCmpString(consentString: String, completionHandler: ((Error?) -> Unit)?)
- New:
importCMPInfo(cmpString: String, completion: (Result<Unit>) -> Unit)
19. Reset Consent Data
- Old:
reset()
- New:
resetConsentManagementData(completion: (Result<Unit>) -> Unit)
Deprecated Methods:
calledThisDay(): Boolean
getConsentstring(): String
getGoogleACString(): String
getUSPrivacyString(): String
initialize(context: Context, cmpConfig: CmpConfig)
setCallbacks(...)
withGoogleAnalyticsCallback(analyticsListener: CmpGoogleAnalyticsInterface)
Migration examples
Kotlin
// ============================================
// Previous versions
// ============================================
class CmpDemoActivity : FragmentActivity() {
private lateinit var cmpManager: CmpManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val config = CmpConfig.apply {
id = "<YOUR-CONSENTMANAGER-APP-ID>" // example: b238acdf1a
domain = "<YOUR-CONSENTMANAGER-APP-DOMAIN>" // example: delivery.consentmanager.net
appName = "<YOUR-CONSENTMANAGER-APP-NAME>" // example: testApp
language = "<YOUR-CONSENTMANAGER-APP-LANGUAGE>" // example: DE
}
cmpManager = CmpManager.createInstance(this, config)
cmpManager.initialize(this)
}
}
// ============================================
// SDK v3 implementation
// ============================================
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
val urlConfig = UrlConfig(
id = "09cb5dca91e6b",
domain = "delivery.consentmanager.net",
language = "EN",
appName = "CMDemoAppKotlin"
)
// This UI Config for Android is limited, but you have the option to create an activity wrapper
// to have full control over the appearance and the position
val webViewConfig = ConsentLayerUIConfig(
position = ConsentLayerUIConfig.Position.FULL_SCREEN, // that's the only position available for Android
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")
}
}
Java
// ===================================================
// Previuous versions
// ===================================================
public class CmpDemoActivity extends AppCompatActivity {
private CmpManager cmpManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CmpConfig cmpConfig = CmpConfig.INSTANCE;
cmpConfig.setId("<YOUR-CONSENTMANAGER-APP-ID>"); // example: a000aaaa1a
cmpConfig.setDomain("<YOUR-CONSENTMANAGER-APP-DOMAIN>"); // example: delivery.consentmanager.net
cmpConfig.setAppName("<YOUR-CONSENTMANAGER-APP-NAME>"); // example: testApp
cmpConfig.setLanguage("<YOUR-CONSENTMANAGER-APP-LANGUAGE>"); // example: EN
cmpConfig.setTimeout(4000);
cmpManager = CmpManager.createInstance(this, cmpConfig);
cmpManager.initialize(this)
}
}
// ===========================================
// SDK v3 implementation
// ===========================================
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cmp_demo);
UrlConfig urlConfig = new UrlConfig(
"09cb5dca91e6b",
"delivery.consentmanager.net",
"EN",
"CMDemoAppJava"
);
// This UI Config for Android is limited, but you have the option to create an activity wrapper
// to have full control over the appearance and the position
ConsentLayerUIConfig webViewConfig = new ConsentLayerUIConfig(
ConsentLayerUIConfig.Position.FULL_SCREEN,
ConsentLayerUIConfig.BackgroundStyle.dimmed(Color.BLACK, 0.5f),
10f,
true,
false
);
JavaCMPManager cmpManager = JavaCMPManager.getInstance(this, urlConfig, webViewConfig, this);
cmpManager.setActivity(this);
cmpManager.checkWithServerAndOpenIfNecessary(result -> {
if (result.isSuccess()) {
} else {
Log.e("JavaDemoAp", "Initialize method failed with error: " + result.exceptionOrNull());
}
return null;
});
}
private void showCMPDemoScreen() {
Intent intent = new Intent(this, CMPDemoActivity.class);
startActivity(intent);
finish();
}
@Override
public void didShowConsentLayer() {
Log.d("CMP JavaDemoAp", "Consent Layer open message received.");
}
@Override
public void didCloseConsentLayer() {
Log.d("CMP JavaDemoAp", "Consent Layer close message received.");
}
@Override
public void didReceiveError(@NonNull String error) {
Log.e("CMP JavaDemoAp", "SDK error: " + error);
}
@Override
public void didReceiveConsent(@NonNull String consent, @NonNull JsonObject jsonObject) {
Log.d("CMP JavaDemoAp", "Consent received: " + consent);
runOnUiThread(this::showCMPDemoScreen);
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
@Override
public void addMenuProvider(@NonNull MenuProvider provider, @NonNull LifecycleOwner owner, @NonNull Lifecycle.State state) {
}
}