通過靜態代碼分析和運行時檢查改善Android代碼質量:
建議您在創建一個新項目並在每個簽到中解決問題(作為連續集成過程的一部分)時添加這些檢查。否則,當您面對並解決大量錯誤時,它將需要巨大的勇氣和耐心。
build.gradle中添加以下行: apply from: '../quality/static_analysis.gradle'
git remote add analysis https://github.com/BrianSpace/Android-Quality-Essentials.gitbuild.gradle中: apply from: '../analysis/quality/static_analysis.gradle'
android {
...
lintOptions {
// Turn off analysis progress reporting by lint
quiet true
// Stop the gradle build if errors are found
abortOnError true
// Do not ignore lint warnings
ignoreWarnings false
// Treat warnings as errors
warningsAsErrors true
// Ignore rules list
ignore 'GoogleAppIndexingWarning' // Remove this if the app support App Indexing
}
...
}
build.gradle中添加洩漏的依賴性: dependencies {
.. .
debugCompile ' com.squareup.leakcanary:leakcanary-android:1.5.1 '
releaseCompile ' com.squareup.leakcanary:leakcanary-android-no-op:1.5.1 '
testCompile ' com.squareup.leakcanary:leakcanary-android-no-op:1.5.1 '
.. .
}Application類中(不要忘記添加到清單中): public class AndroidQualityEssentialsApplication extends Application {
@ Override
public void onCreate () {
super . onCreate ();
if ( BuildConfig . DEBUG ) {
StrictMode . setThreadPolicy ( new StrictMode . ThreadPolicy . Builder ()
. detectAll ()
. penaltyDeath ()
. build ());
StrictMode . setVmPolicy ( new StrictMode . VmPolicy . Builder ()
. detectAll ()
. penaltyDeath () // If violations are in Android itself or 3rd-party libs, use penaltyLog.
. build ());
// Avoid the process dedicated to LeakCanary for heap analysis.
if (! LeakCanary . isInAnalyzerProcess ( this )) {
LeakCanary . install ( this );
}
}
...
}
}gradlew check以啟動靜態分析。static_analysis.gradle的項目的build/reports/目錄中。樣式規則中定義的命名約定不允許單個字母前綴“ mmember”之類的成員字段名稱。但是,如果您喜歡這種樣式,則可以將MemberName模塊的format屬性更改為“^[az] [A-ZA-Z0-9] $”。或更改為“^m [az] [a-za-Z0-9] $”,以強制“ M”前綴。
對於PMD規則使用式頻道,如果您的類只有靜態字段和方法,它將提示創建實用程序類。您只需要:
public final class FileUtil {
private static Context appContext ;
private FileUtil () throws InstantiationException {
throw new InstantiationException ( "Utility class FileUtil should not be instantiated!" );
}
public static void init ( final Context context ) {
appContext = context . getApplicationContext ();
}
/**
* Get available cache directory. Prefer external over internal.
*/
@ NonNull
public static File getAvailableCacheDir () {
final File externalCacheDir = appContext . getExternalCacheDir ();
return externalCacheDir == null ? appContext . getCacheDir () : externalCacheDir ;
}
}所有的靜態分析規則都是人們過去經驗的最佳實踐,但它們並不總是無法破壞的真理。一些PMD規則需要基於您自己的偏好:
ItemTypeT ,以提高可讀性。如果您喜歡單個字母命名,則可以從“排除”列表中刪除它。 命名約定在質量/checkStyle/naming_convention.xml文件中定義。定義了以下規則:
您可以根據自己的項目需要修改正則表達式。如果您只想檢查命名約定,請運行gradlew checkFileNames 。
CheckStyle用於檢查Java代碼樣式。樣式規則基於Google Java樣式指南,並具有以下更改:
如果您只想檢查代碼樣式,請運行gradlew checkCodeStyle 。如果您想排除第三方代碼之類的某些文件,則可以在static_analysis.gradle文件中的checkCodeStyle任務中添加一個exclude項目。
FindBugs掃描您的代碼是否可能導致錯誤。此處定義要排除分析的文件。如果您只想運行findbugs,請運行gradlew findBugs 。
PMD是一個靜態代碼分析儀,可以檢測常見的編程缺陷。規則以質量/PMD/PMD-Ruleset.xml定義。規則的完整列表可以在此處找到。如果您只想運行PMD,請運行gradlew pmdCheck 。
Android lint是一種特定於Android的靜態代碼分析工具。完整的檢查列表在這裡。如果您只想運行棉絨規則,請運行gradlew lint 。
嚴格模式對於檢測UI線程和資源洩漏中的緩慢操作非常有用。具體而言,ThreadPolicy將檢測磁盤/網絡I/O並在UI Thead中進行緩慢操作,而VMPolicy將檢測資源洩漏。有關詳細信息,請參閱文檔以獲取threadpolicy.builder and vmpolicy.builder。
洩漏可以幫助檢測內存洩漏。它具有非常好的UI來報告洩漏並顯示整個參考鏈,以便您可以輕鬆地找到用於修復洩漏的位置。
The MIT License
Copyright (c) 2017-2017 AndroidQualityEssentials project contributors
https://github.com/BrianSpace/AndroidQualityEssentials/graphs/contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.