I would like to have my code run slightly differently when running on the emulator than when running on a device. (For example, using 10.0.2.2 instead of a public URL to run against a development server automatically.) What is the best way to detect when an Android application is running in the emulator?
There is no official API in iOS or Android to detect an emulator. Therefore, several proprietary checks have to be done by the RASP system.
There are a few ways to see what apps are running in the background and consuming your Android's resources. Go to Settings > System > Developer Options. If you don't see Developer Options, scroll down and select About phone, then look for Build number and tap it seven times. Tap Running Services.
How about this solution (class implementation of SystemProperties
is available here):
val isProbablyRunningOnEmulator: Boolean by lazy { // Android SDK emulator return@lazy ((Build.FINGERPRINT.startsWith("google/sdk_gphone_") && Build.FINGERPRINT.endsWith(":user/release-keys") && Build.MANUFACTURER == "Google" && Build.PRODUCT.startsWith("sdk_gphone_") && Build.BRAND == "google" && Build.MODEL.startsWith("sdk_gphone_")) // || Build.FINGERPRINT.startsWith("generic") || Build.FINGERPRINT.startsWith("unknown") || Build.MODEL.contains("google_sdk") || Build.MODEL.contains("Emulator") || Build.MODEL.contains("Android SDK built for x86") //bluestacks || "QC_Reference_Phone" == Build.BOARD && !"Xiaomi".equals( Build.MANUFACTURER, ignoreCase = true ) //bluestacks || Build.MANUFACTURER.contains("Genymotion") || Build.HOST.startsWith("Build") //MSI App Player || Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic") || Build.PRODUCT == "google_sdk" // another Android SDK emulator check || SystemProperties.getProp("ro.kernel.qemu") == "1") }
Note that some emulators fake exact specs of real devices, so it might be impossible to detect it. I've added what I could, but I don't think there is a 100% way to detect if it's really an emulator or not.
Here a tiny snippet you can make in the APK to show various things about it, so you could add your own rules:
textView.text = "FINGERPRINT:${Build.FINGERPRINT}\n" + "MODEL:${Build.MODEL}\n" + "MANUFACTURER:${Build.MANUFACTURER}\n" + "BRAND:${Build.BRAND}\n" + "DEVICE:${Build.DEVICE}\n" + "BOARD:${Build.BOARD}\n" + "HOST:${Build.HOST}\n" + "PRODUCT:${Build.PRODUCT}\n"
One common one sems to be Build.FINGERPRINT.contains("generic")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With