I have read this stackoverflow thread already and I tried using the code given in that answer to find out if I run my code on the emulator or on a real device:
import android.content.ContentResolver;
import android.provider.Settings.Secure;
...
mTextView.setText(Secure.getString(getContentResolver(), Secure.ANDROID_ID));
On my real device it returns "2bccce3...", however on the emulator it does not return null, but also a string "bd9f8..."
Ideas how to find out if emulator or real device from code would be highly appreciated
Android apps are typically not meant to be run on emulators by their users. Such behaviour can therefore be a sign of a possible attack or reverse engineering attempt. Malwarelytics for Android is able to detect that the app is running on an emulator and can be configured to terminate the app in such case.
An Android Virtual Device (AVD) is a configuration that defines the characteristics of an Android phone, tablet, Wear OS, Android TV, or Automotive OS device that you want to simulate in the Android Emulator. The Device Manager is an interface you can launch from Android Studio that helps you create and manage AVDs.
In the Android Studio toolbar, select your app from the run configurations drop-down menu. From the target device drop-down menu, select the device that you want to run your app on. Select Run ▷. This will launch the app on your connected device.
In general there are three ways to bypass an emulator check: Modify the app and remove the emulator check. Modify the emulator so that it pretends to be a real device. Modify the system calls the app does for detecting it is running on an emulator.
This should do it:
boolean inEmulator = false;
String brand = Build.BRAND;
if (brand.compareTo("generic") == 0)
{
inEmulator = true;
}
EDIT:
boolean inEmulator = "generic".equals(Build.BRAND.toLowerCase());
With the advent of the new Intel native emulator the above mentioned methods did not work any longer. Now I am using this code snippet which works on both Intel and ARM emulators:
if (Build.MODEL.contains("google_sdk") ||
Build.MODEL.contains("Emulator") ||
Build.MODEL.contains("Android SDK")) {
RunsInEmulator = true;
}
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