Bubble ZOO Escape Anouncement

  Hi folks! I am proud to present: 
Bubble Zoo Escape, my first Android game built using AndEngine 
If you have a time, please download and give feedback!

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

Oct 25

Written by: Goran Siric
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;
}

5 comment(s) so far...


Gravatar

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

Thanks for this information. I used it to dynamically load my debug map layout when I am testing my app.

By Ed Kawas on   10/13/2012 6:59 PM
Gravatar

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

Great article!

By Sujit on   1/17/2013 1:00 PM
Gravatar

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

Such a nice article. Exactly the thing I was looking for. Thank you very much Goran Siric.

By Dattatray Z on   4/3/2014 5:20 PM
Gravatar

&= is wrong

should use (appinfo.flags & ApplicationInfo.FLAG_DEBUGGABLE)

By hurelhuyag on   4/24/2014 9:16 AM
Gravatar

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

Hi hurelhuyag,
thanks for info :)

By Goran Siric on   5/5/2014 2:22 PM

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 

Bubble Zoo Escape

  Hi folks! I am proud to present: 
Bubble Zoo Escape, my first Android game built using AndEngine 
If you have a time, please download and give feedback!

Search Blog

If you found this useful,
you can buy me a coffe :)
By me a coffe through PayPal :)

Featured articles

AndEngine - Textures tips and tricks

What you should know abot textures before get started programming your first game.

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

Unity start guide
AndEngine - Textures tips and tricks
Calculated variables
Comments about Parking SMS Scheduler
Comments about cities and parking zones
Android "DataEntry ListView" Design Pattern
Android application and the Internet

Recent Comments

Re: Creating Android button with image and text using relative layout
What if, using the drawable left method, i want to increase the distance of the icon from the left limit of the button, making it a bit more central?
Re: Creating Android button with image and text using relative layout
Hi Shameel,try to use ImageView android:scaleType property in xml button layout:

android:scaleType="centerInside"

For more info see: developer.android.com/reference/android/widget/ImageView.ScaleType.html
Re: Creating Android button with image and text using relative layout
Hi bro,

Can u tell me how to set the size of icon according to our requirement in button ?

Actually my icon size is 256x256 and i want 20x20 ???
Re: Creating Android button with image and text using relative layout
Hi dude, i have also find out one good example

How To Create Button

Re: Creating Android button with image and text using relative layout
Hello, I am new for android.I want to move text on image view. how its possible?
Plz help me.Thanks in advance