تحسين جودة رمز 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 في وحدة التحكم لبدء التحليل الثابت.build/reports/ دليل المشروع الذي تقوم فيه بتطبيق static_analysis.gradle . لا تسمح اتفاقية التسمية المحددة في قواعد النمط ببادئة الحروف المفردة لأسماء حقل الأعضاء مثل "Mmember". ولكن إذا كنت تحب هذا النمط ، فيمكنك تغيير خاصية format وحدة MemberName إلى "^[AZ] [A-ZA-Z0-9] $". أو تغيير إلى "^m [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 ، لتحسين القراءة. يمكنك إزالتها من قائمة "استبعاد" إذا كنت تحب تسمية الحرف المفرد. يتم تعريف اتفاقيات التسمية في ملف الجودة/checkstyle/naming_convention.xml. يتم تعريف القواعد التالية:
يمكنك تعديل التعبيرات العادية كما تحتاج لمشروعك. Run gradlew checkFileNames إذا كنت ترغب في التحقق من اتفاقية التسمية فقط.
يتم استخدام CheckStyle للتحقق من نمط رمز Java. تعتمد قواعد النمط على دليل نمط Google Java مع التغييرات التالية:
قم بتشغيل gradlew checkCodeStyle إذا كنت ترغب في التحقق من نمط الكود فقط. إذا كنت ترغب في استبعاد بعض الملفات مثل رمز الطرف الثالث ، فيمكنك إضافة عنصر exclude في مهمة checkCodeStyle في ملف static_analysis.gradle.
FindBugs مسح الكود الخاص بك للأنماط التي قد تؤدي إلى الأخطاء. يتم تعريف الملفات المراد استبعادها من التحليل هنا. قم بتشغيل gradlew findBugs إذا كنت ترغب في تشغيل FindBugs فقط.
PMD هو محلل رمز ثابت يمكنه اكتشاف عيوب البرمجة الشائعة. يتم تعريف القواعد في الجودة/PMD/PMD-Ruleset.xml. يمكن العثور على قائمة كاملة بالقواعد هنا. قم بتشغيل gradlew pmdCheck إذا كنت ترغب في تشغيل PMD فقط.
Android Lint هي أداة تحليل رمز ثابت Android. القائمة الكاملة للشيكات هنا. قم بتشغيل gradlew lint إذا كنت ترغب في تشغيل قواعد Lint فقط.
يعد StrictMode مفيدًا جدًا لاكتشاف العمليات البطيئة في مؤشر ترابط واجهة المستخدم وتسريبات الموارد. على وجه التحديد ، سيقوم ThreadPolicy باكتشاف القرص/الشبكة I/O والعمليات البطيئة في واجهة المستخدم ، في حين أن VMPolicy ستكتشف تسرب الموارد. لمزيد من التفاصيل ، راجع المستندات الخاصة بـ threadpolicy.builder و vmpolicy.builder
يمكن أن يساعد LeakCanary في اكتشاف تسرب الذاكرة. يحتوي على واجهة مستخدم لطيفة للغاية للإبلاغ عن التسريبات وإظهار السلسلة المرجعية بأكملها بحيث يمكنك بسهولة تحديد مكان إصلاح التسريبات.
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.