9

In my app I have created a test page with some code that only will be available for the developer. This page/activity can only be reached via a menu-item. Is there a way to determine if an android app is running in debug mode, and only show the menu-item when this is true?

regards, Goldhorn

Jeroen
  • 307
  • 1
  • 6
  • 17

3 Answers3

13

To directly check debug mode in menu or another resource XML, we can define resValue in build.gradle like below.

buildTypes {
    release {
        ...
        resValue "bool", "DEBUG", "false"
    }
    debug {
        ...
        resValue "bool", "DEBUG", "true"
    }
}

In generated.xml we will get:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Automatically generated file. DO NOT MODIFY -->

    <!-- Values from build type: debug -->
    <bool name="DEBUG">true</bool>

</resources>

Now we can use this values and in menu.xml:

<item android:visible="@bool/DEBUG" ... />
marioosh
  • 27,328
  • 49
  • 143
  • 192
3

You need to check if the debuggable="true" is set in AndroidManifest.xml. See this post for more info - Getting "debuggable" value of androidManifest from code?

Community
  • 1
  • 1
Vino
  • 1,544
  • 1
  • 18
  • 26
-6

USE THIS HACK

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String nOp = tm.getNetworkOperatorName();
if("Android".equals(nOp)) {
    // ADD YOUR MENUS FOR EMULATOR
}
else {
    // ADD YOUR MENUS FOR DEVICE
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • 1
    I don't think this is exactly what I'm looking for. I am debugging on both emulator and device. So I don't want to know if the app is running in an emulator or on a device. I want to know if the app is running in debug mode or in 'production' mode – Jeroen Jul 29 '11 at 12:33
  • oh sorry ... Try @Vino's answer? – Sherif elKhatib Jul 29 '11 at 12:34
  • @Vino's answer was what I was looking for :D – Jeroen Jul 29 '11 at 12:56