使用 Firebase Crashlytics 記錄廣告回應 ID

Firebase Crashlytics 是輕巧的即時當機回報程式,可讓您輕鬆管理應用程式中的穩定性問題。Crashlytics 會將智慧當機情形分組,並醒目顯示導致問題發生的原因,因此能為您節省時間。

本指南說明如何將 Crashlytics 整合至 Android Studio 專案,以便記錄廣告回應 ID。之後解決應用程式的當機問題時,您可以查詢廣告回應 ID,然後使用 Ad Manager 找出並封鎖廣告。

步驟 1:將 Firebase 新增至 Android 應用程式

  1. 如果您想透過簡潔的應用程式使用 Firebase 記錄,您可以到 GitHub 下載或複製 Android 的 Google Mobile Ads SDK 範例存放區。本指南專門使用 橫幅示例

    如果已有應用程式,您應該能使用應用程式的套件名稱繼續完成其他步驟。同樣的步驟也可以套用到存放區中其他範例,但經過微幅調整。

  2. 如要使用 Firebase Crashlytics,你必須建立 Firebase 專案並將應用程式新增至其中。如果您尚未建立 Firebase 專案,請先建立。請務必註冊您的應用程式

    1. 在 Firebase 控制台的 Crashlytics 頁面中,按一下「設定 Crashlytics」

    2. 在隨即顯示的畫面中,依序按一下「否」 >「設定新的 Firebase 應用程式」

  3. 在 build.gradle 中,新增 Google Analytics (分析)、Fabric 和 Crashlytics 的依附元件。

    app/build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    
    // Add the Fabric plugin
    apply plugin: 'io.fabric'
    
    dependencies {
        // ...
    
        // Add the Google Mobile Ads SDK
        implementation 'com.google.android.gms:play-services-ads:22.4.0'
    
        // Add the Firebase Crashlytics dependency.
        implementation 'com.google.firebase:firebase-crashlytics:18.4.1'
    }
    

    專案/build.gradle

    buildscript {
        repositories {
            // ...
            // Add Google's Maven repository.
            google()
        }
    
        dependencies {
            // ...
    
            classpath 'com.google.gms:google-services:4.3.15'
    
            // Add the Fabric Crashlytics plugin.
            classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9'
        }
    }
    
    allprojects {
        // ...
        repositories {
           // Check that Google's Maven repository is included (if not, add it).
           google()
    
           // ...
        }
    }
    
  4. 建構並執行應用程式,確保 Crashlytics 設定正確無誤。成功完成之後,您將可存取 Crashlytics 資訊主頁。

(選用):測試設定

您可以透過新增當機按鈕,在每次按下按鈕時強制導致應用程式當機。

以下範例說明如何在 ActivityonCreate() 方法中新增當機按鈕:

MainActivity (摘錄)

Java

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view);

  // Start loading the ad in the background.
  adView.loadAd(new AdManagerAdRequest.Builder().build());

  // Add a crash button.
  Button crashButton = new Button(this);
  crashButton.setText("Crash!");
  crashButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      throw new RuntimeException("Test Crash"); // Force a crash
    }
  });

  addContentView(crashButton, new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT));
}

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_my)

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view)

  // Start loading the ad in the background.
  adView.loadAd(AdManagerAdRequest.Builder().build())

  // Add a crash button.
  val crashButton = Button(this)
  crashButton.text = "Crash!"
  crashButton.setOnClickListener {
    throw RuntimeException("Test Crash") // Force a crash
  }

  addContentView(crashButton, ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT))
}

在 Android Studio 中,在模擬器或已連結的裝置上建構及執行應用程式。應用程式載入完畢後,請按一下「當機」按鈕。透過裝置或 Android Studio 重新啟動應用程式,即可將當機記錄上傳至 Crashlyics。

步驟 2:記錄廣告回應 ID

如果您載入了多個廣告,並於不同時間顯示這些廣告,建議您使用個別金鑰記錄每個廣告回應 ID。舉例來說,本指南中使用的範例只有一個橫幅廣告。因此,下列程式碼片段會將廣告回應 ID 記錄為 banner_ad_response_id 鍵。確實需要在 Firebase Crashlytics 中為不同的廣告類型和廣告事件建立多個自訂鍵 / 值組合 (詳情請參閱廣告生命週期)。AdListener如要進一步瞭解自訂記錄功能,請參閱「自訂 Firebase Crashlytics 當機報告」一文。

將下列程式碼新增到 MyActivity.java。基本上,它會使用 onAdLoaded() 回呼函式中的 FirebaseCrashlytics.setCustomKey() 函式,確保在嘗試呼叫 getResponseInfo() 之前已載入廣告。

Java

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view);

  adView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
      String adResponseId = adView.getResponseInfo().getResponseId();
      FirebaseCrashlytics.getInstance().setCustomKey(
          "banner_ad_response_id", adResponseId);
    }
  });

  // Start loading the ad in the background.
  adView.loadAd(new AdManagerAdRequest.Builder().build());

  // Add a crash button.
  Button crashButton = new Button(this);
  crashButton.setText("Crash!");
  crashButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      throw new RuntimeException("Test Crash"); // Force a crash
    }
  });

  addContentView(crashButton, new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT));
}

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_my)

  // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
  // values/strings.xml.
  adView = findViewById(R.id.ad_view)

  adView.adListener = object : AdListener() {
    override fun onAdLoaded() {
      mAdView.responseInfo?.responseId?.let { adResponseId ->
          FirebaseCrashlytics.getInstance().setCustomKey(
              "banner_ad_response_id", adResponseId)
      }
    }
  }

  // Start loading the ad in the background.
  adView.loadAd(AdManagerAdRequest.Builder().build())

  // Add a crash button.
  val crashButton = Button(this)
  crashButton.text = "Crash!"
  crashButton.setOnClickListener {
    throw RuntimeException("Test Crash") // Force a crash
  }

  addContentView(crashButton, ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT))
}

恭喜!現在,您會在 Crashlytics 資訊主頁的當機工作階段的主要部分中看到最新的 banner_ad_response_id。請注意,部分金鑰最多可能需要一小時才會出現在資訊主頁上。