Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BuildConfig.DEBUG vs ApplicationInfo.FLAG_DEBUGGABLE

As refer by Getting "debuggable" value of androidManifest from code?, there are two options of checking if the build is debuggable:

1.) BuildConfig.DEBUG flag

 if (BuildConfig.DEBUG)`

2.) ApplicationInfo.FLAG_DEBUGGABLE

 if (0 != (getContext().getApplicationInfo().flags & 
     ApplicationInfo.FLAG_DEBUGGABLE))

Are they two identical, or they are different? When to use what?

like image 454
Elye Avatar asked Aug 11 '17 14:08

Elye


2 Answers

Found a good article here: http://tekeye.biz/2013/android-debug-vs-release-build.

Also tested it out. If we force android:debuggable="false" or android:debuggable="true" on Manifest Application, it will warn :

 Avoid hardcoding the debug mode; leaving it out allows debug and release builds to automatically assign one less...

 It's best to leave out the android:debuggable attribute from the manifest. If you do, then the tools will automatically insert android:debuggable=true when building an APK to debug on an emulator or device. And when you perform a release build, such as Exporting APK, it will automatically set it to false.  
 If on the other hand you specify a specific value in the manifest file, then the tools will always use it. This can lead to accidentally publishing your app with debug information.

I would conclude, in default situation ApplicationInfo.FLAG_DEBUGGABLE behave the same as BuildConfig.DEBUG, unless override by changing android:debuggable, which is not something advisable.

Compare to BuildConfig.DEBUG, ApplicationInfo.FLAG_DEBUGGABLE is a more reliable way to check for debug build, as at a lower dependent module, it can't access the BuildConfig.DEBUG of the parent module, and could have a different value.

e.g. App use MyLib module. App's BuildConfig.DEBUG could be false, but MyLib BuildConfig.DEBUG could be true. Hence it's better to check using ApplicationInfo.FLAG_DEBUGGABLE

like image 99
Elye Avatar answered Oct 20 '22 00:10

Elye


They are not identical.

There may be many buildTypes, but debug and release are mandatory. BuildConfig.DEBUG will be true if currently selected build type is debug, otherwise it will be false (see exclusion case below).

ApplicationInfo.FLAG_DEBUGGABLE corresponds to following:


    buildTypes {
        debug {
            debuggable true
        }

        ...
    }

Now, ApplicationInfo.FLAG_DEBUGGABLE will be true.

Thus, you may conclude, that you may perform following:


    buildTypes {
        debug {
            debuggable false
        }

        ...
    }

Interestingly, although you are in debug build type, BuildConfig.DEBUG will become false.

like image 12
azizbekian Avatar answered Oct 20 '22 00:10

azizbekian