Two ways to judge whether an Android application is in Debug mode

Two ways to judge whether an Android application is in Debug mode


Sometimes we want to judge whether the application is a debug version in the code, so as to shield or do some special processing, for example, 1. Print the log; 2. If it is the crash information in Debug mode, we need to mark it out, etc.

So how to judge the Debug mode?

Use BuildConfig.DEBUG to judge

The easiest way:

BuildConfig.DEBUG

Using BuildConfig.DEBUG can easily judge whether the current package is Debug package or Release package, so as to carry out logical processing.

However, when our project is an SDK, this method is not acceptable. The BuildConfig.DEBUG in our released SDK is a Release package, and BuildConfig.DEBUG is always false.

So how to judge whether the package where the SDK is located is Debug?

Use android:debuggable to judge

We can use android:debuggable to determine whether the application is in a debuggable state, and its corresponding flag is ApplicationInfo.FLAG_DEBUGGABLE:

if (0 != (getContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)){
            //Debug 模式是打开状态
        }

It can be set in build.gradle:

debug {
            debuggable true
        }

difference between the two

BuildConfig.DEBUG

BuildConfig.DEBUG indicates whether the currently compiled APP is a Debug package, which is automatically generated and set by the compiler.

android:debuggable

android:debuggable indicates whether the application can be debugged (even when running on a user-mode device). Set to "true" if debugging is possible, otherwise set to "false". The default value is "false".

The android:debuggable tag will eventually be set in the application tag in the AndroidManifest:

<application android:debuggable=["true" | "false"]
. . .

It represents the tunable state of the APP.

connection between the two

BuildConfig.DEBUG is related to android:debuggable: if you set android:debuggable to false, BuildConfig.DEBUG will always be false no matter whether the APP is a Debug package or not.

set up:

debug {
            debuggable true
        }

After that, the value of BuildConfig.DEBUG is always false.

Supongo que te gusta

Origin blog.csdn.net/u011578734/article/details/129560703
Recomendado
Clasificación