Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out from code if my Android app runs on emulator or real device?

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

like image 925
Addi Avatar asked Jul 28 '11 19:07

Addi


People also ask

Can emulators be detected?

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.

Is a device configuration that runs on the Android emulator?

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.

How can I run Android apps instead of emulator?

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.

How do you bypass an app's ability to know is being run on an emulator rather than a real 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.


2 Answers

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());
like image 120
A. Abiri Avatar answered Oct 06 '22 01:10

A. Abiri


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;
}
like image 43
Ernie Avatar answered Oct 06 '22 01:10

Ernie