ปิดใช้การตรวจสอบประสิทธิภาพของ Firebase

ในระหว่างการพัฒนาและทดสอบแอป คุณอาจพบว่าการปิดใช้ Performance Monitoring

ตัวอย่างเช่น โดย การปิดใช้ Performance Monitoring ระหว่างขั้นตอนการสร้างแอป คุณสามารถ

  • ปิดใช้งานฟังก์ชันบางอย่างของ Performance Monitoring (เช่น ฟังก์ชันที่กำหนดให้โดย ปลั๊กอิน Gradle Performance Monitoring รายการ) ในบิลด์การแก้ไขข้อบกพร่อง แต่เปิดใช้ฟังก์ชันอีกครั้งสําหรับ บิลด์ที่เผยแพร่

  • ปิดใช้ Performance Monitoring ขณะสร้างแอป แต่อนุญาตให้แอปเปิดใช้อีกครั้ง ขณะรันไทม์

  • ปิดใช้ Performance Monitoring เมื่อสร้างแอปและไม่อนุญาตให้แอป เปิดใช้อีกครั้งที่รันไทม์

คุณยังสร้างแอปโดยเปิดใช้ Performance Monitoring ได้ด้วย แต่ ใช้ Firebase Remote Config เพื่อช่วยให้คุณได้รับ ความยืดหยุ่นในการปิดใช้ (และเปิดใช้อีกครั้ง) Performance Monitoring ในแอปเวอร์ชันที่ใช้งานจริง ด้วย ตัวเลือกนี้คุณยังสามารถกำหนดค่าแอปเพื่อให้ผู้ใช้เลือกใช้หรือเลือกไม่ใช้ ด้วย Performance Monitoring

ปิดใช้ Performance Monitoring ในระหว่างกระบวนการสร้างแอป

คุณสามารถปิดใช้ Performance Monitoring ในระหว่างกระบวนการสร้างได้โดย ปิดใช้ปลั๊กอิน Performance Monitoring Gradle และ/หรือโดย การปิดใช้คลัง Android ของ Performance Monitoring

ในระหว่างการพัฒนาและการแก้ไขข้อบกพร่อง การปิดใช้ปลั๊กอินนั้นมีประโยชน์เนื่องจาก การใช้เครื่องมือของปลั๊กอิน อาจมีส่วนทำให้เวลาบิลด์เพิ่มขึ้น อย่างไรก็ตาม คุณอาจพิจารณารักษา ไลบรารีที่เปิดใช้ไว้เพื่อให้คุณยังคงดูข้อมูลประสิทธิภาพจาก การเริ่มแอป การใช้แอปในเบื้องหน้า และการติดตามในแอปในเบื้องหลัง รวมถึงส่วนใดก็ได้ การติดตามโค้ดที่กำหนดเอง ในแอปของคุณ

ปิดใช้ปลั๊กอิน Performance Monitoring Gradle

คุณสามารถปิดใช้ปลั๊กอิน Performance Monitoring ได้โดยการเพิ่ม instrumentationEnabled แจ้งโดยใช้ตัวเลือกต่อไปนี้

ปิดใช้ปลั๊กอินผ่านแฟล็กพร็อพเพอร์ตี้ส่วนขยาย

การใช้แฟล็กพร็อพเพอร์ตี้ส่วนขยายจะช่วยให้คุณสามารถปิดใช้ปลั๊กอิน Performance Monitoring สำหรับตัวแปรบิลด์ที่เจาะจงในเวลาคอมไพล์

  1. ในไฟล์ Gradle ระดับราก (ระดับโปรเจ็กต์) (<project>/build.gradle.kts หรือ <project>/build.gradle) โปรดตรวจสอบว่า ทรัพยากร Dependency ของปลั๊กอิน Android Gradle ระบุเป็น v3.4.0 ขึ้นไป

    สำหรับปลั๊กอิน Android Gradle เวอร์ชันก่อนหน้า คุณยังคงปิดใช้ได้ ปลั๊กอิน Performance Monitoring สำหรับตัวแปรบิลด์ที่เจาะจง แต่เวลาบิลด์ การสนับสนุนสำหรับตัวแปรนั้นจะไม่ถูกกำจัดออกทั้งหมด

  2. เพิ่ม Flag ต่อไปนี้ในไฟล์ 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
          }
        }
      }
    }
    

ปิดใช้ปลั๊กอินผ่านแฟล็กพร็อพเพอร์ตี้ของโปรเจ็กต์

การใช้แฟล็กพร็อพเพอร์ตี้ของโปรเจ็กต์ช่วยให้คุณปิดใช้ปลั๊กอิน Performance Monitoring สำหรับ ตัวแปรของบิลด์ all ในเวลาคอมไพล์

เพิ่มแฟล็กต่อไปนี้ลงในไฟล์ 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

  1. ตรวจสอบว่า Remote Config อยู่ในส่วน dependencies ของ ไฟล์ Gradle แบบ module (ระดับแอป) (โดยปกติแล้ว <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle):

    Kotlin+KTX

      implementation("com.google.firebase:firebase-config-ktx:22.0.0")
    

    Java

      implementation("com.google.firebase:firebase-config:22.0.0")
    
  2. ตั้งค่า Remote Config และปิดใช้ Performance Monitoring หาก perf_disable คือ ตั้งค่าเป็น true:

    Kotlin+KTX

    // 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
                    }
                }
            });
  3. เพิ่มโค้ดต่อไปนี้ลงใน MainActivity.java เพื่อดึงข้อมูลและเปิดใช้งาน ค่า Remote Config:

    Kotlin+KTX

    // 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
                    }
                }
            });
  4. หากต้องการปิดใช้ Performance Monitoring ในคอนโซล Firebase ให้สร้าง perf_disable ในโปรเจ็กต์ของแอป แล้วกำหนดค่าเป็น true

    การเปลี่ยนแปลงนี้จะเรียกใช้ SDK Performance Monitoring ว่า "ไม่มีการดำเนินการ" การโทร (NOOP) ซึ่งขจัดผลกระทบอย่างมากต่อประสิทธิภาพของแอปจากการใช้ SDK Performance Monitoring ในแอปของคุณ

    หากคุณตั้งค่า perf_disable เป็น false ค่า Performance Monitoring จะยังคงอยู่ เปิดอยู่