با Firebase Crashlytics شروع کنید


این راهنمای سریع نحوه راه‌اندازی Firebase Crashlytics در برنامه شما با Firebase Crashlytics SDK را شرح می‌دهد تا بتوانید گزارش‌های جامعی از خرابی‌ها را در کنسول Firebase دریافت کنید.با Crashlytics برای اندروید، گزارش‌هایی برای خرابی‌ها، خطاهای غیرمهلک و خطاهای «برنامه پاسخ نمی‌دهد» (ANR) دریافت می‌کنید.

راه‌اندازی Crashlytics نیازمند انجام وظایفی هم در کنسول Firebase و هم در IDE شما است (مانند اضافه کردن فایل پیکربندی Firebase و Crashlytics SDK). برای تکمیل راه‌اندازی، باید یک کرش آزمایشی را مجبور کنید تا اولین گزارش کرش شما را به Firebase ارسال کند.

قبل از اینکه شروع کنی

  1. اگر هنوز Firebase را به پروژه اندروید خود اضافه نکرده‌اید، آن را اضافه کنید. اگر برنامه اندروید ندارید، می‌توانید یک برنامه نمونه را دانلود کنید.

  2. توصیه می‌شود : برای اینکه گزارش‌های breadcrumb به طور خودکار اقدامات کاربر را که منجر به خرابی، عدم موفقیت یا رویداد ANR می‌شود، درک کنند، باید Google Analytics در پروژه Firebase خود فعال کنید.

    • اگر پروژه Firebase فعلی شما Google Analytics فعال نکرده است، می‌توانید Google Analytics از تب Integrations فعال کنید. > تنظیمات پروژه در کنسول Firebase .

    • اگر در حال ایجاد یک پروژه جدید Firebase هستید، در طول فرآیند ایجاد پروژه، Google Analytics فعال کنید.

  3. مطمئن شوید که برنامه شما حداقل نسخه‌های مورد نیاز زیر را دارد:

    • گریدل ۸.۰
    • افزونه اندروید گریدل ۸.۱.۰
    • افزونه Gradle سرویس‌های گوگل نسخه ۴.۴.۱

مرحله ۱ : Crashlytics SDK را به برنامه خود اضافه کنید

در فایل Gradle ماژول (سطح برنامه) خود (معمولاً <project>/<app-module>/build.gradle.kts یا <project>/<app-module>/build.gradle )، وابستگی مربوط به کتابخانه Crashlytics NDK برای اندروید را اضافه کنید. توصیه می‌کنیم برای کنترل نسخه‌بندی کتابخانه Firebase Android BoM استفاده کنید.

برای استفاده از گزارش‌های breadcrumb ، کیت توسعه نرم‌افزار Firebase برای Google Analytics را نیز به برنامه خود اضافه کنید. مطمئن شوید که گوگل آنالیتیکس در پروژه فایربیس شما فعال است .

dependencies {
    // Import the BoM for the Firebase platform
    implementation(platform("com.google.firebase:firebase-bom:34.4.0"))

    // Add the dependencies for the Crashlytics NDK and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-crashlytics-ndk")
    implementation("com.google.firebase:firebase-analytics")
}

با استفاده از Firebase Android BoM ، برنامه شما همیشه از نسخه‌های سازگار کتابخانه‌های اندروید Firebase استفاده خواهد کرد.

(جایگزین) اضافه کردن وابستگی‌های کتابخانه Firebase بدون استفاده از BoM

اگر تصمیم به استفاده از Firebase BoM ندارید، باید هر نسخه از کتابخانه Firebase را در خط وابستگی آن مشخص کنید.

توجه داشته باشید که اگر از چندین کتابخانه Firebase در برنامه خود استفاده می‌کنید، اکیداً توصیه می‌کنیم از BoM برای مدیریت نسخه‌های کتابخانه استفاده کنید، که تضمین می‌کند همه نسخه‌ها سازگار هستند.

dependencies {
    // Add the dependencies for the Crashlytics NDK and Analytics libraries
    // When NOT using the BoM, you must specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-crashlytics-ndk:20.0.3")
    implementation("com.google.firebase:firebase-analytics:23.0.0")
}

