382

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?

4
  • 2
    Might have a look at android.os.Build.
    – yanchenko
    May 9, 2010 at 21:25
  • 16
    Amaze me... Google should have a standard way of doing this?
    – powder366
    Apr 11, 2014 at 19:41
  • @kreker what is issue, you are facing in existing solutions? Oct 1, 2018 at 6:07
  • @Khemraj fraud issues. Evil guy can mock some sensors and change some strings to pretend real device
    – k4dima
    Oct 2, 2018 at 8:46

41 Answers 41

1
2
3

Try using this method.

Tested on Google and Genymotion emulators.

public Boolean IsVM() {
    String radioVersion = android.os.Build.getRadioVersion();
    return radioVersion == null || radioVersion.isEmpty() || radioVersion.equals("1.0.0.0");
}
2
  • This works but generate false positives on real Tablets.
    – icaro56
    May 3, 2022 at 23:59
  • @icaro56, can you provide more details on the false positives? May 9, 2022 at 9:00
2

Since the underlying emulation engine for Genymotion is VirtualBox and that's not going to change any time soon I found the following code the most reliable:

   public static boolean isGenymotion() {
        return Build.PRODUCT != null && Build.PRODUCT.contains("vbox");
}
2

It is a good idea to verify if the device has these packages installed:

    mListPackageName.add("com.google.android.launcher.layouts.genymotion");
    mListPackageName.add("com.bluestacks");
    mListPackageName.add("com.vphone.launcher");
    mListPackageName.add("com.bignox.app");

I just put it inside an ArrayList...

And then simply check against the package manager till it finds one.

private static boolean isEmulByPackage(Context context) {
        final PackageManager pm = context.getPackageManager();
        for (final String pkgName : mListPackageName) {
            return isPackageInstalled(pkgName, pm);
        }
        return false;
    }

private static boolean isPackageInstalled(final String packageName, final PackageManager packageManager) {
    try {
        packageManager.getPackageInfo(packageName, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

Just note that there are most likely some ways for a VM to fool the application, in that case it might be worthwhile looking at some physical sensors which wouldn't be present in a virtual device.

2
if ("sdk".equals( Build.PRODUCT )) {
 // Then you are running the app on the emulator.
        Log.w("MyAPP", "\n\n  Emulator \n\n"); 
}

Edit:

Tested on 2023-04 Android Studio IDE emulator and on real device.

 public static boolean isRunningOnEmulator() {
        return Build.PRODUCT.startsWith("sdk");
 }
1
if (Build.BRAND.equalsIgnoreCase("generic")) {
    // Is the emulator
}

All BUILD references are build.prop values, so you have to consider that if you are going to put this into release code, you may have some users with root that have modified theirs for whatever reason. There are virtually no modifications that require using generic as the brand unless specifically trying to emulate the emulator.

Fingerprint is the build compile and kernel compile signature. There are builds that use generic, usually directly sourced from Google.

On a device that has been modified, the IMEI has a possibility of being zeroed out as well, so that is unreliable unless you are blocking modified devices altogether.

Goldfish is the base android build that all other devices are extended from. EVERY Android device has an init.goldfish.rc unless hacked and removed for unknown reasons.

1

This worked for me instead of startsWith : Build.FINGERPRINT.contains("generic")

For more check this link: https://gist.github.com/espinchi/168abf054425893d86d1

1

try this link from github.

https://github.com/mofneko/EmulatorDetector

This module help you to emulator detection to your Android project suported Unity.

Basic checker

  • BlueStacks
  • Genymotion
  • Android Emulator
  • Nox App Player
  • Koplayer
  • .....
0

Most commonly used technique is to match values from brand,name ... etc. But this method is static and for a limited versions of emulators. What if there are 1000+ VM manufacturers ? then you would have to write a code to match 1000+ VMs ?

But its time waste. Even after sometime, there would be new other VMs launched and your script would be wasted.

So based upon my tests, I got know getRadioVersion() returns empty on VM, and returns version number on real android device.

public Boolean IsVM() 
{
    return android.os.Build.getRadioVersion().length() == 0;
} 
//return true if VM
//return false if real

Although it works, But I don't have official explanation for this.

Code: http://github.com/Back-X/anti-vm/blob/main/android/anti-vm.b4a

Release: http://github.com/Back-X/anti-vm/releases/download/1/anti-vm.apk

0

this method work for me

    public static boolean isRunningOnEmulator(String fingerprint, String hardware, String manufacturer) {
    boolean isEmulatorFingerprint = fingerprint.endsWith("test-keys");
    boolean isHardware = hardware.toLowerCase().contains("intel") || hardware.toLowerCase().contains("vbox");
    boolean isEmulatorManufacturer = manufacturer.equals("Genymotion")
            || manufacturer.equals("unknown");

    if (isHardware || isEmulatorFingerprint && isEmulatorManufacturer) {
        return true;
    } else {
        return false;
    }
}
-1

You may check if deviceId (IMEI) is "000000000000000" (15 zeroes)

2
  • 2
    The detection should be done without extra permissions.
    – ENSATE
    Mar 24, 2019 at 3:36
  • 2
    You can no longer get IMEI starting API 29
    – Ananth
    Oct 16, 2019 at 13:36
-1

In Android Studio under [Run > Edit Configurations... > Launch Flags]

In Launch Flags add the following...

--ei running_emulator 1

Then in your Activity onCreate

Bundle extras = getIntent().getExtras();
if (extras.getInt("running_emulator", 0) == 1) {
   //Running in the emulator!... Make USE DEV ENV!!
}

Easy!

1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.