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?
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
They are not identical.
There may be many buildType
s, 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With