代碼優(yōu)化
對(duì)Bitmap的優(yōu)化:
Android 提高代碼質(zhì)量-多種檢測(cè)方案
lint
檢測(cè)范圍
- 潛在的bug
- 可優(yōu)化的代碼
- 安全性
- 性能
- 可用性
- 可訪問(wèn)性
- 國(guó)際化
插件使用
Android Studio 自帶。
通過(guò)Gradle運(yùn)行l(wèi)int
- 注意:此時(shí)如果檢測(cè)項(xiàng)目,應(yīng)該把a(bǔ)pp的gradle配置,和module的gradle配置,加上如下結(jié)構(gòu),否則報(bào)錯(cuò)
android {
...
lintOptions {
abortOnError false
}
...
}
- Windows
gradle lint
- Linux 或 mac
./gradlew lint
- 單獨(dú)檢測(cè)模塊文件或者某個(gè)目錄
AS工程選擇module,目錄或者文件,右鍵選擇Analyze----model----Inspect Code。然后配置就好了
具體查詢文檔即可,或者google搜索
findBugs
檢測(cè)范圍
針對(duì)java代碼
- 常見(jiàn)的代碼錯(cuò)誤,序列化錯(cuò)誤
- 肯能導(dǎo)致錯(cuò)誤的代碼,如空指針引用
- 國(guó)際化相關(guān)問(wèn)題:如錯(cuò)誤字符串轉(zhuǎn)換
- 可能受到惡意攻擊,如訪問(wèn)權(quán)限修飾符的定義等
- 多線程的正確性:如多線程編程時(shí)常見(jiàn)的同步,線程調(diào)度問(wèn)題。
- 運(yùn)行時(shí)性能問(wèn)題:如由變量定義,方法調(diào)用導(dǎo)致的代碼抵消問(wèn)題
插件使用
AS 里面插件安裝FindBugs-IDEA
添加plugin apply plugin:'findbugs'
定義任務(wù),指定輸出格式
參考文章
script 代碼
task findbugs(type: FindBugs) {
ignoreFailures = false
effort = "default"
reportLevel = "medium"
excludeFilter = new File("${project.rootDir}/findbugs/findbugs-filter.xml")
classes = files("${project.rootDir}/app/build/intermediates/classes")
source = fileTree('src/main/java/')
classpath = files()
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$project.buildDir/findbugs/findbugs-output.xml"
}
html {
destination "$project.buildDir/findbugs/findbugs-output.html"
}
}
}
不要忘記添加 findbugs/fingdbugs-filter.xml
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<!-- ignore all issues in resource generation -->
<Class name="~.*\.R\$.*"/>
</Match>
<Match>
<Class name="~.*\.Manifest\$.*"/>
</Match>
</FindBugsFilter>
如果報(bào)錯(cuò),添加如下節(jié)點(diǎn)即可解決
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
<Match>
<!-- ignore all issues in resource generation -->
<Class name="~.*\.R\$.*"/>
</Match>
<Match>
<Class name="~.*\.Manifest\$.*"/>
</Match>
<Class name="~.*\.*Test" />
<!-- test classes are suffixed by 'Test' -->
<Not>
<Bug code="IJU" /> <!-- 'IJU' is the code for bugs related to JUnit test code -->
</Not>
</FindBugsFilter>
PMD
它跟Findbugs類似,但是它不是檢測(cè)字節(jié)碼,它是直接檢測(cè)源代碼。它使用靜態(tài)分析來(lái)發(fā)現(xiàn)錯(cuò)誤。它們的檢測(cè)方法不同,可以取到互補(bǔ)的作用
檢測(cè)范圍
- 可能的bug——空的try/catch/finally/switch塊。
- 無(wú)用代碼(Dead code):無(wú)用的本地變量,方法參數(shù)和私有方法。
- 空的if/while語(yǔ)句。
- 過(guò)度復(fù)雜的表達(dá)式——不必要的if語(yǔ)句,本來(lái)可以用while循環(huán)但是卻用了for循環(huán)。
- 可優(yōu)化的代碼:浪費(fèi)性能的String/StringBuffer的使用。
插件使用與findBugs類似
task pmd(type: Pmd) {
ruleSetFiles = files("${project.rootDir}/config/quality/pmd/pmd-ruleset.xml")
ignoreFailures = false
ruleSets = []
source 'src'
include '**/*.java'
exclude '**/gen/**'
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$project.buildDir/reports/pmd/pmd.xml"
}
html {
destination "$project.buildDir/reports/pmd/pmd.html"
}
}
}
CheckStyles
這個(gè)工具用來(lái)自動(dòng)檢測(cè)java源碼。啟動(dòng)之后,可以按照制定的規(guī)則對(duì)java源碼進(jìn)行檢查,被將所有的不符合規(guī)范的地方生成報(bào)告通知給你。
檢測(cè)范圍
- 注解
- javadoc注釋
- 命名規(guī)范
- 文件頭
- 導(dǎo)入包規(guī)范
- 尺寸設(shè)置
- 空格
- 正則表達(dá)式
- 修飾符
- 代碼塊
- 編碼問(wèn)題
- 類設(shè)計(jì)問(wèn)題
- 重復(fù)、度量以及一些雜項(xiàng)
使用
導(dǎo)入plugin
apply plugin: 'checkstyle'
設(shè)置CheckStyle的版本
checkstyle {
toolVersion '6.1.1'
showViolations true
}
配置任務(wù)
task checkstyle(type: Checkstyle) {
ruleSetFiles = files("${project.rootDir}/checkstyle/zcw-checkstyle.xml")
ignoreFailures = false
ruleSets = []
source 'src'
include '**/*.java'
exclude '**/gen/**'
exclude 'androidTest/**'
exclude 'test/**'
}
未雨綢繆
主動(dòng)提前添加資源國(guó)際化劃分(即使你的程序很小,不可能發(fā)到國(guó)外,盡量提早支持,至少?gòu)募軜?gòu)和需求完整性考慮好的。一般中國(guó),韓國(guó),英語(yǔ),香港,澳門(mén) 就夠了)
對(duì)布局的動(dòng)態(tài)管理和拆分提前做。
內(nèi)存檢測(cè)
工具
bugtags
LeakCanary
請(qǐng)求更新調(diào)優(yōu)
Volley
OkHttp
Retrofit
HttpClient
待續(xù)。。。