通过静态代码分析和运行时检查改善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.