مرحله 2 : افزونه Crashlytics Gradle را به برنامه خود اضافه کنید

  1. در فایل Gradle سطح ریشه (سطح پروژه) خود ( <project>/build.gradle.kts یا <project>/build.gradle )، افزونه Crashlytics Gradle را به بلوک plugins اضافه کنید:

    Kotlin

    plugins {
        // Make sure that you have the AGP plugin 8.1+ dependency
        id("com.android.application") version "8.1.4" apply false
        // ...
    
        // Make sure that you have the Google services Gradle plugin 4.4.1+ dependency
        id("com.google.gms.google-services") version "4.4.4" apply false
    
        // Add the dependency for the Crashlytics Gradle plugin
        id("com.google.firebase.crashlytics") version "3.0.6" apply false
    }

    Groovy

    plugins {
        // Make sure that you have the AGP plugin 8.1+ dependency
        id 'com.android.application' version '8.1.4' apply false
        // ...
    
        // Make sure that you have the Google services Gradle plugin 4.4.1+ dependency
        id 'com.google.gms.google-services' version '4.4.4' apply false
    
        // Add the dependency for the Crashlytics Gradle plugin
        id 'com.google.firebase.crashlytics' version '3.0.6' apply false
    }
  2. در فایل Gradle ماژول (سطح برنامه) خود (معمولاً <project>/<app-module>/build.gradle.kts یا <project>/<app-module>/build.gradle )، افزونه Crashlytics Gradle را اضافه کنید:

    Kotlin

    plugins {
      id("com.android.application")
      // ...
    
      // Make sure that you have the Google services Gradle plugin
      id("com.google.gms.google-services")
    
      // Add the Crashlytics Gradle plugin
      id("com.google.firebase.crashlytics")
    }

    Groovy

    plugins {
      id 'com.android.application'
      // ...
    
      // Make sure that you have the Google services Gradle plugin
      id 'com.google.gms.google-services'
    
      // Add the Crashlytics Gradle plugin
      id 'com.google.firebase.crashlytics'
    }

Step 3 : Force a test crash to finish setup

To finish setting up Crashlytics and see initial data in the Crashlytics dashboard of the Firebase console, you need to force a test crash.

  1. Add code to your app that you can use to force a test crash.

    You can use the following code in your app's MainActivity to add a button to your app that, when pressed, causes a crash. The button is labeled "Test Crash".

    Kotlin

    val crashButton = Button(this)
    crashButton.text = "Test Crash"
    crashButton.setOnClickListener {
       throw RuntimeException("Test Crash") // Force a crash
    }
    
    addContentView(crashButton, ViewGroup.LayoutParams(
           ViewGroup.LayoutParams.MATCH_PARENT,
           ViewGroup.LayoutParams.WRAP_CONTENT))

    Java

    Button crashButton = new Button(this);
    crashButton.setText("Test 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));
  2. Build and run your app.

  3. Force the test crash in order to send your app's first crash report:

    1. Open your app from your test device or emulator.

    2. In your app, press the "Test Crash" button that you added using the code above.

    3. After your app crashes, restart it so that your app can send the crash report to Firebase.

  4. Go to the Crashlytics dashboard of the Firebase console to see your test crash.

    If you've refreshed the console and you're still not seeing the test crash after five minutes, enable debug logging to see if your app is sending crash reports.


And that's it! Crashlytics is now monitoring your app for crashes, non-fatal errors, and ANRs. Visit the Crashlytics dashboard to view and investigate all your reports and statistics.

مراحل بعدی

  • Integrate with Google Play so that you can filter your Android app's crash reports by Google Play track directly in the Crashlytics dashboard. This allows you to better focus your dashboard on specific builds.
  • In Android Studio, view and filter Crashlytics data.
    • Use the App Quality Insights (AQI) window in Android Studio to view Crashlytics data alongside your code — no need to jump back and forth between the Crashlytics dashboard and the IDE to start debugging top issues.
    • Learn how to use the AQI window in the Android Studio documentation.
    • We'd love to hear from you! Send us feedback about the AQI window by filing a bug report .