首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我可以在Android设备上使用assert吗?

我可以在Android设备上使用assert吗?
EN

Stack Overflow用户
提问于 2010-03-03 00:45:40
回答 9查看 32K关注 0票数 89

我想在我的安卓应用程序中使用关键字,在某些情况下在模拟器上销毁我的应用程序,或者在测试期间销毁我的设备。这个是可能的吗?

仿真器似乎忽略了我的断言。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-03-10 18:10:02

该接口提供了JUnit Assert

你可以做到

代码语言:javascript
复制
import static junit.framework.Assert.*;

现在您可以使用junit框架中提供的所有函数,如assertTrue、assertEquals和assertNull。

注意不要通过eclipse导入Junit4框架,这将是org.junit包。您必须使用junit.framework包才能使其在安卓设备或模拟器上工作。

票数 -7
EN

Stack Overflow用户

发布于 2011-04-06 16:41:48

请参阅嵌入式虚拟机控制文档(来自source tree的原始超文本标记语言或nicely formatted副本)。

基本上,Dalvik VM默认设置为忽略断言检查,即使.dex字节码包含执行检查的代码也是如此。检查断言可以通过以下两种方式之一打开:

(1)通过以下方式设置系统属性"debug.assert“:

代码语言:javascript
复制
adb shell setprop debug.assert 1

我验证过,只要你在这样做后重新安装你的应用程序,它就能正常工作,或者

(2)向dalvik VM发送命令行参数"--enable-assert“,这可能不是应用程序开发人员能够做的事情(如果我错了,有人纠正我)。

基本上,有一个可以全局设置的标志,可以在包级别设置,也可以在类级别设置,从而在相应级别启用断言。默认情况下,该标志为off,因此将跳过断言检查。

我在我的示例活动中编写了以下代码:

代码语言:javascript
复制
public class AssertActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int x = 2 + 3;
    assert x == 4;
  }
}

对于这段代码,生成的dalvik字节码是(对于Android 2.3.3):

代码语言:javascript
复制
// Static constructor for the class
000318:                                        |[000318] com.example.asserttest.AssertActivity.:()V
000328: 1c00 0300                              |0000: const-class v0, Lcom/example/asserttest/AssertActivity; // class@0003
00032c: 6e10 0c00 0000                         |0002: invoke-virtual {v0}, Ljava/lang/Class;.desiredAssertionStatus:()Z // method@000c
000332: 0a00                                   |0005: move-result v0
000334: 3900 0600                              |0006: if-nez v0, 000c // +0006
000338: 1210                                   |0008: const/4 v0, #int 1 // #1
00033a: 6a00 0000                              |0009: sput-boolean v0, Lcom/example/asserttest/AssertActivity;.$assertionsDisabled:Z // field@0000
00033e: 0e00                                   |000b: return-void
000340: 1200                                   |000c: const/4 v0, #int 0 // #0
000342: 28fc                                   |000d: goto 0009 // -0004

:
:

// onCreate()
00035c:                                        |[00035c] com.example.asserttest.AssertActivity.onCreate:(Landroid/os/Bundle;)V
00036c: 6f20 0100 3200                         |0000: invoke-super {v2, v3}, Landroid/app/Activity;.onCreate:(Landroid/os/Bundle;)V // method@0001
000372: 1501 037f                              |0003: const/high16 v1, #int 2130903040 // #7f03
000376: 6e20 0500 1200                         |0005: invoke-virtual {v2, v1}, Lcom/example/asserttest/AssertActivity;.setContentView:(I)V // method@0005
00037c: 1250                                   |0008: const/4 v0, #int 5 // #5
00037e: 6301 0000                              |0009: sget-boolean v1, Lcom/example/asserttest/AssertActivity;.$assertionsDisabled:Z // field@0000
000382: 3901 0b00                              |000b: if-nez v1, 0016 // +000b
000386: 1251                                   |000d: const/4 v1, #int 5 // #5
000388: 3210 0800                              |000e: if-eq v0, v1, 0016 // +0008
00038c: 2201 0c00                              |0010: new-instance v1, Ljava/lang/AssertionError; // class@000c
000390: 7010 0b00 0100                         |0012: invoke-direct {v1}, Ljava/lang/AssertionError;.:()V // method@000b
000396: 2701                                   |0015: throw v1
000398: 0e00                                   |0016: return-void



Notice how the static constructor invokes the method desiredAssertionStatus on the Class object and sets the class-wide variable $assertionsDisabled; also notice that in onCreate(), all of the code to throw java.lang.AssertionError is compiled in, but its execution is contingent upon the value of $assertionsDisabled which is set for the Class object in the static constructor.

It appears that JUnit's Assert class is what is used predominantly, so it is likely a safe bet to use that. The flexibility of the assert keyword is the ability to turn on assertions at development time and turn them off for shipping bits and instead fail gracefully.

Hope this helps.
票数 145
EN

Stack Overflow用户

发布于 2012-08-27 16:36:59

启用断言后,当布尔表达式为false时,assert关键字只抛出一个AssertionError

所以,国际海事组织,最好的选择,特别是。如果你不喜欢依赖junit,那就是显式抛出一个AssertionError,如下所示:

代码语言:javascript
复制
assert x == 0 : "x = " + x;

上述语句的另一种替代方法是:

代码语言:javascript
复制
Utils._assert(x == 0, "x = " + x);

其中该方法定义为:

代码语言:javascript
复制
public static void _assert(boolean condition, String message) {
    if (!condition) {
        throw new AssertionError(message);
    }
}

Oracle java将抛出AssertionError作为可接受的替代方案的recommend文档。

我猜您可以配置Proguard来剥离生产代码的这些调用。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2364910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档