使用 Firebase Crashlytics 記錄廣告回應 ID

選取平台: Android iOS Unity

Firebase Crashlytics 是一項輕巧的即時當機回報工具,可協助您輕鬆管理應用程式的穩定性問題。Crashlytics 會智慧地將當機情況分組,並醒目顯示導致當機的情況,節省您排解問題的時間。

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

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

  1. 如要嘗試從乾淨的應用程式使用 Firebase 記錄,您可以從 GitHub 下載或複製 Android 適用的 Google 行動廣告 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:24.5.0'
    
        // Add the Firebase Crashlytics dependency.
        implementation 'com.google.firebase:firebase-crashlytics:20.0.0'
    }

    project/build.gradle

    buildscript {
        repositories {
            // ...
            // Add Google's Maven repository.
            google()
        }
    
        dependencies {
            // ...
    
            classpath 'com.google.gms:google-services:4.4.3'
    
            // Add the Fabric Crashlytics plugin.
            classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.5'
        }
    }
    
    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 重新啟動應用程式,以便將當機記錄上傳至 Crashlytics。

步驟 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。請注意,部分金鑰最多可能需要一小時才會顯示在資訊主頁中。