Friday, 7 June 2013

Test if an app is running on an android emulator


How do I test that my app is running on the Android emulator and not a real device?


import android.os.Build;


if( "sdk".equals( Build.PRODUCT ) ){
 // do emulator specific stuff here,
 // like set configs that are missing


Tested to work using ADT Build: v22.0.1-685705. Your mileage may vary with other versions.

Note that in some versions you might need to test that Build.PRODUCT is "google_sdk" or "sdk_x86" instead of "sdk". An easy way to find the string is to print the value of Build.PRODUCT to the log or console while running your app in the emulator, then use that string.

This test can be useful if one needs to set configurations that would normally work on a device by default (but be missing in the emulator, such as needing to call setEGLConfigChooser(...) prior to doing anything else with a GLSurfaceView, as of ADT Build: v22.0.1-685705).