how does eclipse optimize your android code

according to android reference(http://androidappdocs.appspot.com/reference/android/util/Config.html), we know that

boolean DEBUG. If this is a debug build, this field will be true.

boolean LOGD. This constant is deprecated. Always true.

boolean LOGV. This constant is deprecated. Always false.

 

I wrote hello.java in eclipse and use android SDK to build.

        setContentView(R.layout.main);
        if (Config.DEBUG) {
            Log.d("================", "Config.DEBUG=" + Config.DEBUG);
        }
        if (Config.LOGD) {
            Log.d("================", "Config.LOGD=" + Config.LOGD);
        }
        if (Config.LOGV) {
            Log.d("================", "Config.LOGV=" + Config.LOGV);
        }
        if (false) {
            Log.d("================", "false");
        }
        if (true) {
            Log.d("================", "true");
        }
by decompile hello.class, we can see how does it optimize the code during build.
1, if the value is false at build time, the code will be omit, such as Config.LOGV.
2, if the value is true at build time, the conditional judgement will be omit, such as Config.LOGD.
3, if the value is unknown at build time, the conditional judgement will not be omit. it will read the value at run time, such as Config.DEBUG.
//setContentView(R.layout.main);
     8  invokevirtual hello.world.hello.setContentView(int) : void [19]
//Log.d("================", "Config.DEBUG=" + Config.DEBUG);
    11  getstatic android.util.Config.DEBUG : boolean [23]
    14  ifeq 41
    17  ldc <String "================"> [29]
    19  new java.lang.StringBuilder [31]
    22  dup
    23  ldc <String "Config.DEBUG="> [33]
    25  invokespecial java.lang.StringBuilder(java.lang.String) [35]
    28  getstatic android.util.Config.DEBUG : boolean [23]
    31  invokevirtual java.lang.StringBuilder.append(boolean) : java.lang.StringBuilder [38]
    34  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [42]
    37  invokestatic android.util.Log.d(java.lang.String, java.lang.String) : int [46]
    40  pop
//Log.d("================", "Config.LOGD=" + Config.LOGD);
    41  ldc <String "================"> [29]
    43  ldc <String "Config.LOGD=true"> [52]
    45  invokestatic android.util.Log.d(java.lang.String, java.lang.String) : int [46]
    48  pop
//Log.d("================", "true");
    49  ldc <String "================"> [29]
    51  ldc <String "true"> [54]
    53  invokestatic android.util.Log.d(java.lang.String, java.lang.String) : int [46]
    56  pop
conclusion:
1, eclipse can do necessary optimize at build time.
2, value of Config.DEBUG in android SDK is uncertain at build time.
question:
android SDK is a debug build or release build?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值