Catch up on highlights from Firebase at Google I/O 2023. Learn more

เพิ่ม Firebase ให้กับโปรเจ็กต์ Android ของคุณ

ข้อกำหนดเบื้องต้น

  • ติดตั้งหรืออัปเดต Android Studio เป็นเวอร์ชันล่าสุด

  • ตรวจสอบให้แน่ใจว่าโครงการของคุณตรงตามข้อกำหนดเหล่านี้:

    • กำหนดเป้าหมาย API ระดับ 19 (KitKat) หรือสูงกว่า
    • ใช้ Android 4.4 หรือสูงกว่า
    • ใช้ Jetpack (AndroidX) ซึ่งรวมถึงการปฏิบัติตามข้อกำหนดเวอร์ชันเหล่านี้:
      • com.android.tools.build:gradle v3.2.1 หรือใหม่กว่า
      • compileSdkVersion 28 หรือใหม่กว่า
  • ตั้งค่าอุปกรณ์จริงหรือใช้โปรแกรม จำลอง เพื่อเรียกใช้แอปของคุณ
    โปรดทราบว่า Firebase SDK ที่ต้องพึ่งพาบริการ Google Play กำหนดให้อุปกรณ์หรือโปรแกรมจำลองต้องติดตั้งบริการ Google Play

  • ลงชื่อเข้าใช้ Firebase โดยใช้บัญชี Google ของคุณ

หากคุณยังไม่มีโปรเจ็กต์ Android และเพียงต้องการลองใช้ผลิตภัณฑ์ Firebase คุณสามารถดาวน์โหลดหนึ่งใน ตัวอย่างการเริ่มต้นอย่างรวดเร็ว ของเรา


คุณเชื่อมต่อแอป Android กับ Firebase ได้โดยใช้ตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้



ตัวเลือกที่ 1 : เพิ่ม Firebase โดยใช้คอนโซล Firebase

การเพิ่ม Firebase ลงในแอปเกี่ยวข้องกับงานทั้งใน คอนโซล Firebase และในโครงการ Android ที่เปิดอยู่ (เช่น คุณดาวน์โหลดไฟล์กำหนดค่า Firebase จากคอนโซล จากนั้นย้ายไฟล์เหล่านั้นไปยังโครงการ Android)

ขั้นตอนที่ 1 : สร้างโครงการ Firebase

ก่อนที่คุณจะเพิ่ม Firebase ลงในแอป Android ได้ คุณต้องสร้างโครงการ Firebase เพื่อเชื่อมต่อกับแอป Android ของคุณ ไปที่ ทำความเข้าใจโครงการ Firebase เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโครงการ Firebase

ขั้นตอนที่ 2 : ลงทะเบียนแอปของคุณกับ Firebase

หากต้องการใช้ Firebase ในแอป Android คุณต้องลงทะเบียนแอปกับโปรเจ็กต์ Firebase การลงทะเบียนแอปมักเรียกว่า "การเพิ่ม" แอปของคุณในโครงการ

  1. ไปที่ คอนโซล Firebase

  2. ที่กึ่งกลางของหน้าภาพรวมโครงการ ให้คลิกไอคอน Android ( ) หรือ เพิ่มแอป เพื่อเริ่มเวิร์กโฟลว์การตั้งค่า

  3. ป้อนชื่อแพ็กเกจของแอปในช่อง ชื่อแพ็กเกจ Android

  4. (ไม่บังคับ) ป้อนข้อมูลแอปอื่นๆ: ชื่อเล่นแอป และ ใบรับรองการลงนามการแก้ไขข้อบกพร่อง SHA-1

  5. คลิก ลงทะเบียนแอป

