This is a lightweight Kotlin-based utility class to easily implement Google UMP (User Messaging Platform) Consent Form for GDPR/EEA compliance in your Android apps.
Supports:
- ✅ Consent request using Google UMP SDK
- ✅ Debug mode for testing in EEA
- ✅ Callback support to load ads after consent
- ✅ Privacy options support
- ✅ Easy integration with Dagger Hilt
- ✅ User Messaging Platform Dependency
- ✅ Dagger/Hilt Integration
ump = { group = "com.google.android.ump", name = "user-messaging-platform", version.ref = "ump_version" }Add following depedencies and plugins to libs.versions.toml
Dependencies:
dagger-hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "daggerHilt_version" }
dagger-ksp = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "daggerHilt_version" }
Plugin:
hilt = { id = "com.google.dagger.hilt.android", version.ref = "daggerHilt_version" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp_version" }
Versions:
daggerHilt_version = "2.55"
ksp_version = "2.1.0-1.0.29"
Plugins:
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
Dependencies:
implementation(libs.dagger.hilt)
ksp(libs.dagger.ksp)
Plugin:
alias(libs.plugins.hilt) apply false
alias(libs.plugins.ksp) apply false
@HiltAndroidApp
class MyApp : Application()
<application
android:name=".MyApp"
... >
</application>
Initialize UMP in your activity as below:
@Inject latinit var googleConsentFormManager: GoogleConsentFormManager
class MainActivity : AppCompatActivity(), GoogleConsentFormManager.UmpCallbacks {
override fun onRequestAds() {
// Consent is gathered — load ads now
}
override fun onConsentFormError(error: String) {
// Handle any error that occurred
Log.e("ConsentError", error)
}
}
consentManager.setConsentCallbacks(this)
consentManager.gatherConsentIfAvailable(this) { consentGiven ->
Log.d("ConsentResult", "Consent gathered: $consentGiven")
}
consentManager.showPrivacyOptionsForm(this) { formError ->
Log.e("PrivacyFormError", "Error: ${formError.message}")
}
consentManager.consentReset()
To test consent form in debug mode (EEA simulation)
val id = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
val hashed = MessageDigest.getInstance("MD5").digest(id.toByteArray()).joinToString("") {
"%02x".format(it)
}
Log.d("DeviceHash", hashed)
val debugSettings = ConsentDebugSettings.Builder(it)
.setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId("YOUR-DEVICE-HASHED-ID")
.build()