Add Firebase to Your Android Project

Add Firebase to Your Android Project

Prerequisites
A device running Android 4.0 (Ice Cream Sandwich) or newer, and Google Play services 15.0.0 or higher.
The latest version of Android Studio
If you don't have an Android Studio project already, you can download one of our quickstart samples if you just want to try a Firebase feature. If you're using a quickstart, remember to get the application ID from the build.gradle file in your project's module folder (typically app/), as you'll need this package name for the next step.

Note: If you are upgrading from a 2.X version of the Firebase SDK, see our upgrade guide for Android to get started.
Add Firebase to your app
If you're using Android Studio version 2.2 or later, the Firebase Assistant is the simplest way to connect your app to Firebase. The Assistant can connect your existing project or create a new one for you with all the necessary gradle dependencies.

If you're using an older version of Android Studio or have a more complex project configuration, you can still manually add Firebase to your app.

Use the Firebase Assistant
To open the Firebase Assistant in Android Studio:

https://youtu.be/cNPCgJW8c-E



Click Tools > Firebase to open the Assistant window.
Click to expand one of the listed features (for example, Analytics), then click the provided tutorial link (for example, Log an Analytics event).
Click the Connect to Firebase button to connect to Firebase and add the necessary code to your app.

That's it! You can skip ahead to the next steps.

Manually add Firebase
It's time to add Firebase to your app. To do this you'll need a Firebase project and a Firebase configuration file for your app.

To create a Firebase project:

Create a Firebase project in the Firebase console, if you don't already have one.
Click Add project. If you already have an existing Google project associated with your mobile app, select it from the Project name drop down menu.

Otherwise, enter a project name to create a new project.
Optional: Edit your Project ID. Your project is given a unique ID automatically, and it's used in publicly visible Firebase features such as database URLs and your Firebase Hosting subdomain. You can change it now if you want to use a specific subdomain.
Follow the remaining setup steps and click Create project (or Add Firebase if you're using an existing project) to begin provisioning resources for your project. This typically takes a few minutes. When the process completes, you'll be taken to the project overview.


Now that you have a project, you can add your Android app to it:

Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.
When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.
During the process, you'll download a google-services.json file. You can download this file again at any time.
After you add the initialization code, run your app to send verification to the Firebase console that you've successfully installed Firebase.
Note: If you have multiple build variants with different package names defined, each app must be added to your project in Firebase console.
Add the SDK
If you would like to integrate the Firebase libraries into one of your own projects, you need to perform a few basic tasks to prepare your Android Studio project. You may have already done this as part of adding Firebase to your app.

First, add rules to your root-level build.gradle file, to include the google-services plugin and the Google's Maven repository:

buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.0.1' // google-services plugin
}
}

allprojects {
// ...
repositories {
// ...
google() // Google's Maven repository
}
}
Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:

apply plugin: 'com.android.application'

android {
// ...
}

dependencies {
// ...
implementation 'com.google.firebase:firebase-core:16.0.1'

// Getting a "Could not find" error? Make sure you have
// added the Google maven respository to your root build.gradle
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

You should also add the dependencies for the Firebase SDKs you want to use. We recommend starting with com.google.firebase:firebase-core, which provides Google Analytics for Firebase functionality. See the list of available libraries.

Note: To learn more about what the Gradle plugin does, see our Google Services Gradle Plugin guide.
Available libraries
The following libraries are available for the various Firebase features.

Gradle Dependency Line Service
com.google.firebase:firebase-core:16.0.1 Analytics
com.google.firebase:firebase-database:16.0.1 Realtime Database
com.google.firebase:firebase-firestore:17.0.4 Cloud Firestore
com.google.firebase:firebase-storage:16.0.1 Storage
com.google.firebase:firebase-crash:16.0.1 Crash Reporting
com.google.firebase:firebase-auth:16.0.2 Authentication
com.google.firebase:firebase-messaging:17.1.0 Cloud Messaging
com.google.firebase:firebase-config:16.0.0 Remote Config
com.google.firebase:firebase-invites:16.0.1 Invites and Dynamic Links
com.google.firebase:firebase-ads:15.0.1 AdMob
com.google.firebase:firebase-appindexing:16.0.1 App Indexing
com.google.firebase:firebase-perf:16.0.0 Performance Monitoring
com.google.firebase:firebase-functions:16.1.0 Cloud Functions for Firebase Client SDK
com.google.firebase:firebase-ml-vision:16.0.0 ML Kit (Vision)
com.google.firebase:firebase-ml-model-interpreter:16.0.0 ML Kit (Custom Model)
Note: com.google.firebase:firebase-core is a recommended alias for the com.google.firebase:firebase-analytics library.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Next steps
Learn about Firebase:

Explore sample Firebase apps.
Get hands-on experience with the Firebase Android Codelab.
Learn more with the Firebase in a Weekend course.
Add Firebase features to your app:

Gain insights on user behavior with Analytics.
Set up user authentication with Authentication.
Store user info with Realtime Database or blob data with Cloud Storage.
Send notifications to users with Cloud Messaging.
Find out when and why your app is crashing with Crashlytics.


Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License.

For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

https://youtu.be/cNPCgJW8c-E

Comments

Popular Posts