静的コード分析とランタイムチェックで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] $」に変更できます。または、「^[az] [a-za-z0-9] $」に変更して、 「m」プレフィックスを強制します。
PMDルールのUSEUTILITYCLASSの場合、クラスに静的フィールドとメソッドのみがある場合、ユーティリティクラスを作成するように求められます。あなたはただする必要があります:
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のようなポストフィックスとして「t」文字を含む、より意味のある名前を好む。単一の文字の命名が好きな場合は、「除外」リストから削除できます。 命名規則は、Quality/CheckStyle/naming_convention.xmlファイルで定義されています。次のルールが定義されています。
自分のプロジェクトに必要な定期式を変更できます。命名規則のみをチェックする場合はgradlew checkFileNames実行してください。
CheckStyleは、Javaコードスタイルを確認するために使用されます。スタイルのルールは、次の変更を伴うGoogle Javaスタイルガイドに基づいています。
コードスタイルのみをチェックしたい場合はgradlew checkCodeStyle実行してください。サードパーティコードなどの一部のファイルを除外したい場合は、static_analysis.gradleファイルのcheckCodeStyleタスクにexcludeアイテムを追加できます。
FindBugsは、バグにつながる可能性のあるパターンをコードスキャンします。分析から除外されるファイルはここで定義されています。 FindBugsのみを実行したい場合はgradlew findBugsを実行してください。
PMDは、一般的なプログラミングの欠陥を検出できる静的コードアナライザーです。ルールは、Quality/PMD/PMD-RuleSet.xmlで定義されています。ルールの完全なリストはここにあります。 PMDのみを実行したい場合はgradlew pmdCheckを実行してください。
Android Lintは、Android固有の静的コード分析ツールです。チェックの完全なリストはこちらです。 Lintルールのみを実行したい場合はgradlew lintを実行してください。
StrictModeは、UIスレッドとリソースリークの遅い操作を検出するのに非常に役立ちます。具体的には、ThreadPolicyはUI Theadでのディスク/ネットワークI/Oおよび遅い操作を検出しますが、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.