Emanuel Moecklin Emanuel Moecklin - 4 months ago 13
Android Question

How to determine if an app is running on Android M (Developer Preview)

Note: this question was asked for the Android M developer preview, not the official Android 6.0.

android.os.Build.VERSION.SDK_INT returns 22 on Android M (22 is the api level for LOLLIPOP_MR1). How are we supposed to check whether the app runs on Android M? I had something like this in mind but that obviously doesn't work:

public boolean isAndroidMOrHigher() {
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.MNC;

Why do I need this? E.g. to check whether I need to request permissions because with Android M there's a new permission model.

I could do this:

public boolean isAndroidMOrHigher() {
return "M".equalsIgnoreCase(Build.VERSION.RELEASE);

But that seems to be a hack IMO.

There are really two questions:

  • What's the cleanest way to do this?

  • Is this a bug or is this supposed to be like that?


What's the cleanest way to do this?

UPDATE: Google code samples show a different option: "MNC".equals(Build.VERSION.CODENAME).

Personally, I'm looking at PREVIEW_SDK_INT, if it exists:

static private boolean putMarzipanInYourPiePlateBingo() {
  boolean result=false;

    try {
      Field f=Build.VERSION.class.getField("PREVIEW_SDK_INT");

      if (f.getInt(null)>0) {
    catch (NoSuchFieldException e) {
      // no problem, must really be API 22
    catch (IllegalAccessException e) {
      // ummm... this shouldn't happen


This is more cumbersome than your M check. However, if Google decides to play yet more games (e.g., change Build.VERSION.RELEASE), I think this approach will be more stable.

Is this a bug or is this supposed to be like that?

One man's bug is another man's working-as-intended.