ขั้นตอนที่ 3 : เพิ่มไฟล์กำหนดค่า Firebase

  1. ดาวน์โหลดแล้วเพิ่มไฟล์กำหนดค่า Firebase Android ( google-services.json ) ลงในแอปของคุณ:

    1. คลิก ดาวน์โหลด google-services.json เพื่อรับไฟล์กำหนดค่า Firebase Android

    2. ย้ายไฟล์กำหนดค่าของคุณไปยังไดเรกทอรีรากของ โมดูล (ระดับแอป) ของแอป

  2. ในการทำให้ค่าในไฟล์กำหนด google-services.json เข้าถึงได้โดย Firebase SDK คุณต้องใช้ ปลั๊กอิน Gradle ของบริการ Google ( google-services )

    1. ในไฟล์ Gradle ระดับรูท (ระดับโครงการ) ( <project>/build.gradle ) ให้เพิ่มปลั๊กอินบริการของ Google เป็นการพึ่งพา buildscript:

      buildscript {
      
          repositories {
            // Make sure that you have the following two repositories
            google()  // Google's Maven repository
            mavenCentral()  // Maven Central repository
          }
      
          dependencies {
            ...
      
            // Add the dependency for the Google services Gradle plugin
            classpath 'com.google.gms:google-services:4.3.15'
          }
      }
      
      allprojects {
        ...
      
        repositories {
          // Make sure that you have the following two repositories
          google()  // Google's Maven repository
          mavenCentral()  // Maven Central repository
        }
      }
      
    2. ในไฟล์ Gradle ของ โมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle ) ให้เพิ่มปลั๊กอินบริการของ Google:

      plugins {
          id 'com.android.application'
      
          // Add the Google services Gradle plugin
          id 'com.google.gms.google-services'
          ...
      }
      

ขั้นตอนที่ 4 : เพิ่ม Firebase SDK ในแอปของคุณ

  1. ใน ไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle ) ให้เพิ่มการอ้างอิงสำหรับ ผลิตภัณฑ์ Firebase ที่คุณต้องการใช้ในแอป ขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารี

    เปิดใช้งานการวิเคราะห์แล้ว

    Kotlin+KTX

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation platform('com.google.firebase:firebase-bom:32.1.0')
    
      // When using the BoM, you don't specify versions in Firebase library dependencies
    
      // Add the dependency for the Firebase SDK for Google Analytics
      implementation 'com.google.firebase:firebase-analytics-ktx'
    
      // TODO: Add the dependencies for any other Firebase products you want to use
      // See https://firebase.google.com/docs/android/setup#available-libraries
      // For example, add the dependencies for Firebase Authentication and Cloud Firestore
      implementation 'com.google.firebase:firebase-auth-ktx'
      implementation 'com.google.firebase:firebase-firestore-ktx'
    }
    

    Java

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation platform('com.google.firebase:firebase-bom:32.1.0')
    
      // When using the BoM, you don't specify versions in Firebase library dependencies
    
      // Add the dependency for the Firebase SDK for Google Analytics
      implementation 'com.google.firebase:firebase-analytics'
    
      // TODO: Add the dependencies for any other Firebase products you want to use
      // See https://firebase.google.com/docs/android/setup#available-libraries
      // For example, add the dependencies for Firebase Authentication and Cloud Firestore
      implementation 'com.google.firebase:firebase-auth'
      implementation 'com.google.firebase:firebase-firestore'
    }
    

    เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ

    ไม่ได้เปิดใช้การวิเคราะห์

    Kotlin+KTX

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation platform('com.google.firebase:firebase-bom:32.1.0')
    
      // When using the BoM, you don't specify versions in Firebase library dependencies
    
      // TODO: Add the dependencies for Firebase products you want to use
      // See https://firebase.google.com/docs/android/setup#available-libraries
      // For example, add the dependencies for Firebase Authentication and Cloud Firestore
      implementation 'com.google.firebase:firebase-auth-ktx'
      implementation 'com.google.firebase:firebase-firestore-ktx'
    }
    

    Java

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation platform('com.google.firebase:firebase-bom:32.1.0')
    
      // When using the BoM, you don't specify versions in Firebase library dependencies
    
      // TODO: Add the dependencies for Firebase products you want to use
      // See https://firebase.google.com/docs/android/setup#available-libraries
      // For example, add the dependencies for Firebase Authentication and Cloud Firestore
      implementation 'com.google.firebase:firebase-auth'
      implementation 'com.google.firebase:firebase-firestore'
    }
    

    เมื่อใช้ Firebase Android BoM แอปของคุณจะใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ

  2. หลังจากเพิ่มการอ้างอิงสำหรับผลิตภัณฑ์ที่คุณต้องการใช้แล้ว ให้ซิงค์โปรเจ็กต์ Android ของคุณกับไฟล์ Gradle

