Android 反编译中的 $assertionsDisabled

很久以前看了一些apk经过反编译后得到的 .dex 格式文件,发现有assertionsDisabled, 不解记录之,今天翻看以前的工作日志,于是写个程序验证。

.field static final $assertionsDisabled Z

.method static <clinit>()V
.limit registers 1
.line 85
 16     const-class v0,org/lytsing/android/Assert
 17 ; v0 : Ljava/lang/Class;
 18     invoke-virtual  {v0},java/lang/Class/desiredAssertionStatus ; desiredAssertionStatus()Z
 19 ; v0 : Ljava/lang/Class;
 20     move-result v0
 21 ; v0 : single-length
 22     if-nez  v0,l1c6ac
 23 ; v0 : single-length
 24     const/4 v0,1
 25 ; v0 : single-length
 26 l1c6a6:
 27     sput-boolean    v0,org/lytsing/android/Assert$assertionsDisabled Z
 28 ; v0 : Z
 29     return-void
 30 l1c6ac:
 31     const/4 v0,0
 32 ; v0 : single-length
 33     goto    l1c6a6
 34 .end method

在某个函数调用:

110     .line 153
111     sget-boolean v0, Lorg/lytsing/android/Assert;->$assertionsDisabled:Z
112 
113     if-nez v0, :cond_0
114 
115     array-length v0, p2
116 
117     array-length v1, p4
118 
119     if-eq v0, v1, :cond_0
120 
121     new-instance v0, Ljava/lang/AssertionError;
122 
123     invoke-direct {v0}, Ljava/lang/AssertionError;-><init>()V
124 
125     throw v0

好像很复杂, 其实仅仅一行代码:

assert conIds.length == stringIds.length;

就产生了上面一坨的代码。

If you enjoyed this post, make sure you subscribe to my RSS feed!

One thought on “Android 反编译中的 $assertionsDisabled

Leave a Reply

Your email address will not be published. Required fields are marked *