Android 程式碼品質工具集成與應用

  • 2020 年 3 月 16 日
  • 筆記

Android 程式碼品質工具集成(checkstyle,findbugs,infer,PMD,Android lint)

1.程式碼檢查工具介紹:

image.png

程式碼檢查工具通過對源程式碼的掃描,檢查程式碼風格,識別程式碼中的錯誤,幫助我們改進程式碼品質。

Android 常用的程式碼檢查插件有:

——————————————————————————————————————————————————————————  名稱            作用                                     分析方式  ——————————————————————————————————————————————————————————  checkstyle       檢查程式碼風格 ,變數,注釋      分析源程式碼  findbugs        程式碼缺陷,Java語法使用缺陷       分析編譯過程中的 .class  PMD             程式碼缺陷                          分析源程式碼  infer           程式碼缺陷,空指針等                編譯過程分析  android lint     數組,性能,圖標,布局等           分析靜態源程式碼  ——————————————————————————————————————————————————————————

2.持續集成環境的演示

2.1 通過 Jenkins 集成

http://172.22.80.8:8080/job/qingqi_serialport_tool~CHECK/

2.2 檢查結果展示

image.png

2.3 郵件通知展示

image.png

3.如何使用和集成到 CI

Grade 提供了 checkstyle,findbugs , PMD, 等程式碼檢查工具的插件,直接配置規則即可。

下面以 checkstyle 工具為例演示一個集成過程:

3.1 工程結構

腳本存放在 scripts 文件夾下

image.png

checkstyle.gradle gradle 腳本配置,關鍵在這裡

huawei_CheckStyle.xml 規則配置文件

suppressions.xml 規則配置文件

3.2 編寫 grade 腳本

checkstyle.gradle 文件內容如下

image.png

3.3 在 android module 里啟用

在 app 主項目 module 下 的 build.gradle 中添加如下:

 apply from: '../scripts/checkstyle/checkstyle.gradle'

3.4 執行檢查

./gradlew clean check

4. 研發流程改進

簡單就是:

開發人員 push 到倉庫 —> git 倉庫觸發器通知 —> Jenkins 觸發構建 —> 產出 reports —> fix 缺陷

image.png

5. 參考

Checkstyle: https://www.jianshu.com/p/0b70a7928033 PMD 示例: https://www.jianshu.com/p/5fe23ed8d147 Android Lint: https://www.jianshu.com/p/e0de4c1bc747 Infer: https://www.jianshu.com/p/2d5cd6304ee7

Tags: