主题:启用断言在Android上
断言可以被疯狂的从一个终端设备中运行,如果你是根。
su
setprop debug.assert 1
或者,你可以做同样的事情从主机使用adb:
adb shell setprop debug.assert 1
如果您正在开发在你的日常设备,这确实使谷歌地图更少,因为他们断言可用虽然在的地方,将会降低,应用程序。
增强断言在Android上
字节码对断言将呆在一个发布版本 ( http://www.zhouzhaoyiedu.com/)这样一个包装器可能会被要求做更多,例如日志文件和行错误甚至当assert关键字是禁用的。 例如:
private static final int ASSERTION_LEVEL = 1;
private static final boolean DEV_CODE_ENABLED = true;
// ...
public static void assert1(final boolean val)
{
if(ASSERTION_LEVEL >= 1 && DEV_CODE_ENABLED) {
assert val;
if(!val){
final StackTraceElement elem = callers_caller();
logE(elem.getClassName(), annotate_msg("ASSERTION FAILED (Level 1). Line: ", elem));
}
}
}
private static StackTraceElement callers_caller(){
final StackTraceElement[] trace = Thread.currentThread().getStackTrace();
final StackTraceElement elem = trace[4];
return elem;
}
/** Take a user message and decorate it with file and line number information.
* In release builds the decoration is omitted and the message passes through
* unchanged. */
private static final String annotate_msg(final String msg, final StackTraceElement elem){
final String safe_msg = msg != null ? msg : "";
if(DEV_CODE_ENABLED)
{
final String annotated_msg = safe_msg + ". On line: " + elem.getLineNumber() + " of file " + elem.getFileName();
return annotated_msg;
} else {
return safe_msg;
}
}