An easy hack to see if your Android code executes on the emulator

William J. Francis shares a handy tip for detecting whether an app is running on an actual Android device or the emulator.

If you've written even the most elementary of Android programs, you've almost certainly used the Android emulator. Over the years working as an embedded software engineer I've had my hands on all sorts of emulators. When it comes to embedded programming, emulators are often an essential part of the software development life cycle (SDLC).

The emulator Google provides as part of the Android development kit is one of the best I've worked with. And yet, at the end of the day it is an emulator, not an actual Android device. Using an emulator means sooner or later there will come a time when you face a conundrum: Due to some limitation or critical difference between your physical target device and the emulator, you will want your code to behave one way on the emulator and another when deployed to the device. This begs the question: How can you determine programmatically whether an app is executing on the emulator or on an actual device?

To my knowledge, there is no officially supported mechanism for doing this. However, in this short tutorial, I will demonstrate the technique I frequently use in my own code. You can follow along with the step-by-step instructions provided, or download the entire project and import it directly into Eclipse.

1. Create a new Android project in Eclipse. Target Android 1.6 or higher. Change the name of the startup activity to, and the corresponding layout file to main.xml.

2. In the /res/layout folder define main.xml to include a single text view wrapped in a linear layout.

<LinearLayout xmlns:android=""
        android:textColor="#ffffff" />

3. The final step is to override the on create method of our /src/ file. Here I am pulling the brand property of the build. On the emulator, this returns the word "generic" all in lower case.
package com.authorwjf.amianemulator;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
    public void onCreate(Bundle savedInstanceState) {
       TextView tv = (TextView)findViewById(;
       if (Build.BRAND.equalsIgnoreCase("generic")) {
              tv.setText("YES, I am an emulator");
       } else {
              tv.setText("NO, I am NOT an emulator");
Go ahead and compile the APK and download it both to an emulator and your physical device. Ta-da! (Figure A) Figure A

Using the build brand as an indicator is one of many ways I've come across in the various Android forums for performing this test. My own first-hand experiences have proven this method the most reliable.

However, I stress again that it is not a Google sanctioned approach, and therefore is not guaranteed as future proof. To my knowledge, Google has not offered an alternate solution. When and if Google does, I will go back and refactor my code. Until then, I will continue to use the approach outlined in this post.


William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...

Editor's Picks