Friday, 26 August 2016

Firebase in Android - Getting started

Firebase is a cloud backend that enables you to quickly get synchronized data and use that for multi-user apps. This is important because nearly every mobile app these days requires authentication and real-time data updates.
Firebase is a gentle but very powerful introduction to storing and managing data. With just a few lines of code, you can read and write almost any data you could dream up from your own custom Firebase backend.
Furthermore, if your app needs any of the following features:
  • Online data storage
  • Real-time synchronization between many users
  • Authentication for Email/Password as well as OAuth providers
  • Data permissions and security
  • Offline access to data

then Firebase is the best solution now.

-Prerequisites:
  • A device running Android 2.3 (Gingerbread) or newer, and Google Play services 9.4.0 or newer
  • The Google Play services SDK from the Android SDK Manager
  • Android Studio 1.5 or higher
  • An Android Studio project and its package name

-Add Firebase to your app:

To add Firebase to your app you'll need a Firebase project and a Firebase configuration file for your app.
  • Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.
  • 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.
  • At the end, you'll download a google-services.json file. You can download this file again at any time.
  • If you haven't done so already, copy this into your project's module folder, typically app/.
-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:

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

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 {
  // ...
  compile 'com.google.firebase:firebase-core:9.4.0'
}

// ADD THIS LINE 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 Firebase Analytics functionality.

-Add the Permissions:
The Firebase library requires the android.permission.INTERNET permission to operate. Your app will not work unless you add this permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

-Setup Firebase on Android:
The Firebase library must be initialized once with an Android context. This must happen before any Firebase app reference is created or used. You can add the setup code to your Android Application file or Activity's onCreate method as follows:

@Override
public void onCreate() {
    super.onCreate();
    Firebase.setAndroidContext(this);
    .......
}

That's all. You are done with setting up of Firebase in your android app.
In the next blog we will going through other features of Firebase like
-Creating a user
-Authenticate a user
-Creating a database
-Writing values to database
-Reading database values.





No comments:

Post a Comment