ในระหว่างการพัฒนาและการทดสอบแอป คุณอาจเห็นว่าการปิดใช้ Performance Monitoring มีประโยชน์
เช่น การปิดใช้ Performance Monitoring ในระหว่างกระบวนการสร้างแอป จะช่วยให้คุณทำสิ่งต่อไปนี้ได้
ปิดใช้ฟังก์ชันการทำงานบางอย่างของ Performance Monitoring (เช่น ฟังก์ชันที่ปลั๊กอิน Performance Monitoring Gradle มีให้) ในการสร้างการแก้ไขข้อบกพร่อง แต่เปิดใช้ฟังก์ชันการทำงานอีกครั้งสำหรับการสร้างเวอร์ชันที่เผยแพร่
ปิดใช้ Performance Monitoring เมื่อสร้างแอป แต่ให้แอปเปิดใช้ Performance Monitoring อีกครั้งได้ ในขณะรันไทม์
ปิดใช้ Performance Monitoring เมื่อสร้างแอป และไม่อนุญาตให้แอป เปิดใช้ Performance Monitoring อีกครั้งในรันไทม์
คุณยังสร้างแอปโดยPerformance Monitoring เปิดใช้ได้ด้วย แต่ใช้ Firebase Remote Config เพื่อให้คุณมีความยืดหยุ่นในการปิดใช้ (และเปิดใช้ซ้ำ) Performance Monitoring ในแอปเวอร์ชันที่ใช้งานจริงได้ ตัวเลือกนี้ยังช่วยให้คุณกำหนดค่าแอปเพื่อให้ผู้ใช้เลือกใช้หรือไม่ใช้ Performance Monitoring ได้ด้วย
ปิดใช้ Performance Monitoring ในระหว่างกระบวนการสร้างแอป
คุณปิดใช้ Performance Monitoring ในระหว่างกระบวนการบิลด์ได้โดย ปิดใช้ปลั๊กอิน Gradle ของ Performance Monitoring และ/หรือโดย ปิดใช้คลัง Android ของ Performance Monitoring
ในระหว่างการพัฒนาและการแก้ไขข้อบกพร่อง การปิดใช้ปลั๊กอินจะมีประโยชน์เนื่องจาก การวัดผลโดยปลั๊กอิน อาจทำให้เวลาในการสร้างเพิ่มขึ้น อย่างไรก็ตาม คุณอาจพิจารณาเปิดใช้ไลบรารีไว้เพื่อให้ยังดูข้อมูลประสิทธิภาพจากการเริ่มต้นแอป แอปในเบื้องหน้า และการติดตามแอปในเบื้องหลัง รวมถึงการติดตามโค้ดที่กำหนดเอง ในแอปได้
ปิดใช้ปลั๊กอิน Performance Monitoring Gradle
คุณปิดใช้ปลั๊กอิน Performance Monitoring ได้โดยเพิ่มinstrumentationEnabled
แฟล็กโดยใช้ตัวเลือกต่อไปนี้
พร็อพเพอร์ตี้ส่วนขยาย — ปิดใช้ปลั๊กอินสำหรับ ตัวแปรบิลด์ที่เฉพาะเจาะจงในเวลาคอมไพล์
พร็อพเพอร์ตี้ของโปรเจ็กต์ — ปิดใช้ปลั๊กอินสำหรับตัวแปรบิลด์ทั้งหมดในเวลาคอมไพล์
ปิดใช้ปลั๊กอินผ่านค่าสถานะพร็อพเพอร์ตี้ส่วนขยาย
การใช้แฟล็ก Extensions Property จะช่วยให้คุณปิดใช้ปลั๊กอิน Performance Monitoring สำหรับตัวแปรบิลด์ที่เฉพาะเจาะจงในเวลาคอมไพล์ได้
ในไฟล์ Gradle ระดับรูท (ระดับโปรเจ็กต์) (
<project>/build.gradle.kts
หรือ<project>/build.gradle
) โปรดตรวจสอบว่าได้ระบุทรัพยากร Dependency ของปลั๊กอิน Android Gradle เป็น v3.4.0 ขึ้นไปสำหรับ Android Gradle Plugin เวอร์ชันก่อนหน้า คุณยังคงปิดใช้ Performance Monitoring ปลั๊กอินสำหรับตัวแปรบิลด์ที่เฉพาะเจาะจงได้ แต่เวลาบิลด์ สำหรับตัวแปรนั้นจะไม่หายไปโดยสมบูรณ์
เพิ่มค่าสถานะต่อไปนี้ลงในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ
<project>/<app-module>/build.gradle.kts
หรือ<project>/<app-module>/build.gradle
) จากนั้นตั้งค่าเป็นfalse
เพื่อปิดใช้ปลั๊กอิน Performance MonitoringKotlin
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 } } } }
ปิดใช้ปลั๊กอินผ่าน Flag ของพร็อพเพอร์ตี้โปรเจ็กต์
การใช้แฟล็ก Project Property จะช่วยให้คุณปิดใช้ปลั๊กอิน Performance Monitoring สำหรับตัวแปรบิลด์ทั้งหมดในเวลาคอมไพล์ได้
เพิ่มค่าสถานะต่อไปนี้ลงในไฟล์ 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
ปิดใช้Performance Monitoringไลบรารี Android
หากปิดใช้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.0")
Java
implementation("com.google.firebase:firebase-config:23.0.0")
ตั้งค่า 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
การเปลี่ยนแปลงนี้จะทำให้การเรียกใช้ SDK ของ Performance Monitoring เป็นการเรียกใช้ "ไม่มีการดำเนินการ" (NOOP) ซึ่งจะช่วยลดผลกระทบที่สำคัญต่อประสิทธิภาพของแอปจากการใช้ SDK ของ Performance Monitoring ในแอป
หากตั้งค่า perf_disable เป็น
false
Performance Monitoring จะยังคง เปิดใช้