BuildConfig.DEBUG与ApplicationInfo.FLAG_DEBUGGABLE

Ely*_*lye 7 java android android-build android-build-type android-flavors

由于指通过从代码获取androidManifest的"可调试"的价值?,有两种选项可以检查构建是否可调试:

1.)BuildConfig.DEBUG标志

 if (BuildConfig.DEBUG)`
Run Code Online (Sandbox Code Playgroud)

2.)ApplicationInfo.FLAG_DEBUGGABLE

 if (0 != (getContext().getApplicationInfo().flags & 
     ApplicationInfo.FLAG_DEBUGGABLE))
Run Code Online (Sandbox Code Playgroud)

它们是两个相同的,还是不同的?什么时候用?

azi*_*ian 9

它们不完全相同.

可能有很多buildTypes,但是debug并且release是强制性的.BuildConfig.DEBUG将是true当前选择的构建类型debug,否则它将是false(见下面的排除案例).

ApplicationInfo.FLAG_DEBUGGABLE 对应于以下内容:


    buildTypes {
        debug {
            debuggable true
        }

        ...
    }

现在,ApplicationInfo.FLAG_DEBUGGABLE将是true.

因此,您可以得出结论,您可以执行以下操作:


    buildTypes {
        debug {
            debuggable false
        }

        ...
    }

有趣的是,虽然你在debug构建类型,但BuildConfig.DEBUG将成为false.


Ely*_*lye 5

在这里找到一篇好文章:http://tekeye.biz/2013/android-debug-vs-release-build

也测试了一下。如果我们在清单应用程序上强制使用android:debuggable="false"or android:debuggable="true",它会发出警告:

 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.
Run Code Online (Sandbox Code Playgroud)

我的结论是,在默认情况下,ApplicationInfo.FLAG_DEBUGGABLE行为与 相同BuildConfig.DEBUG,除非通过更改覆盖android:debuggable,这是不可取的。

与 相比BuildConfig.DEBUGApplicationInfo.FLAG_DEBUGGABLE是检查调试构建的更可靠方法,因为在较低依赖模块中,它无法访问BuildConfig.DEBUG父模块的 ,并且可能具有不同的值。

例如应用程序使用 MyLib 模块。AppBuildConfig.DEBUG可能是假的,但 MyLibBuildConfig.DEBUG可能是真的。因此最好使用检查ApplicationInfo.FLAG_DEBUGGABLE