How to distinguish an emulator from a physical device?



  • I test Xamarin mobile applications for Android and iOS, tests are carried out both on real devices and on emulators.
    However, recently it became necessary to distinguish between a test run on an emulator and on a physical device.

    Question: Is there a way in the process of testing .apk or .ipa to understand where the application is running - on an emulator or on a real device?
    Are there any system parameters that always differ from the real device on the emulator?



  • These lines will return true on the emulator:

    Build.PRODUCT.equals ("google_sdk");
    Build.MODEL.equals ("google_sdk");
    Build.HARDWARE.equals ("goldfish");
    Build.MANUFACTURER.equals ("unknown");
    

    These lines are for C#:

    global :: Android.OS.Build.Product
    global :: Android.OS.Build.Model
    global :: Android.OS.Build.HardWare
    global :: Android.OS.Build.Manufacturer
    But this is not always the case.
    

    You can try like this (Java example):

    if (Build.PRODUCT.matches (". * _? sdk _?. *")) {
      // - emulator -
    } else {
      // - other device -
    }
    

    Another way (Java):

    TelephonyManager tm = (TelephonyManager) getSystemService (Context.TELEPHONY_SERVICE);
    String networkOperator = tm.getNetworkOperatorName ();
    if ("Android" .equals (networkOperator)) {
        // Emulator
    }
    else {
        // Device
    }
    

    That is, if you need to be 100% sure that this is an emulator, you will have to combine methods. For example @AndroidCrop function (Java):

    public static final boolean isEmulator () {
    
    int rating = 0;
    
    if ((Build.PRODUCT.equals ("sdk")) || (Build.PRODUCT.equals ("google_sdk"))
            || (Build.PRODUCT.equals ("sdk_x86")) || (Build.PRODUCT.equals ("vbox86p"))) {
        rating ++;
    }
    if ((Build.MANUFACTURER.equals ("unknown")) || (Build.MANUFACTURER.equals ("Genymotion"))) {
        rating ++;
    }
    if ((Build.BRAND.equals ("generic")) || (Build.BRAND.equals ("generic_x86"))) {
        rating ++;
    }
    if ((Build.DEVICE.equals ("generic")) || (Build.DEVICE.equals ("generic_x86")) || (Build.DEVICE.equals ("vbox86p"))) {
        rating ++;
    }
    if ((Build.MODEL.equals ("sdk")) || (Build.MODEL.equals ("google_sdk"))
            || (Build.MODEL.equals ("Android SDK built for x86"))) {
        rating ++;
    }
    if ((Build.HARDWARE.equals ("goldfish")) || (Build.HARDWARE.equals ("vbox86"))) {
        rating ++;
    }
    if ((Build.FINGERPRINT.contains ("generic / sdk / generic"))
            || (Build.FINGERPRINT.contains ("generic_x86 / sdk_x86 / generic_x86"))
            || (Build.FINGERPRINT.contains ("generic / google_sdk / generic"))
            || (Build.FINGERPRINT.contains ("generic / vbox86p / vbox86p"))) {
        rating ++;
    }
    
    return rating> 4;
    
    }
    


Suggested Topics

  • 2
  • 4
  • 5
  • 1
  • 2