How to restrict an APK not to get installed in Android Emulator/Simulator but in real device?

拥有回忆 提交于 2019-12-05 08:14:32

It's currently not possible to prevent APKs from being installed on a emulator. (it used to be possible by adding a sensor requirement to the app, but hese days emulators can emulate that too)

However, at runtime you should be able to validate if your app is running on a emulator using the following check:

if (android.os.Build.MODEL.equals(“google_sdk”)) {
   // emulator
} else {
   //not emulator
}

You could do this check before you create your DB and SharedPreference files etc.

There is no general way of doing this. The job of the emulator, after all, is to behave exactly like a real device as far as possible, including simulating inputs from cameras, GPS sensors, etc.

You may get somewhere if you set your manifest to have a dependency on some hardware which is not normally supported in the emulator. For example:

<uses-feature android:name="android.hardware.sensor.accelerometer" 
              android:required="true" />

But really, if you want to prevent people poking around in your app's stored data, the best solution is to encrypt or obfuscate it.

Use this function:

public static boolean isEmulator() {
    return 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")
            || Build.MANUFACTURER.contains("Genymotion")
            || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
            || "google_sdk".equals(Build.PRODUCT);
}

Therefor:

if (isEmulator()) {
   // emulator
} else {
   //not emulator
}

My recommendation:

try this from github.

Easy to detect android emulator

How to use with an Example:

EmulatorDetector.with(this)
                .setCheckTelephony(true)
                .addPackageName("com.bluestacks")
                .setDebug(true)
                .detect(new EmulatorDetector.OnEmulatorDetectorListener() {
                    @Override
                    public void onResult(boolean isEmulator) {

                    }
                });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!