How to check if your android application is running in debug or release mode ?

Oct 25

Written by:
10/25/2011 2:31 PM  RssIcon

There are some cases when you want to check if your Android application is running in debug or release mode. 

Testing if "android:debuggable" attribute is set to "true"


The best way you can do that is to use android:debuggable attribute defined in AndroidManifest.xml file. If you are using Eclipse or Ant developing environment and have installed Android SDK 8.0.1 or higher this attribute will be set automatically to true or false by the build tools. Other way is to set value of android:debuggable attribute manually to true or false, dependig of are you building debug or release version of your application.

Here is the example code you can use to check the value of debuggable attribute. 

private boolean isDebuggable(Context ctx)
{
    boolean debuggable = false;
 
    PackageManager pm = ctx.getPackageManager();
    try
    {
        ApplicationInfo appinfo = pm.getApplicationInfo(ctx.getPackageName(), 0);
        debuggable = (0 != (appinfo.flags &= ApplicationInfo.FLAG_DEBUGGABLE));
    }
    catch(NameNotFoundException e)
    {
        /*debuggable variable will remain false*/
    }
     
    return debuggable;
}


Testing if application is signed with  debug key

There are some other ways to check if you are running debug or release mode of application. You cant test if application package is signed with debug key.  Debug key is automatically generated by Android SDK tools while you are developing application. One way is to obtain your debug key signature, hardcode it into your code, and then test if your application is signed with this key. In my opinion this is not a good way to check debug/release mode, because it will not work if you copy your code to the another machine or if you share your code with other developers using some sort of versioning software. 

Here is the example code with hardcoding debug key:

// copy your debug key here from LogCat window using Ctrl+C after first execution of code
private static final String DEBUG_KEY = "copy debug key signature here";
 
private boolean isDebuggable(Context ctx)
{
     
    String TAG = "isDebuggable";
     
    boolean debuggable = false;
    try
    {
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),PackageManager.GET_SIGNATURES);
        Signature signatures[] = pinfo.signatures;
         
        for ( int i = 0; i < signatures.length;i++)
            Log.d(TAG,signatures[i].toCharsString());
         
        if (DEBUG_KEY.equals(signatures[0].toCharsString()))
        {
            debuggable = true;
        }
 
    }
    catch (NameNotFoundException e)
    {
        //debuggable variable will remain false
    }
 
    return debuggable;
}


According to the info in Android documentation  Signing Your Application, debug key contain following subject distinguished name:  "CN=Android Debug,O=Android,C=US".  We can use this information to test if package is signed with debug key without hardcoding debug key signature into our code.

Here is the example code for checking subject distinguihsed name of key:

private static final X500Principal DEBUG_DN = new X500Principal("CN=Android Debug,O=Android,C=US");
private boolean isDebuggable(Context ctx)
{
    boolean debuggable = false;
     
    try
    {
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(),PackageManager.GET_SIGNATURES);
        Signature signatures[] = pinfo.signatures;
         
        for ( int i = 0; i < signatures.length;i++)
        {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);       
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable)
                break;
        }
 
    }
    catch (NameNotFoundException e)
    {
        //debuggable variable will remain false
    }
    catch (CertificateException e)
    {
        //debuggable variable will remain false
    }
    return debuggable;
}


Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Search Blog

Featured articles

GIMP script for creating Android icons at once

Script for creating Android icons for different screen resolutions at once. Icons can be saved using standard Android icons naming conventions and saved in appropriate folders.

Creating Android button with image and text using relative layout

Source code with examples how to use relative layout to create nice buttons with text and images in Android

Android application and the Internet

Tutorial about connecting to the web pages on the Internet from Android application, using both POST and GET web requests.

Recent Entries

Android application and the Internet
Keep my DNN site alive
Android default icons
How to format code blocks in DotNetNuke 6
How to convert date and time to text in Android

Recent Comments

Flash The Net
Izvorni kod >Blog - How to force Eclipse to rebuild generated resource class (R) in Android project ?
# Flash The Net
Great Info.
Great information you got here. I've been reading about this topic for one week now for my papers in school and thank God I found it here in your blog. I had a great time reading this.
Re: GIMP script for creating Android icons at once
Thank you for supporting me :)
Re: GIMP script for creating Android icons at once
Thank you so much! Have donated some money to you so you can buy yourself a good beer.
You deserve it!
Re: GIMP script for creating Android icons at once
Hello all,
script is updated with some new features.

Added support for:
- action bar icons
- icons with custom size
- xhdpi screen icon

I hope that there is no bugs introduced.

Enjoy