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?
本文探讨了Android SDK中Config类的DEBUG、LOGD和LOGV字段的使用方式及其在编译过程中的优化策略。通过代码示例展示了这些字段如何影响最终的APK大小,并讨论了它们在debug和release构建中的行为差异。
6832

被折叠的 条评论
为什么被折叠?



