Working with Google Firebase
In this series of post we will be looking on how to integrate Google Firebase in our Android application.
Firebase is a technology that permits you to make web applications with no server-side programming so that development turns out to be quicker and easier. With Firebase, we don’t have to stress over-provisioning servers or building REST APIs with just a little bit of configuration; we can give Firebase a chance to take every necessary step: storing data, verifying users, and implementing access rules.
Firebase includes features which you can add in your project. This reduces a lot of development time. Starter pack for Firebase is free and you can scale as your app grows.
Prerequisites
- A device running Android 2.3 (Gingerbread) or newer, and Google Play services 10.0.1 or higher
- The Google Play services SDK from the Google Repository, available in the Android SDK Manager
- The latest version of Android Studio, version 1.5 or higher
Setting up project on Firebase Console
Creating new project
First thing you have to create a project on Firebase console for getting started with integrating modules. Login into you google account and go to Firebase Console. You will be shown screen like this.
Here click on Create new project. Which will show a popup with project name and country.
Enter proper project name and country here and then click on Create project button. Now your project is added into Firebase console.
Create app inside project
Now you can add different projects here. Below screen shows the content.
Since we are creating this for Android, So we will continue with choosing android option here. This will present a popup asking for Application Package Name and App nickname. After entering details when you hit add App in bottom.
After clicking this you will be prompted to download google-services.json which contain api keys and other data used by firebase. Save this file as this will be added to your Android Studio project.
Just click next on other screens which will provide some info. This is all you need to to setup in Firebase console.
Firebase Setup in Android studio
There are few things which you need to setup in your Android Studio also.
Add google-services.json to gradle project
First thing you have to do is to add google-services.json file to our project. This should be added to root of the project module as shown in picture below.
Update gradle files
Along with this you also have to update your gradle files. The project level build.gradle will look like this.
Project config for gradle
For project level build.gradle file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.google.gms:google-services:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } |
Here you have to add classpath for google services.
Also in module level build.gradle file you have to add highlighted lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "net.androidsrc.remoteconfig" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.0' testCompile 'junit:junit:4.12' //firebase core compile 'com.google.firebase:firebase-core:10.0.1' } |
This is all to need to add for adding different Firebase module in your app. Next we will be covering different modules.