แค่นั้นแหละ! คุณสามารถข้ามไปเพื่อดู ขั้นตอนถัดไป ที่แนะนำ

หากคุณประสบปัญหาในการตั้งค่า โปรดไปที่ การแก้ไขปัญหาและคำถามที่พบบ่อยเกี่ยวกับ Android



ตัวเลือกที่ 2 : เพิ่ม Firebase โดยใช้ Firebase Assistant

Firebase Assistant ลงทะเบียนแอปของคุณกับโปรเจ็กต์ Firebase และเพิ่มไฟล์ Firebase ปลั๊กอิน และการอ้างอิงที่จำเป็นให้กับโปรเจ็กต์ Android ของคุณ — ทั้งหมดนี้ทำได้จากภายใน Android Studio!

  1. เปิดโปรเจ็กต์ Android ใน Android Studio จากนั้นตรวจสอบให้แน่ใจว่าคุณกำลังใช้ Android Studio เวอร์ชันล่าสุดและ Firebase Assistant:

    • Windows / Linux: วิธีใช้ > ตรวจสอบการอัปเดต
    • macOS: Android Studio > ตรวจสอบการอัปเดต
  2. เปิดตัวช่วย Firebase: เครื่องมือ > Firebase

  3. ในบานหน้าต่าง ผู้ช่วย เลือกผลิตภัณฑ์ Firebase เพื่อเพิ่มไปยังแอปของคุณ ขยายส่วน จากนั้นคลิกลิงก์บทช่วยสอน (เช่น Analytics > บันทึกเหตุการณ์ Analytics )

    1. คลิก เชื่อมต่อกับ Firebase เพื่อเชื่อมต่อโครงการ Android ของคุณกับ Firebase

    2. คลิกปุ่มเพื่อเพิ่มผลิตภัณฑ์ Firebase ที่ต้องการ (เช่น เพิ่ม Analytics ในแอปของคุณ )

  4. ซิงค์แอปของคุณเพื่อให้แน่ใจว่าการอ้างอิงทั้งหมดมีเวอร์ชันที่จำเป็น

  5. ในบานหน้าต่าง ผู้ช่วย ให้ ทำตามคำแนะนำการตั้งค่าที่เหลือสำหรับผลิตภัณฑ์ Firebase ที่คุณเลือก

  6. เพิ่มผลิตภัณฑ์ Firebase อื่นๆ ได้มากเท่าที่คุณต้องการผ่าน Firebase Assistant!

แค่นั้นแหละ! อย่าลืมตรวจสอบ ขั้นตอนถัดไป ที่แนะนำ

หากคุณประสบปัญหาในการตั้งค่า โปรดไปที่ การแก้ไขปัญหาและคำถามที่พบบ่อยเกี่ยวกับ Android



ห้องสมุดที่มีอยู่

ส่วนนี้แสดงรายการผลิตภัณฑ์ Firebase ที่รองรับสำหรับ Android และการขึ้นต่อกันของ Gradle เรียนรู้เพิ่มเติมเกี่ยวกับไลบรารี Firebase Android เหล่านี้:

โปรดทราบว่าเมื่อใช้ Firebase Android BoM คุณไม่ต้องระบุเวอร์ชันของไลบรารีแต่ละเวอร์ชันเมื่อคุณประกาศการอ้างอิงไลบรารีของ Firebase ใน build.gradle

Kotlin+KTX

