Để thảo luận và đưa ra ý kiến phản hồi về các sản phẩm của chúng tôi, hãy tham gia kênh Discord chính thức của AdMob trong máy chủ Cộng đồng quảng cáo và đo lường của Google.
Firebase
Crashlytics
là một trình báo cáo sự cố theo thời gian thực và gọn nhẹ, giúp bạn dễ dàng quản lý các vấn đề về độ ổn định trong ứng dụng của mình. Crashlytics giúp bạn tiết kiệm thời gian khắc phục sự cố bằng cách nhóm các sự cố và làm nổi bật những tình huống dẫn đến các sự cố đó theo cách thông minh.
Hướng dẫn này mô tả cách tích hợp Crashlytics vào dự án Android Studio
để bạn có thể ghi nhật ký mã phản hồi quảng cáo. Sau này, khi khắc phục sự cố trong ứng dụng, bạn có thể tra cứu mã phản hồi quảng cáo này và sử dụng Trung tâm xem xét quảng cáo trong
AdMob
để tìm và chặn quảng cáo.
Bước 1: Thêm Firebase vào một ứng dụng Android
Nếu muốn thử ghi nhật ký bằng Firebase từ một ứng dụng không có sự cố, bạn có thể tải xuống hoặc sao chép các ví dụ về SDK quảng cáo trên thiết bị di động của Google cho kho lưu trữ Android trên GitHub. Cụ thể, hướng dẫn này sử dụng Ví dụ về quảng cáo biểu ngữ.
Nếu đã có một ứng dụng, bạn có thể tiến hành các bước khác
bằng tên gói của ứng dụng đó. Bạn cũng có thể áp dụng các bước tương tự cho các ví dụ khác trong kho lưu trữ bằng các điều chỉnh nhỏ.
Để sử dụng Firebase Crashlytics, bạn phải tạo một dự án Firebase và thêm ứng dụng của bạn vào dự án đó. Nếu chưa có, bạn cần tạo một dự án Firebase. Hãy nhớ đăng ký ứng dụng của bạn cho dự án đó.
Trên trang Crashlytics trong bảng điều khiển của Firebase, hãy nhấp vào Thiết lập
Crashlytics.
Trên màn hình xuất hiện, hãy nhấp vào Không > Thiết lập ứng dụng
Firebase mới.
Trong tệp build.gradle của bạn, hãy thêm các phần phụ thuộc cho Google Analytics, Fabric và Crashlytics.
app/build.gradle
applyplugin:'com.android.application'applyplugin:'com.google.gms.google-services'// Add the Fabric pluginapplyplugin:'io.fabric'dependencies{// ...// Add the Google Mobile Ads SDKimplementation'com.google.android.gms:play-services-ads:24.5.0'// Add the Firebase Crashlytics dependency.implementation'com.google.firebase:firebase-crashlytics:20.0.1'}
Tạo và chạy ứng dụng của bạn để đảm bảo Crashlytics được thiết lập
chính xác. Sau khi thực hiện thành công, bạn sẽ có thể truy cập vào trang tổng quan Crashlytics.
(Không bắt buộc): Kiểm tra thông tin thiết lập của bạn
Bằng cách Thêm nút
sự cố, bạn có thể xác định sự cố gây ra một sự cố ứng dụng sau mỗi lần nhấn nút.
Sau đây là ví dụ cho thấy cách thêm một nút sự cố trong phương thức onCreate() của một Activity:
Trong Android Studio, hãy tạo và chạy ứng dụng của bạn trên một trình mô phỏng hoặc thiết bị đã kết nối.
Sau khi tải ứng dụng, bạn có thể nhấp vào nút Sự cố. Chạy lại ứng dụng trên thiết bị hoặc Android Studio để tải nhật ký sự cố lên Crashlyics.
Bước 2: Ghi nhật ký mã phản hồi quảng cáo
Nếu tải nhiều quảng cáo và hiển thị các quảng cáo đó vào các thời điểm khác nhau, bạn nên ghi nhật ký từng mã phản hồi quảng cáo bằng một khoá riêng. Ví dụ: hướng dẫn này sử dụng một ví dụ chỉ có một quảng cáo biểu ngữ. Do đó, chúng tôi ghi nhật ký mã phản hồi quảng cáo làm khoá banner_ad_response_id trong đoạn mã sau. Bạn thực sự có thể tạo nhiều cặp giá trị / khoá tuỳ chỉnh trong Firebase Crashlytics cho nhiều loại quảng cáo và sự kiện quảng cáo (xem AdListener trong thời gian hoạt động của quảng cáo). Hãy xem bài viết Tuỳ chỉnh báo cáo sự cố trong Firebase Crashlytics để biết thêm thông tin về cách ghi nhật ký tuỳ chỉnh.
Thêm mã sau vào MyActivity.java của bạn. Về cơ bản, mã này sử dụng hàm FirebaseCrashlytics.setCustomKey() trong hàm callback onAdLoaded() để đảm bảo rằng bạn đã tải quảng cáo trước khi tìm cách gọi getResponseInfo().
Xin chúc mừng! Bây giờ, bạn sẽ thấy banner_ad_response_id
gần đây nhất trong mục khoá của các phiên hoạt động có sự cố trên trang tổng quan Crashlytics. Xin lưu ý rằng
có thể mất tới một giờ thì một số khoá mới xuất hiện trên trang tổng quan của bạn.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eFirebase Crashlytics is integrated into your Android app to help manage and troubleshoot app stability issues.\u003c/p\u003e\n"],["\u003cp\u003eBy logging ad response IDs with Crashlytics, you can identify and block problematic ads using AdMob's Ad Review Center.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides step-by-step instructions to set up Crashlytics, log ad response IDs, and optionally test the implementation by forcing a crash.\u003c/p\u003e\n"],["\u003cp\u003eCustom keys, such as the ad response ID, might take up to an hour to appear in your Firebase Crashlytics dashboard.\u003c/p\u003e\n"]]],[],null,["# Logging Ad Response ID with Firebase Crashlytics\n\nSelect platform: [Android](/admob/android/crashlytics \"View this page for the Android platform docs.\") [iOS](/admob/ios/crashlytics \"View this page for the iOS platform docs.\") [Unity](/admob/unity/crashlytics \"View this page for the Unity platform docs.\")\n\n\u003cbr /\u003e\n\n[Firebase\nCrashlytics](//firebase.google.com/docs/crashlytics/get-started-new-sdk?platform=android)\nis a lightweight, realtime crash reporter that makes it easy for you to manage\nstability issues in your app. Crashlytics saves you troubleshooting time by\nintelligently grouping crashes and highlighting the circumstances that lead up\nto them.\n\nThis guide describes how to integrate Crashlytics into your Android Studio\nproject so that you can log ad response IDs. Later, when you troubleshoot\ncrashes in your app, you can look up the ad response ID and use the Ad Review\nCenter in\n\n[AdMob](//support.google.com/admob/answer/3500252)\n\nto find and block the ads.\n\n**Step 1:** Add Firebase to an Android application\n--------------------------------------------------\n\n1. If you would like to try logging with Firebase from a clean app, you can\n download or clone the Google Mobile Ads SDK examples for Android\n [repository](//github.com/googleads/googleads-mobile-android-examples) on\n GitHub. This guide specifically uses the\n\n [Banner Example](//github.com/googleads/googleads-mobile-android-examples/tree/main/java/admob/BannerExample).\n\n\n If you already have an app, you should be able to proceed to other steps\n with your app's package name. The same steps can also be applied to other\n examples in the repository with minor adaptations.\n2. In order to use Firebase Crashlytics, you must create a Firebase project and\n add your app to it. If you haven't already, create a Firebase project. Make\n sure to [register your\n app](//firebase.google.com/docs/android/setup#register-app) to it.\n\n 1. In the Crashlytics page of the Firebase console, click **Set up\n Crashlytics**.\n\n 2. In the screen that appears, click **No** \\\u003e **Set up a new Firebase\n app**.\n\n3. In your build.gradle, add the dependencies for Google Analytics, Fabric, and\n Crashlytics.\n\n #### app/build.gradle\n\n ```carbon\n apply plugin: 'com.android.application'\n apply plugin: 'com.google.gms.google-services'\n\n // Add the Fabric plugin\n apply plugin: 'io.fabric'\n\n dependencies {\n // ...\n\n // Add the Google Mobile Ads SDK\n implementation 'com.google.android.gms:play-services-ads:24.5.0'\n\n // Add the Firebase Crashlytics dependency.\n implementation 'com.google.firebase:firebase-crashlytics:20.0.0'\n }\n ```\n\n #### project/build.gradle\n\n ```text\n buildscript {\n repositories {\n // ...\n // Add Google's Maven repository.\n google()\n }\n\n dependencies {\n // ...\n\n classpath 'com.google.gms:google-services:4.4.3'\n\n // Add the Fabric Crashlytics plugin.\n classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.6'\n }\n }\n\n allprojects {\n // ...\n repositories {\n // Check that Google's Maven repository is included (if not, add it).\n google()\n\n // ...\n }\n }\n ```\n4. Build and run your app to make sure to make sure Crashlytics is configured\n correctly. Once successful, you will be able to access the Crashlytics\n dashboard.\n\n### **(Optional): Test your Setup**\n\nBy [Adding a crash\nbutton](//firebase.google.com/docs/crashlytics/test-implementation?platform=android)\nyou can force a crash for causing an app crash with each button press.\n\nHere's an example that shows how to add a crash button in the `onCreate()` method of an\n`Activity`:\n\n#### MainActivity (excerpt)\n\n### Java\n\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_my);\n\n // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in\n // values/strings.xml.\n adView = findViewById(R.id.ad_view);\n\n // Start loading the ad in the background.\n adView.loadAd(new AdRequest.Builder().build());\n\n // Add a crash button.\n Button crashButton = new Button(this);\n crashButton.setText(\"Crash!\");\n crashButton.setOnClickListener(new View.OnClickListener() {\n public void onClick(View view) {\n throw new RuntimeException(\"Test Crash\"); // Force a crash\n }\n });\n\n addContentView(crashButton, new ViewGroup.LayoutParams(\n ViewGroup.LayoutParams.MATCH_PARENT,\n ViewGroup.LayoutParams.WRAP_CONTENT));\n}\n```\n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_my)\n\n // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in\n // values/strings.xml.\n adView = findViewById(R.id.ad_view)\n\n // Start loading the ad in the background.\n adView.loadAd(AdRequest.Builder().build())\n\n // Add a crash button.\n val crashButton = Button(this)\n crashButton.text = \"Crash!\"\n crashButton.setOnClickListener {\n throw RuntimeException(\"Test Crash\") // Force a crash\n }\n\n addContentView(crashButton, ViewGroup.LayoutParams(\n ViewGroup.LayoutParams.MATCH_PARENT,\n ViewGroup.LayoutParams.WRAP_CONTENT))\n}\n```\n\nIn Android Studio, build and run your app on an emulator or a connected device.\nAfter the app is loaded, you can click the **Crash** button. Relaunch the app\nfrom the device or Android Studio for the crash log to be uploaded to\nCrashlyics.\n| **Key Point:** While the crash and session data might be visible instantly in the Firebase console, custom keys may take an hour or more to propagate.\n\n**Step 2:** Log the ad response ID\n----------------------------------\n\nIf you load multiple ads and show them at different times, it is a good idea to\nlog each ad response Id with a separate key. For instance, this guide uses an\nexample that has only one banner ad. Hence, we log the ad response ID as the\n`banner_ad_response_id` key in the following snippet. Indeed you can create\nmultiple custom key / value pairs in Firebase Crashlytics for different ad types\nand ad events (see\n[`AdListener`](/admob/android/reference/com/google/android/gms/ads/AdListener) for\nad's life cycle). Visit [Customize your Firebase Crashlytics crash\nreports](//firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android)\nfor more information on custom logging.\n\nAdd the following code to your `MyActivity.java`. Essentially, it uses the\n[`FirebaseCrashlytics.setCustomKey()`](//firebase.google.com/docs/reference/android/com/google/firebase/crashlytics/FirebaseCrashlytics#public-void-setcustomkey-string-key,-long-value)\nfunction in the `onAdLoaded()` callback function to ensure that the ad has\nloaded before attempting to call `getResponseInfo()`. \n\n### Java\n\n```java\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_my);\n\n // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in\n // values/strings.xml.\n adView = findViewById(R.id.ad_view);\n\n adView.setAdListener(new AdListener() {\n @Override\n public void onAdLoaded() {\n String adResponseId = adView.getResponseInfo().getResponseId();\n FirebaseCrashlytics.getInstance().setCustomKey(\n \"banner_ad_response_id\", adResponseId);\n }\n });\n\n // Start loading the ad in the background.\n adView.loadAd(new AdRequest.Builder().build());\n\n // Add a crash button.\n Button crashButton = new Button(this);\n crashButton.setText(\"Crash!\");\n crashButton.setOnClickListener(new View.OnClickListener() {\n public void onClick(View view) {\n throw new RuntimeException(\"Test Crash\"); // Force a crash\n }\n });\n\n addContentView(crashButton, new ViewGroup.LayoutParams(\n ViewGroup.LayoutParams.MATCH_PARENT,\n ViewGroup.LayoutParams.WRAP_CONTENT));\n}\n```\n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_my)\n\n // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in\n // values/strings.xml.\n adView = findViewById(R.id.ad_view)\n\n adView.adListener = object : AdListener() {\n override fun onAdLoaded() {\n mAdView.responseInfo?.responseId?.let { adResponseId -\u003e\n FirebaseCrashlytics.getInstance().setCustomKey(\n \"banner_ad_response_id\", adResponseId)\n }\n }\n }\n\n // Start loading the ad in the background.\n adView.loadAd(AdRequest.Builder().build())\n\n // Add a crash button.\n val crashButton = Button(this)\n crashButton.text = \"Crash!\"\n crashButton.setOnClickListener {\n throw RuntimeException(\"Test Crash\") // Force a crash\n }\n\n addContentView(crashButton, ViewGroup.LayoutParams(\n ViewGroup.LayoutParams.MATCH_PARENT,\n ViewGroup.LayoutParams.WRAP_CONTENT))\n}\n```\n\nCongratulations! You will now see the most recent `banner_ad_response_id`\nin the key section of crash sessions on your Crashlytics dashboard. Note that\nsome keys may take up to an hour to become visible in your dashboard."]]