Running on emulator and on real device, I initialized Admob as such for testing ads:
Log.d("AppLog", "adding current device/emulator as test device...")
val deviceIds = arrayListOf(AdRequest.DEVICE_ID_EMULATOR)
getDeviceIdForAdMobTestAds(context)?.let { deviceIds.add(it.uppercase(Locale.US)) }
MobileAds.setRequestConfiguration(RequestConfiguration.Builder().setTestDeviceIds(deviceIds).build())
Log.d("AppLog", "success being test device? ${AdRequest.Builder().build().isTestDevice(context)}")
@SuppressLint("HardwareIds")
private fun getDeviceIdForAdMobTestAds(context: Application): String? {
val md5 = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
try {
val md = MessageDigest.getInstance("MD5")
val array = md.digest(md5.toByteArray())
val sb = StringBuilder()
for (i in array.indices) sb.append(Integer.toHexString(array[i].toInt() and 0xFF or 0x100).substring(1, 3))
return sb.toString()
} catch (_: NoSuchAlgorithmException) {
}
return null
}
However, when I try to start the ad-inspector , I get this error:
"Code": 2,
"Message": "Ad inspector cannot be opened because the device is not in test mode. See https:\/\/
developers.google.com\/admob\/android\/test-ads#enable_test_devices for more information.",
"Domain": "com.google.android.gms.ads",
"Cause": "null"
Please fix this.
Even the link doesn't show correctly in the error.
Gradle config:
classpath 'com.google.gms:google-services:4.4.0'
implementation 'com.google.android.gms:play-services-ads:22.4.0'