参考:
AndroidStudio 插件 之 Findbugs 安装与简单使用教程
FindBugs规则整理
详解FindBugs的各项检测器
下载插件
下载插件并重启android studio。
分析
类或者包右键
选择一个种类分析。
分析结果 :
dodgy code 糟糕的代码 [ˈdɒdʒi]
bad practice 坏习惯
internationalization 国际 ['ɪntə,næʃənəlaɪ'zeɪʃən]
performance 表现 [pəˈfɔ:məns]
correctness 正确性 [kə'rɛktnɪs]
experimental 实验性 [ɛk'spɛrɪ'mɛntəl]
malicious code vulnerability 恶意代码漏洞 [mə'lɪʃəs] [,vʌlnərə'bɪlətɪ]
multithreaded correctness 多线程正确性
问题一:
Switch statement found where default case is missing
Switch没有默认情况下执行的case语句。添加default即可。
switch末尾添加:
default:
break;
问题二:
casting from integer values:
Integral division result cast to double or float
整形数除法强制转换为double或者float类型。
int x = 2;
int y = 5;
// Wrong: yields result 0.0
double value1 = x / y;
// Right: yields result 0.4
double value2 = x / (double) y;
问题三:
misuse of static fields:滥用静态变量
Write to static field UserCenterActivity.sHead from instance method onActivityResult(int, int, Intent)
private static Bitmap sHead;
去掉static即可。
问题四:
Boxing/unboxing to parse a primitive toApplyReg()
//valueOf 返回 Integer
int code = Integer.valueOf(applyRegModel.code);//wrong
//parseInt 返回 int
int code = Integer.parseInt(applyRegModel.code);//right
问题五:
The code contains a conditional test is performed twice, one right after the other (e.g., x == 0 || x == 0).
Perhaps the second occurrence is intended to be something else (e.g., x == 0 || y == 0).
if (password_one.length() > 0 && password_two.length() > 0 && password_two.length() > 0) {
mButtonOK.setBackground(getResources().getDrawable(R.drawable.bg_btn_enabled));
mButtonOK.setEnabled(true);
} else {
mButtonOK.setBackground(getResources().getDrawable(R.drawable.bg_btn_disabled));
mButtonOK.setEnabled(false);
}
password_two.length判断重复,去掉一个即可。
问题六:
Method concatenates strings using + in a loop
在循环中构建一个String对象时从性能上讲使用StringBuffer来代替String对象
// This is bad
String s = "";
for (int i = 0; i < field.length; ++i) {
s = s + field[i];
}
// This is better
StringBuffer buf = new StringBuffer();
for (int i = 0; i < field.length; ++i) {
buf.append(field[i]);
}
String s = buf.toString();