บริการหรือสินค้า การพึ่งพา Gradle ล่าสุด
รุ่น
เพิ่มการวิเคราะห์?
Firebase Android BoM
(รายการวัสดุ)
com.google.firebase:firebase-bom

Firebase BoM เวอร์ชันล่าสุดประกอบด้วยไลบรารี Firebase Android แต่ละเวอร์ชันล่าสุด หากต้องการเรียนรู้ว่าไลบรารีเวอร์ชันใดที่แมปกับเวอร์ชัน BoM เฉพาะ ให้ตรวจสอบบันทึกย่อประจำรุ่นสำหรับเวอร์ชัน BoM นั้น

32.1.0
AdMob com.google.android.gms:play-services-ads 22.1.0
การวิเคราะห์ com.google.firebase:firebase-analytics-ktx 21.3.0
แอปตรวจสอบผู้ให้บริการที่กำหนดเอง com.google.firebase:firebase-appcheck-ktx 17.0.1
ผู้ให้บริการตรวจแก้จุดบกพร่องของแอปตรวจสอบ com.google.firebase:firebase-appcheck-debug 17.0.1
แอปตรวจสอบผู้ให้บริการความสมบูรณ์ของ Play com.google.firebase:firebase-appcheck-playintegrity 17.0.1
การกระจายแอพ com.google.firebase:firebase-appdistribution 16.0.0-เบต้า08
API การกระจายแอป com.google.firebase:firebase-appdistribution-api-ktx 16.0.0-เบต้า08
ปลั๊กอินการกระจายแอป com.google.firebase:firebase-appdistribution-gradle 4.0.0
การรับรองความถูกต้อง com.google.firebase:firebase-auth-ktx 22.0.0
Cloud Firestore com.google.firebase:firebase-firestore-ktx 24.6.1
ฟังก์ชันคลาวด์สำหรับ Firebase Client SDK com.google.firebase:firebase-functions-ktx 20.3.1
การส่งข้อความบนคลาวด์ com.google.firebase:firebase-messaging-ktx 23.1.2
การจัดเก็บเมฆ com.google.firebase:firebase-storage-ktx 20.2.0
แครชไลติค com.google.firebase:firebase-crashlytics-ktx 18.3.7
Crashlytics NDK com.google.firebase:firebase-crashlytics-ndk 18.3.7
ปลั๊กอิน Crashlytics com.google.firebase:firebase-crashlytics-gradle 2.9.5
รองรับโมดูลคุณลักษณะไดนามิก com.google.firebase:firebase-dynamic-module-support 16.0.0-เบต้า03
ลิงค์แบบไดนามิก com.google.firebase:firebase-dynamic-links-ktx 21.1.0
การส่งข้อความในแอป com.google.firebase:firebase-inappmessaging-ktx 20.3.2
(ที่จำเป็น)
การแสดงข้อความในแอป com.google.firebase:firebase-inappmessaging-display-ktx 20.3.2
(ที่จำเป็น)
การติดตั้ง Firebase com.google.firebase:firebase-installations-ktx 17.1.3
Firebase ML Model Downloader API com.google.firebase:firebase-ml-modeldownloader-ktx 24.1.2
การตรวจสอบประสิทธิภาพ com.google.firebase:firebase-perf-ktx 20.3.2
ปลั๊กอินการตรวจสอบประสิทธิภาพ com.google.firebase:perf-plugin 1.4.2
ฐานข้อมูลเรียลไทม์ com.google.firebase:firebase-database-ktx 20.2.2
การกำหนดค่าระยะไกล com.google.firebase:firebase-config-ktx 21.4.0
ปลั๊กอินบริการ Google Play com.google.gms:google-services 4.3.15
ห้องสมุดที่เลิกใช้แล้ว
App ตรวจสอบผู้ให้บริการ SafetyNet com.google.firebase:firebase-appcheck-safetynet 16.1.2
การทำดัชนีแอพ com.google.firebase:firebase-appindexing 20.0.0

