Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguishing development mode and release mode environment settings on Android

The following solution assumes that in manifest file you always set android:debuggable=true while developing and android:debuggable=false for application release.

Now you can check this attribute's value from your code by checking the ApplicationInfo.FLAG_DEBUGGABLE flag in the ApplicationInfo obtained from PackageManager.

The following code snippet could help:

PackageInfo packageInfo = ... // get package info for your context
int flags = packageInfo.applicationInfo.flags; 
if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
    // development mode
} else {
    // release mode
}

According to this stackoverflow post, in SDK Tools version 17 (we're on 19 as of this writing) adds a BuildConfig.DEBUG constant that is true when building a dev build.


@viktor-bresan Thanks for a useful solution. It'd be more helpful if you just included a general way to retrieve the current application's context to make it a fully working example. Something along the lines of the below:

PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);

Android build.gradle has Handles Debug and Release Environment well.

Append the following code snippet in build.gradle file

buildTypes {
    debug {
        buildConfigField "Boolean", "IS_DEBUG_MODE", 'true'
    }

    release {
        buildConfigField "Boolean", "IS_DEBUG_MODE", 'false'
    }
}

Now you can access the variable like below

    if (BuildConfig.IS_DEBUG_MODE) { {
        //Debug mode.
    } else {
        //Release mode
    }