如何在代码中检测我处于发布模式还是调试模式?
当前回答
最简单、最好的长期解决方案是使用BuildConfig.DEBUG。这是一个布尔值,调试版本为真,否则为假:
if (BuildConfig.DEBUG) {
// do something for a debug build
}
有报道说,这个值在基于eclipse的构建中不是100%可靠的,尽管我个人没有遇到过问题,所以我不能说它到底有多大的问题。
如果你正在使用Android Studio,或者如果你在命令行中使用Gradle,你可以在BuildConfig中添加你自己的东西,或者调整调试和发布构建类型,以帮助在运行时区分这些情况。
The solution from Illegal Argument is based on the value of the android:debuggable flag in the manifest. If that is how you wish to distinguish a "debug" build from a "release" build, then by definition, that's the best solution. However, bear in mind that going forward, the debuggable flag is really an independent concept from what Gradle/Android Studio consider a "debug" build to be. Any build type can elect to set the debuggable flag to whatever value that makes sense for that developer and for that build type.
其他回答
我正在使用这种解决方案,以防发现我的应用程序正在调试版本上运行。
if (BuildConfig.BUILD_TYPE.equals("debug")){
//Do something
}
if (BuildConfig.DEBUG) {
}
这对我很有用
确保您导入了正确的BuildConfig类 是的,你将没有问题使用:
if (BuildConfig.DEBUG) {
//It's not a release version.
}
最简单、最好的长期解决方案是使用BuildConfig.DEBUG。这是一个布尔值,调试版本为真,否则为假:
if (BuildConfig.DEBUG) {
// do something for a debug build
}
有报道说,这个值在基于eclipse的构建中不是100%可靠的,尽管我个人没有遇到过问题,所以我不能说它到底有多大的问题。
如果你正在使用Android Studio,或者如果你在命令行中使用Gradle,你可以在BuildConfig中添加你自己的东西,或者调整调试和发布构建类型,以帮助在运行时区分这些情况。
The solution from Illegal Argument is based on the value of the android:debuggable flag in the manifest. If that is how you wish to distinguish a "debug" build from a "release" build, then by definition, that's the best solution. However, bear in mind that going forward, the debuggable flag is really an independent concept from what Gradle/Android Studio consider a "debug" build to be. Any build type can elect to set the debuggable flag to whatever value that makes sense for that developer and for that build type.
试试下面的方法:
boolean isDebuggable = ( 0 != ( getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
科特林:
val isDebuggable = 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE
这是从这里的邦德尔邮局拍下来的
推荐文章
- 在XML中“图像上缺少contentDescription属性”
- 在Android SQLite中处理日期的最佳方法
- 读取Android APK的包名
- Android-Facebook应用程序的键散列
- 登出时,清除活动历史堆栈,防止“返回”按钮打开已登录的活动
- 如何改变标题的活动在安卓?
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE