เพิ่ม Firebase ลงในโปรเจ็กต์ Android

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

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

  • ตรวจสอบว่าโปรเจ็กต์เป็นไปตามข้อกำหนดเหล่านี้ (โปรดทราบว่าผลิตภัณฑ์บางอย่างอาจมีข้อกำหนดที่เข้มงวดกว่า)

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

หากยังไม่มีโปรเจ็กต์ 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. (ไม่บังคับ) ป้อนชื่อเล่นของแอป ซึ่งเป็นตัวระบุภายในที่สะดวกและมีเพียงคุณเท่านั้นที่มองเห็นในคอนโซล Firebase

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

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

  1. ดาวน์โหลดแล้วเพิ่มไฟล์การกำหนดค่า Firebase ของแอป (google-services.json) ลงในฐานของโค้ด โดยทำดังนี้

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

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

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

    1. ในไฟล์ Gradle ระดับรูท (ระดับโปรเจ็กต์) (<project>/build.gradle.kts หรือ <project>/build.gradle) ให้เพิ่มปลั๊กอิน Gradle ของบริการของ Google เป็นทรัพยากร Dependency ดังนี้

      Kotlin

      plugins {
        id("com.android.application") version "7.3.0" apply false
        // ...
      
        // Add the dependency for the Google services Gradle plugin
        id("com.google.gms.google-services") version "4.6.0" apply false
      }

      Groovy

      plugins {
        id 'com.android.application' version '7.3.0' apply false
        // ...
      
        // Add the dependency for the Google services Gradle plugin
        id 'com.google.gms.google-services' version '4.6.0' apply false
      }
    2. ในไฟล์ Gradle ระดับโมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle) ให้เพิ่มปลั๊กอิน Gradle ของบริการของ Google ดังนี้

      Kotlin

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

      Groovy

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

    เปิดใช้ Analytics แล้ว

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation(platform("com.google.firebase:firebase-bom:35.2.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 เวอร์ชันที่เข้ากันได้อยู่เสมอ

    ไม่ได้เปิดใช้ Analytics รายการ

    dependencies {
      // ...
    
      // Import the Firebase BoM
      implementation(platform("com.google.firebase:firebase-bom:35.2.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. หลังจากเพิ่มทรัพยากร Dependency สำหรับผลิตภัณฑ์ที่ต้องการใช้แล้ว ให้ซิงค์โปรเจ็กต์ Android กับไฟล์ Gradle

เพียงเท่านี้ก็เสร็จแล้ว! คุณสามารถข้ามไปดูขั้นตอนถัดไปที่แนะนำได้

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



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

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

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

    • Windows / Linux: Help > Check for updates
    • macOS: Android Studio > ตรวจหาอัปเดต
  2. เปิด Firebase Assistant: เครื่องมือ > Firebase

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

    1. คลิกเชื่อมต่อกับ Firebase เพื่อเชื่อมต่อโปรเจ็กต์ Android กับ Firebase

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

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

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

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

เพียงเท่านี้ก็เสร็จแล้ว! อย่าลืมดูขั้นตอนถัดไปที่แนะนำ

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



ไลบรารีที่ใช้ได้

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

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

บริการหรือผลิตภัณฑ์ ทรัพยากร Dependency ของ Gradle เวอร์ชัน
ล่าสุด
เพิ่ม Analytics ใช่ไหม
Firebase Android BoM
(Bill of Materials)
com.google.firebase:firebase-bom

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

35.2.0
ปลั๊กอิน Gradle ของบริการ Google com.google.gms:google-services 4.6.0
AdMob com.google.android.gms:play-services-ads 25.5.0
Firebase AI Logic 1 com.google.firebase:firebase-ai 18.2.0
Firebase AI Logic On-Device com.google.firebase:firebase-ai-ondevice 16.0.0-beta08
Firebase AI KSP com.google.firebase:firebase-ai-ksp-processor 17.0.0
Analytics com.google.firebase:firebase-analytics 24.0.0
ผู้ให้บริการที่กำหนดเอง App Check ราย com.google.firebase:firebase-appcheck 20.1.0
ผู้ให้บริการแก้ไขข้อบกพร่อง App Check com.google.firebase:firebase-appcheck-debug 20.1.0
ผู้ให้บริการ Play Integrity App Check com.google.firebase:firebase-appcheck-playintegrity 20.1.0
ผู้ให้บริการ App Check reCAPTCHA com.google.firebase:firebase-appcheck-recaptcha 20.1.0
App Distribution com.google.firebase:firebase-appdistribution 16.0.0-beta21
App Distribution API com.google.firebase:firebase-appdistribution-api 16.0.0-beta21
ปลั๊กอิน App Distribution com.google.firebase:firebase-appdistribution-gradle 5.3.1
Authentication com.google.firebase:firebase-auth 25.0.0
Cloud Firestore com.google.firebase:firebase-firestore 27.2.0
Cloud Functions for Firebase Client SDK com.google.firebase:firebase-functions 23.0.0
Cloud Messaging com.google.firebase:firebase-messaging 26.0.2
Cloud Storage com.google.firebase:firebase-storage 23.0.0
Crashlytics com.google.firebase:firebase-crashlytics 21.1.0
Crashlytics NDK com.google.firebase:firebase-crashlytics-ndk 21.1.0
ปลั๊กอิน Crashlytics com.google.firebase:firebase-crashlytics-gradle 3.0.8
การรองรับโมดูลฟีเจอร์แบบไดนามิก com.google.firebase:firebase-dynamic-module-support 16.0.0-beta05
In-App Messaging com.google.firebase:firebase-inappmessaging 23.0.0
(ต้องระบุ)
In-App Messaging Display com.google.firebase:firebase-inappmessaging-display 23.0.0
(ต้องระบุ)
มีการติดตั้งไปแล้ว Firebase ครั้ง com.google.firebase:firebase-installations 20.0.0
Performance Monitoring com.google.firebase:firebase-perf 23.0.1
ปลั๊กอิน Performance Monitoring com.google.firebase:perf-plugin 2.0.2
Firebase Phone Number Verification com.google.firebase:firebase-pnv 17.0.0
Realtime Database com.google.firebase:firebase-database 23.0.0
Remote Config com.google.firebase:firebase-config 24.0.1
SQL Connect 2 com.google.firebase:firebase-dataconnect 18.0.1
ไลบรารีที่เลิกใช้งานแล้วหรือไม่รองรับ
Dynamic Links com.google.firebase:firebase-dynamic-links 22.1.0

โมดูล Firebase KTX - ไม่รองรับอีกต่อไป

Analytics อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX แล้ว
com.google.firebase:firebase-analytics-ktx
22.5.0
ผู้ให้บริการที่กำหนดเอง App Check ราย อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-appcheck-ktx
18.0.0
App Distribution API อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-appdistribution-api-ktx
16.0.0-beta15
Authentication อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-auth-ktx
23.2.1
Cloud Firestore อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-firestore-ktx
25.1.4
Cloud Functions for Firebase Client SDK อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-functions-ktx
21.2.1
Cloud Messaging อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-messaging-ktx
24.1.2
Cloud Storage อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-storage-ktx
21.0.2
Crashlytics อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-crashlytics-ktx
19.4.4
Dynamic Links อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-dynamic-links-ktx
22.1.0
In-App Messaging อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-inappmessaging-ktx
21.0.2
(ต้องระบุ)
In-App Messaging Display อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-inappmessaging-display-ktx
21.0.2
(ต้องระบุ)
มีการติดตั้งไปแล้ว Firebase ครั้ง อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-installations-ktx
18.0.0
API สำหรับดาวน์โหลดโมเดล Firebase ML อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-ml-modeldownloader-ktx
25.0.1
Performance Monitoring อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-perf-ktx
21.0.5
Realtime Database อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-database-ktx
21.0.0
Remote Config อย่าใช้ เนื่องจากระบบไม่รองรับไลบรารีโมดูล KTX อีกต่อไป
com.google.firebase:firebase-config-ktx
22.1.2

ไลบรารี Firebase ML และ Firebase ML Kit

API โมเดลที่กำหนดเองของ Firebase ML com.google.firebase:firebase-ml-model-interpreter 22.0.4
API สำหรับดาวน์โหลดโมเดล Firebase ML com.google.firebase:firebase-ml-modeldownloader 26.1.1
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: Natural Language API com.google.firebase:firebase-ml-natural-language 22.0.1
Firebase ML: โมเดลการระบุภาษา com.google.firebase:firebase-ml-natural-language-language-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

1 Firebase AI Logic เดิมชื่อ "Vertex AI in Firebase" โดยมีแพ็กเกจ com.google.firebase:firebase-vertexai

2 Firebase SQL Connect เดิมเรียกว่า "Firebase Data Connect"



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

เพิ่มบริการ Firebase ลงในแอป

  • สร้างฟีเจอร์ Generative AI ด้วยโมเดล Gemini โดยใช้ Firebase AI Logic

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

  • ตั้งค่าขั้นตอนการตรวจสอบสิทธิ์ของผู้ใช้ด้วย Authentication

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

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

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

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

  • ดูว่าแอปขัดข้องเมื่อใดและเพราะเหตุใดด้วย Crashlytics

ดูข้อมูลเกี่ยวกับ Firebase