In our previous posts, we were discussing about DoodleBlue Android Interview Questions with Answers – PART 1, DoodleBlue Android Interview Questions with Answers – PART 2. In this post, let us discuss brielfy about Google Integration in Android.
DoodleBlue Android Interview Questions with Answers – Google Integration in Android
What is Google Integration in Android:
Nowadays, as everyone tend to have a google and facebook account, Google integration with Android is a really fruitful feature for both the app developer and the user. Moreover, with this Android login with Google, the UserId and password need not be remembered. Before you can start integrating Google Sign-In in your own app, you must configure an Google API Console project and set up your Android Studio project.
Prerequisites for Google Integration in Android:
1. Android Studio installed on your PC (Unix or Windows).
2. A real time android device (Smartphone or Tablet) configured with Android Studio.
3. A compatible Android deveice that runs Android 4.0 or newer that comprise of the Google Play Store or an emulator with an AVD that runs the Google APIs platform based on Android 4.2.2 or newer and the Google Play services version 15.0.0 or newer.
4. The latest version of the Android SDK, with the SDK Tools compnent.
5. A project compiled against Android 4.0 or newer.
Updating or Installing Google Play Services:
To update or install the Google Play services SDK:
i) In Android Studio, select Tools > Android > SDK Manager.
ii) Select Extras > Google Repository at the bottom of the page.
iii) The package is downloaded to your computer and installed in your SDK environment at android-sdk-folder/extras/google/google_play_services.
iv) This guide is written for users of Android Studio, which is the recommended development environment.
Steps in integration of Google with Android:
1. Adding Google Play Services –
In your project’s top-level build.gradle file, ensure that Google’s Maven repository is included:
allprojects {
repositories {
google()
// If you’re using a version of Gradle lower than 4.1, you must instead use:
// maven {
// url ‘https://maven.google.com’
// }
}
}
Then, in your app-level build.gradle file, declare Google Play services as a dependency:
apply plugin: ‘com.android.application’
…
dependencies {
compile ‘com.google.android.gms:play-services-auth:15.0.1’
}
2.Configuring a Google API Console project –
=>Go to Google Developer’s Page. To get it, you must select an existing project for your app or create a new one. You’ll also need to provide a package name for your app.
=>Create a new project in android studio project. Name the project and give its a package name. The LoginActivity is adopted as the activity name.
=>Now, the app name is added.
=>Click Choose > Configure Services > Google Sign-In.
=>Before continuing in this page, you will also need to generate the SHA-1 fingerprint.
=>Java key tool is used to generate the SHA-1 fingerprint.
=>After generating it, copy the SHA-1 ID in your terminal.
=>This will download and install configuration page.
=>Copy the google-services file you just downloaded into the app / or mobile / directory of your Android Studio project.
3.Getting your backend server’s OAuth 2.0 client ID:
=>If your app authenticates with a backend server or accesses Google APIs from your backend server, you must get the OAuth 2.0 client ID that was created for your server.
=>To find the OAuth 2.0 client ID:
A. Open the Credentials page in the API Console.
B. The Web application type client ID is your backend server’s OAuth 2.0 client ID.
C. Pass this client ID to the requestIdToken or requestServerAuthCode method when you create the GoogleSignInOptions object.
4.Next steps:
Now that you have configured a Google API Console project and set up your Android Studio
project, you can integrate Google Sign-In into your app.
After the user signs in for the first time, a new user account is created and linked to the attestation i.e., the user name and password, phone number, or auth provider information – the user signed in with.
This new account is saved as part of your Firebase projects, and can be used to nalyse a user across every app in your project, regardless of how the user signs in.
In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the signed-in user’s unique user ID to control what data a user can access.
To sign out a user, call signOut:
FirebaseAuth.getInstance().signOut();
By linking auth provider credentials to an existing user account, you can allow users to sign in to your app through various authentication providers.
To be Continued….
References:
https://developers.google.com/identity/sign-in/android/start-integrating
https://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/
https://firebase.google.com/docs/auth/android/google-signin
https://www.androidtutorialpoint.com/material-design/adding-google-login-android-app/
https://stackoverflow.com/questions/33934362/how-to-integrate-google-sign-in-my-android-app