如何在Android Runtime(ART)上启用语言级别的断言? [英] How can I enable language-level assertions on the Android Runtime (ART)?

查看:201
本文介绍了如何在Android Runtime(ART)上启用语言级别的断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在开发的Pixel-C。我的最低API级别是21,这也是ART替代Dalvik的级别。我已经尝试过两种方法:

I have a Pixel-C that I am developing for. My minimum API level is 21, which is also the level at which ART replaced Dalvik. I have tried both of:

adb shell setprop dalvik.vm.enableassertions all
adb shell setprop debug.assert 1

它们似乎成功执行了。我已经放置了

And they seem to execute successfully. I have placed

assert false : "assertions are active!";

在我的onStart中,并且在logcat中没有看到任何堆栈跟踪。我希望该应用程序在安装并运行后立即退出。请告诉我如何执行此断言。

in my onStart, and I am not seeing any stack traces in logcat. I would expect the app to exit immediately after I install and run it. Please tell me how to get this assertion to execute.

请不要提及JUnit或其他进行断言的方法,也不要提及任何需要显式抛出Error的解决方案。生产代码永远不要抛出错误,也不要试图捕获并处理它们。这就是为什么要在语言中添加断言的原因,以便当在 test 环境中违反不变式时导致应用程序崩溃而又不会在生产中造成任何开销或任何风险。

Please do not mention JUnit or other ways to do assertions, nor any solution that requires explicitly throwing an Error. Production code should never throw Errors, nor attempt to catch and handle them. That's why assertions were added to the language, to have a way to cause the app to crash when invariants are violated in test environments without incurring any overhead or risk whatsoever in production.

这个已有6年历史的问题基本相同,但对于Dalvik(即已过期的IE)而言,解决方案要么无效,要么效果不佳:
我可以在Android设备上使用assert吗?

This 6-year old question is basically the same, but for Dalvik (IE out of date) and the solutions are either not working or not good: Can I use assert on Android devices?

推荐答案

我很不情愿地认为答案似乎是:您不能在ART上启用断言。起作用的是用包裹在如下if语句中的明确抛出的AssertionError替换所有断言:

I reluctantly submit that the answer seems to be: you can't enable assertions on ART. What works is to replace all assertions with an explicitly thrown AssertionError wrapped in an if statement like this:

if (BuildConfig.DEBUG) {
  if (writeBuffer.hasRemaining()) {
    // As with all assertions, this condition should never be met.
    throw new AssertionError("whole buffer not written");
  }
}

显然,在API级别21、22和23中,ART将在安装后从非调试版本中完全删除此字节码,即BuildConfig.DEBUG == false。在这些API级别上,ART在安装时会将字节码编译为本机,但是对于Android N而言这已经改变。因此,我推断,在Android N上,ART在检查BuildConfig.DEBUG的生产中仍然可能会忽略不计的性能损失,直到优化程序可能对其进行编译为止

Apparently, in API levels 21, 22, and 23, ART will actually completely remove the bytecode for this if block from non-debug builds upon install, ie where BuildConfig.DEBUG == false. At these API levels, ART compiles bytecode to native on install, but that is changing for Android N. So I infer that on Android N, ART may still see the negligible performance penalty in production of checking BuildConfig.DEBUG until the optimizer potentially compiles it out after a certain amount of usage has occurred.

我不喜欢这样,因为它消除了选择为apk中的特定程序包运行断言的能力。现在的选择是整个构建的粒度,并且仅在构建时进行。

I don't like this because it removes the ability to choose to run assertions for a specific package in an apk. The choice now is at the granularity of the whole build, and only at build time.

另一个令人讨厌的主要原因是它冗长而丑陋。断言的简洁性使其非常适合内联记录代码。尽管这些骇人听闻的断言可以用作文档,但它们不再是强硬而清晰的。看那个例子。应该是一行,而不是五行。

The other major reason this sucks is that it's verbose and ugly. The brevity of assertions makes them good for documenting your code inline. Although these hacked-up assertions can serve as documentation, they're no longer unimposing and legible. Look at that example. That should be one line, not five.

如果您有一个想法,为什么ART似乎不支持断言,例如,关于技术障碍的内部知识或Google内部政治,请发表评论或留下新答案。我的假设是对断言的有用性和作用的广泛误解,以及反模式用法的普遍使用,导致Android团队只是禁用该功能,而不是教育所有人。也许Android团队会遭受相同的误解

If you have an idea why ART doesn't seem to support assertions, for example, inside knowledge about technical hurdles or Google's internal politics, please comment or leave a new answer. My assumption is that the widespread misunderstanding of the usefulness and role of assertions, and the prevalence of antipattern usage has led the Android team to just disable the feature rather than educate everybody. Maybe the Android team suffers from the same misunderstandings.

这篇关于如何在Android Runtime(ART)上启用语言级别的断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