ไลบรารี Firebase ML Kit

API โมเดลที่กำหนดเองของ Firebase ML com.google.firebase:firebase-ml-model-interpreter 22.0.4
Firebase ML Vision API com.google.firebase:firebase-ml-vision 24.1.0
Firebase ML: โมเดลการติดฉลากรูปภาพ com.google.firebase:firebase-ml-vision-image-label-model 20.0.2
Firebase ML: โมเดลการตรวจจับและติดตามวัตถุ com.google.firebase:firebase-ml-vision-object-detection-model 19.0.6
Firebase ML: โมเดลการตรวจจับใบหน้า com.google.firebase:firebase-ml-vision-face-model 20.0.2
Firebase ML: โมเดลการสแกนบาร์โค้ด com.google.firebase:firebase-ml-vision-barcode-model 16.1.2
Firebase ML: AutoML Vision Edge API com.google.firebase:firebase-ml-vision-automl 18.0.6
Firebase ML: API ภาษาธรรมชาติ com.google.firebase:firebase-ml-ภาษาธรรมชาติ 22.0.1
Firebase ML: โมเดลการระบุภาษา com.google.firebase:firebase-ml-ภาษาธรรมชาติ-ภาษา-id-model 20.0.8
Firebase ML: แปลโมเดล com.google.firebase:firebase-ml-natural-language-translate-model 20.0.9
Firebase ML: โมเดลการตอบกลับอัจฉริยะ com.google.firebase:firebase-ml-natural-language-smart-reply-model 20.0.8

Java

บริการหรือสินค้า การพึ่งพา Gradle ล่าสุด
รุ่น
เพิ่มการวิเคราะห์?
Firebase Android BoM
(รายการวัสดุ)
com.google.firebase:firebase-bom

Firebase BoM เวอร์ชันล่าสุดประกอบด้วยไลบรารี Firebase Android แต่ละเวอร์ชันล่าสุด หากต้องการเรียนรู้ว่าไลบรารีเวอร์ชันใดที่แมปกับเวอร์ชัน BoM เฉพาะ ให้ตรวจสอบบันทึกย่อประจำรุ่นสำหรับเวอร์ชัน BoM นั้น

32.1.0
AdMob com.google.android.gms:play-services-ads 22.1.0
การวิเคราะห์ com.google.firebase:firebase-analytics 21.3.0
แอปตรวจสอบผู้ให้บริการที่กำหนดเอง com.google.firebase:firebase-appcheck 17.0.1
ผู้ให้บริการตรวจแก้จุดบกพร่องของแอปตรวจสอบ com.google.firebase:firebase-appcheck-debug 17.0.1
แอปตรวจสอบผู้ให้บริการความสมบูรณ์ของ Play com.google.firebase:firebase-appcheck-playintegrity 17.0.1
การกระจายแอพ com.google.firebase:firebase-appdistribution 16.0.0-เบต้า08
API การกระจายแอป com.google.firebase:firebase-appdistribution-api 16.0.0-เบต้า08
ปลั๊กอินการกระจายแอป com.google.firebase:firebase-appdistribution-gradle 4.0.0
การรับรองความถูกต้อง com.google.firebase:firebase-auth 22.0.0
Cloud Firestore com.google.firebase:firebase-firestore 24.6.1
ฟังก์ชันคลาวด์สำหรับ Firebase Client SDK com.google.firebase:firebase-ฟังก์ชัน 20.3.1
การส่งข้อความบนคลาวด์ com.google.firebase:firebase-messaging 23.1.2
การจัดเก็บเมฆ com.google.firebase:firebase-storage 20.2.0
แครชไลติค com.google.firebase:firebase-crashlytics 18.3.7
Crashlytics NDK com.google.firebase:firebase-crashlytics-ndk 18.3.7
ปลั๊กอิน Crashlytics com.google.firebase:firebase-crashlytics-gradle 2.9.5
รองรับโมดูลคุณลักษณะไดนามิก com.google.firebase:firebase-dynamic-module-support 16.0.0-เบต้า03
ลิงค์แบบไดนามิก com.google.firebase:firebase-dynamic-links 21.1.0
การส่งข้อความในแอป com.google.firebase:firebase-inappmessaging 20.3.2
(ที่จำเป็น)
การแสดงข้อความในแอป com.google.firebase:firebase-inappmessaging-display 20.3.2
(ที่จำเป็น)
การติดตั้ง Firebase com.google.firebase:firebase-การติดตั้ง 17.1.3
Firebase ML Model Downloader API com.google.firebase:firebase-ml-modeldownloader 24.1.2
การตรวจสอบประสิทธิภาพ com.google.firebase:firebase-perf 20.3.2
ปลั๊กอินการตรวจสอบประสิทธิภาพ com.google.firebase:perf-plugin 1.4.2
ฐานข้อมูลเรียลไทม์ com.google.firebase:firebase-database 20.2.2
การกำหนดค่าระยะไกล com.google.firebase:firebase-config 21.4.0
ปลั๊กอินบริการ Google Play com.google.gms:google-services 4.3.15
ห้องสมุดที่เลิกใช้แล้ว
App ตรวจสอบผู้ให้บริการ SafetyNet com.google.firebase:firebase-appcheck-safetynet 16.1.2
การทำดัชนีแอพ com.google.firebase:firebase-appindexing 20.0.0

