במהלך הפיתוח והבדיקות של האפליקציה, יכול להיות שיהיה לכם שימושי להשבית את Performance Monitoring.
לדוגמה, אם משביתים את Performance Monitoring במהלך תהליך build של האפליקציה, אפשר:
להשבית פונקציות מסוימות של Performance Monitoring (כמו אלה שמסופקות על ידי Performance Monitoring Gradle plugin) בגרסאות build לניפוי באגים, אבל להפעיל מחדש את הפונקציות בגרסת build להפצה.
משביתים את Performance Monitoring כשמפתחים את האפליקציה, אבל מאפשרים לאפליקציה להפעיל אותה מחדש בזמן הריצה.
משביתים את Performance Monitoring כשמפתחים את האפליקציה, ולא מאפשרים לאפליקציה להפעיל אותה מחדש בזמן הריצה.
אפשר גם ליצור את האפליקציה עם Performance Monitoring מופעל, אבל להשתמש ב-Firebase Remote Config כדי להשיג גמישות בהשבתה (והפעלה מחדש) של Performance Monitoring באפליקציה שלכם בסביבת הייצור. באמצעות האפשרות הזו, אתם יכולים אפילו להגדיר את האפליקציה כך שהמשתמשים יוכלו להביע הסכמה לשימוש ב-Performance Monitoring או לבטל את ההסכמה.
השבתה של Performance Monitoring במהלך תהליך build של האפליקציה
אפשר להשבית את Performance Monitoring במהלך תהליך build על ידי השבתת הפלאגין Performance Monitoring Gradle או על ידי השבתת הספרייה Performance Monitoring Android.
במהלך פיתוח וניפוי באגים, השבתת התוסף מועילה כי אינסטרומנטציה על ידי התוסף יכולה להאריך את משך זמן של תהליך build. עם זאת, כדאי להשאיר את הספרייה מופעלת כדי שעדיין תוכלו לראות את נתוני הביצועים מהפעלת האפליקציה, האפליקציה בחזית והאפליקציה ברקע, וגם את המעקב אחר קוד בהתאמה אישית באפליקציה.
השבתת הפלאגין Performance Monitoring Gradle
כדי להשבית את התוסף Performance Monitoring, מוסיפים דגל instrumentationEnabled
באמצעות האפשרויות הבאות:
Extension Property – משבית את הפלאגין עבור וריאנט build ספציפי בזמן משך הזמן לקימפול
Project Property – משבית את הפלאגין עבור כל וריאנט build בזמן הקומפילציה
השבתת הפלאגין באמצעות דגל של מאפיין תוסף
באמצעות הדגל Extensions Property, אפשר להשבית את הפלאגין Performance Monitoring עבור וריאנט build ספציפי בזמן הקימפול.
בקובץ Gradle ברמת הבסיס (ברמת הפרויקט) (
<project>/build.gradle.ktsאו<project>/build.gradle), מוודאים שהתלות בפלאגין של Android Gradle מוגדרת כגרסה 3.4.0 ואילך.בגרסאות קודמות של פלאגין של Android Gradle, עדיין אפשר להשבית את הפלאגין Performance Monitoring עבור וריאנט build ספציפי, אבל לא תהיה אפשרות לבטל לגמרי את התרומה של הווריאנט הזה למשך זמן של תהליך build.
מוסיפים את הדגל הבא לקובץ Gradle של המודול (ברמת האפליקציה) (בדרך כלל
<project>/<app-module>/build.gradle.ktsאו<project>/<app-module>/build.gradle), ואז מגדירים אותו לערךfalseכדי להשבית את הפלאגין Performance Monitoring.Kotlin
import com.google.firebase.perf.plugin.FirebasePerfExtension // ... android { // ... buildTypes { getByName("debug") { configure<FirebasePerfExtension> { // Set this flag to 'false' to disable @AddTrace annotation processing and // automatic monitoring of HTTP/S network requests // for a specific build variant at compile time. setInstrumentationEnabled(false) } } } }
Groovy
android { // ... buildTypes { debug { FirebasePerformance { // Set this flag to 'false' to disable @AddTrace annotation processing and // automatic monitoring of HTTP/S network requests // for a specific build variant at compile time. instrumentationEnabled false } } } }
השבתת הפלאגין באמצעות דגל של מאפיין פרויקט
באמצעות הדגל Project Property, אפשר להשבית את הפלאגין Performance Monitoring עבור כל הווריאציות של ה-build בזמן הקימפול.
מוסיפים את הדגל הבא לקובץ gradle.properties, ואז מגדירים אותו לערך false כדי להשבית את הפלאגין Performance Monitoring.
// ... // Set this flag to 'false' to disable @AddTrace annotation processing and // automatic monitoring of HTTP/S network requests // for all build variants at compile time. firebasePerformanceInstrumentationEnabled=false
השבתת ספריית Android Performance Monitoring
אם משביתים את ספריית Performance Monitoring בזמן ההידור, אפשר לבחור אם לאפשר לאפליקציה להפעיל את הספרייה בזמן הריצה.
השבתה של הספרייה בזמן ההידור, אבל אפשרות להפעיל אותה באפליקציה בזמן הריצה
מוסיפים את רכיב <meta-data> הבא לקובץ AndroidManifest.xml של האפליקציה:
<application>
<meta-data
android:name="firebase_performance_collection_enabled"
android:value="false" />
</application>השבתה של הספרייה בזמן ההידור, אבל לא מאפשרים לאפליקציה להפעיל אותה בזמן הריצה
מוסיפים את רכיב <meta-data> הבא לקובץ AndroidManifest.xml של האפליקציה:
<application>
<meta-data
android:name="firebase_performance_collection_deactivated"
android:value="true" />
</application>השבתת האפליקציה בזמן הריצה באמצעות Remote Config
Firebase Remote Config מאפשרת לכם לשנות את ההתנהגות והמראה של האפליקציה, ולכן היא דרך אידיאלית להשבית את Performance Monitoring במופעים של האפליקציה שפרסתם.
כדי להשבית את איסוף הנתונים של Performance Monitoring בפעם הבאה שהאפליקציה ל-Android תופעל, משתמשים בקוד לדוגמה שמוצג בהמשך. מידע נוסף על השימוש ב-Remote Config באפליקציה ל-Android זמין במאמר שימוש ב-Firebase Remote Config ב-Android.
מוודאים שהשורה Remote Config נמצאת בקטע
dependenciesבקובץ Gradle של המודול (ברמת האפליקציה) (בדרך כלל<project>/<app-module>/build.gradle.ktsאו<project>/<app-module>/build.gradle):Kotlin
implementation("com.google.firebase:firebase-config-ktx:23.0.1")Java
implementation("com.google.firebase:firebase-config:23.0.1")מגדירים את Remote Config ומשביתים את Performance Monitoring אם
perf_disableמוגדר ל-true:Kotlin
// Setup remote config val config = Firebase.remoteConfig // You can uncomment the following two statements to permit more fetches when // validating your app, but you should comment out or delete these lines before // distributing your app in production. // val configSettings = remoteConfigSettings { // minimumFetchIntervalInSeconds = 3600 // } // config.setConfigSettingsAsync(configSettings) // Load in-app defaults from an XML file that sets perf_disable to false until you update // values in the Firebase Console // Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true config.setDefaultsAsync(R.xml.remote_config_defaults) .addOnCompleteListener { task -> if (task.isSuccessful) { Firebase.performance.isPerformanceCollectionEnabled = !config.getBoolean("perf_disable") } else { // An error occurred while setting default parameters } }
Java
// Setup remote config final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance(); // You can uncomment the following two statements to permit more fetches when // validating your app, but you should comment out or delete these lines before // distributing your app in production. // FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() // .setMinimumFetchIntervalInSeconds(3600) // .build(); // config.setConfigSettingsAsync(configSettings); // Load in-app defaults from an XML file that sets perf_disable to false until you update // values in the Firebase Console //Observe the remote config parameter "perf_disable" and disable Performance Monitoring if true config.setDefaultsAsync(R.xml.remote_config_defaults) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { if (config.getBoolean("perf_disable")) { FirebasePerformance.getInstance().setPerformanceCollectionEnabled(false); } else { FirebasePerformance.getInstance().setPerformanceCollectionEnabled(true); } } else { // An error occurred while setting default parameters } } });
מוסיפים את הקוד הבא אל
MainActivity.javaכדי לאחזר ולהפעיל ערכים של Remote Config:Kotlin
// Remote Config fetches and activates parameter values from the service val config = Firebase.remoteConfig config.fetch(3600) .continueWithTask { task -> if (!task.isSuccessful) { task.exception?.let { throw it } } config.activate() } .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Parameter values successfully activated // ... } else { // Handle errors } }
Java
//Remote Config fetches and activates parameter values from the service final FirebaseRemoteConfig config = FirebaseRemoteConfig.getInstance(); config.fetch(3600) .continueWithTask(new Continuation<Void, Task<Boolean>>() { @Override public Task<Boolean> then(@NonNull Task<Void> task) throws Exception { if (!task.isSuccessful()) { throw task.getException(); } return config.activate(); } }) .addOnCompleteListener(new OnCompleteListener<Boolean>() { @Override public void onComplete(@NonNull Task<Boolean> task) { if (task.isSuccessful()) { // Parameter values successfully activated // ... } else { // Handle errors } } });
כדי להשבית את Performance Monitoring במסוף Firebase, יוצרים פרמטר perf_disable בפרויקט של האפליקציה ומגדירים את הערך שלו ל-
true.השינוי הזה יהפוך את הקריאות ל-Performance Monitoring SDK לקריאות מסוג 'ללא פעולה' (NOOP), וכך ימנע השפעות משמעותיות על ביצועי האפליקציה כתוצאה משימוש ב-Performance Monitoring SDK באפליקציה.
אם מגדירים את הערך של perf_disable ל-
false, Performance Monitoring נשאר מופעל.