Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect when an Android application is running in the emulator?

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?

like image 346
Joe Ludwig Avatar asked May 09 '10 20:05

Joe Ludwig


People also ask

Can Android emulator be detected?

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.

How do I know if an app is running or not?

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.


2 Answers

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" 
like image 169
android developer Avatar answered Oct 01 '22 01:10

android developer


One common one sems to be Build.FINGERPRINT.contains("generic")

like image 40
Aleadam Avatar answered Oct 01 '22 00:10

Aleadam