ไลบรารี Firebase ML Kit

API โมเดลที่กำหนดเองของ Firebase ML com.google.firebase:firebase-ml-model-interpreter 22.0.4
Firebase ML Vision API com.google.firebase:firebase-ml-vision 24.1.0
Firebase ML: โมเดลการติดฉลากรูปภาพ com.google.firebase:firebase-ml-vision-image-label-model 20.0.2
Firebase ML: โมเดลการตรวจจับและติดตามวัตถุ com.google.firebase:firebase-ml-vision-object-detection-model 19.0.6
Firebase ML: โมเดลการตรวจจับใบหน้า com.google.firebase:firebase-ml-vision-face-model 20.0.2
Firebase ML: โมเดลการสแกนบาร์โค้ด com.google.firebase:firebase-ml-vision-barcode-model 16.1.2
Firebase ML: AutoML Vision Edge API com.google.firebase:firebase-ml-vision-automl 18.0.6
Firebase ML: API ภาษาธรรมชาติ com.google.firebase:firebase-ml-ภาษาธรรมชาติ 22.0.1
Firebase ML: โมเดลการระบุภาษา com.google.firebase:firebase-ml-ภาษาธรรมชาติ-ภาษา-id-model 20.0.8
Firebase ML: แปลโมเดล com.google.firebase:firebase-ml-natural-language-translate-model 20.0.9
Firebase ML: โมเดลการตอบกลับอัจฉริยะ com.google.firebase:firebase-ml-natural-language-smart-reply-model 20.0.8



ขั้นตอนถัดไป

เพิ่มบริการ Firebase ในแอปของคุณ:

  • รับข้อมูลเชิงลึกเกี่ยวกับพฤติกรรมของผู้ใช้ด้วย Analytics

  • ตั้งค่าขั้นตอนการยืนยันตัวตนผู้ใช้ด้วย Authentication

  • จัดเก็บข้อมูล เช่น ข้อมูลผู้ใช้ ด้วย Cloud Firestore หรือ Realtime Database

  • จัดเก็บไฟล์ เช่น รูปภาพและวิดีโอด้วย Cloud Storage

  • ทริกเกอร์แบ็กเอนด์โค้ดที่ทำงานในสภาพแวดล้อมที่ปลอดภัยด้วย Cloud Functions

  • ส่งการแจ้งเตือนด้วย Cloud Messaging

  • ค้นหาว่าเมื่อใดและเพราะเหตุใดแอปของคุณจึงหยุดทำงานด้วย Crashlytics

เรียนรู้เกี่ยวกับ Firebase: