Apps

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 Main.java, 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.

main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#000000"
    android:gravity="center">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text"
        android:textSize="14sp"
        android:textColor="#ffffff" />
</LinearLayout>

3. The final step is to override the on create method of our /src/Main.java file. Here I am pulling the brand property of the build. On the emulator, this returns the word "generic" all in lower case.

Main.java
package com.authorwjf.amianemulator;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class Main extends Activity {
   @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       TextView tv = (TextView)findViewById(R.id.text);
       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.

About

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...

3 comments
jwronski
jwronski

A lot of words to bring attention to android.os.Build. While you were in there, did you notice any other interesting fields? Any usable info in BOARD, DEVICE, FINGERPRINT, HARDWARE, MANUFACTURER, MODEL, PRODUCT? I can't think of a situation where the distinction between hardware and emulator is needed. Yes, I may need to know if the device has WIFI or bluetooth or other characteristics not available on an emulator, but all of these can be determined by other means.

aflynnhpg
aflynnhpg

I know what I'm going to be doing in my spare time today. Thanks for the article.

ProjectJourneyman
ProjectJourneyman

I primarily use BUILD.MODEL to check the specific device (e.g. Kindle Fire, Nook models, etc.). BUILD.MANUFACTURER is also useful, with a little less resolution. In some cases, BUILD.PRODUCT is recommended. Research is needed for each specific model you want to identify, because not every manufacturer makes it easy. One more that I haven't used, BUILD.SERIAL, is available in Android 2.3+.