Are you want to know about Google Sign in SDK integration for andoid apps? Google plus login lets users login into your android app with their existing Google account and get their profile information like name, email and other details. By SDK integration google plus login in your apps, you can get all the user details in one shot.The major advantage of integrating G+ login is, you can drive more users to your app by providing quicker & easiest way of signup process.
So let’s start by doing required setup.
1. Installing / updating Google Play Services
Google plus is a part of Google Play Services API. So first we need to download the google play services in Android SDK manager. If you have already installed play services, it it very important to update it to latest version.Open the SDK manager and install or update the play services under Extras section.
2. Generating Google-Services.json
Now all the android projects which uses google apis, requires google-services.json file to be placed in project’s app folder. Follow the below steps to get your google-services.json file.
2.1 Java keytool can be used to generate SHA-1 fingerprint. Open your terminal and execute the following command to generate SHA-1 fingerprint. If it ask for password, type android and press enter.
On Windows
keytool -list -v -keystore "%USERPROFILE%.androiddebug.keystore" -alias androiddebugkey -storepass android -keypass android
On Linux or Mac OS
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
In the output you can notice SHA1 fingerprint.
2.2 Goto this https://developers.google.com/identity/sign-in/android/start-integrating link. This will redirect you to a page where you configure a project.Check the screenshot below for that
2.3 Create/Choose an app to configure the project
2.4 After giving the app name click on NEXT button,you need to choose application environment to prevent it from being abused. I have selected ANDROID as we are doing it for android app.
2.5 After selecting the application environment click on CREATE,where you will have an interface to completing the OAuth configuration.Provide the app package name (in my case i have given com.ahextech.ahexgoogleplusintegration) and SHA-1 key which we generated earlier.Finally click on CREATE to download your credentials.json
3. Creating New Project
3.1. Create a new project in Android Studio from File ⇒ New Project. When it prompts you to select the default activity, select Blank Activity and proceed.
While filling the project details, use the same package name which you gave in google console. In my case I am using same com.ahextech.ahexgoogleplusintegration
3.2. Open project level build.gradle and add ‘com.google.gms:google-services:3.0.0’ class path to dependencies.
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.google.gms:google-services:3.0.0’
}
3.3. Open app level build.gradle and add ‘compile com.google.android.gms:play-services-auth:9.2.1’ to dependencies.
dependencies {
// ..
compile 'com.google.android.gms:play-services-auth:9.2.1'
}
3.4. Now I am designing a simple layout to provide an interface to login through google plus.
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@drawable/bg1"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/logo"
android:src="@drawable/bg2"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/login_with_google_plus"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="25dp"
android:layout_below="@+id/logo"
android:background="#E12027">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/join_us_with_google_text"
android:src="@drawable/google_plus_logo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:id="@+id/join_us_with_google_text"
android:textSize="20sp"
android:text="@string/login_with"
/>
</RelativeLayout>
</RelativeLayout>
3.6 . Now open MainActivity.java and do the below modifications as per the SDK integration process
- Implement the activity from GoogleApiClient.OnConnectionFailedListener
- Create the GoogleApiClient instance in onCreate() method.
- signIn() performs google plus sign in
- onActivityResult() is called whenever user returns from Google Login UI.
- handleSignInResult() handles the google plus profile information upon successful login.
After doing the required changes my main activity code looks like below.
MainActivity.java
package com.ahextech.ahexgoogleplusintegration;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import org.w3c.dom.Text;
public class MainActivity extends Activity implements GoogleApiClient.OnConnectionFailedListener {
Typeface typeface;
RelativeLayout joinUsWithGoogleButton;
TextView joinUsWithGoogle;
private static final String TAG = MainActivity.class.getSimpleName();
private static final int RC_SIGN_IN = 007;
private GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Typeface to make the custom font for views
typeface = Typeface.createFromAsset(MainActivity.this.getAssets(), "verlag_book.otf");
joinUsWithGoogle = (TextView) findViewById(R.id.join_us_with_google_text);
joinUsWithGoogle.setTypeface(typeface);
joinUsWithGoogleButton = (RelativeLayout) findViewById(R.id.login_with_google_plus);
joinUsWithGoogleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
//Google+ sign successfull
GoogleSignInAccount acct = result.getSignInAccount();
String userName = acct.getDisplayName();
String userEmail = acct.getEmail();
//Here we fetched the user data successfully.We can use this data as per our needs/requirements.Here i'm just passing this data to next activity to display
Log.e(TAG, "User Name : " + userName + ",User email: " + userEmail);
//sending user data which we fetched from google plus SDK to next screen to display
Intent i = new Intent(getApplicationContext(),UserProfileInfoActivity.class);
i.putExtra("userName",userName);
i.putExtra("userEmail",userEmail);
startActivity(i);
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
}
- Try running the app to see the user data we fetched from google SDK in the logcat
- After choosing the google account which we want to fetch the user data we can able to see in the logcat shown below
- After getting the user data we can use them up according to our requirement.Here i’m just passing it to the next screen just to display over there
You can see the xml and java file for the above need as following –
activity_user_profile_info.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="@drawable/bg2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:id="@+id/welcome_text"
android:layout_below="@id/logo"
android:layout_marginTop="20dp"
android:textColor="#008DD3"
android:text="WELCOME TO AHEX TECHNOLOGIES"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:id="@+id/user_name_text"
android:layout_below="@id/welcome_text"
android:layout_marginTop="20dp"
android:textColor="#FFF"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:id="@+id/user_email_text"
android:layout_below="@id/user_name_text"
android:layout_marginTop="10dp"
android:textColor="#FFF"
/>
</RelativeLayout>
UserProfileInfoActivity.java
package com.ahextech.ahexgoogleplusintegration;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class UserProfileInfoActivity extends Activity {
TextView welcomeText, userNameText, userEmailText;
Typeface typeface;
String userNameValue,userEmailValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile_info);
typeface = Typeface.createFromAsset(UserProfileInfoActivity.this.getAssets(), "verlag_book.otf");
Bundle extras = getIntent().getExtras();
if (extras != null)
{
userNameValue = extras.getString("userName");
userEmailValue = extras.getString("userEmail");
}
welcomeText = (TextView) findViewById(R.id.welcome_text);
userEmailText = (TextView) findViewById(R.id.user_email_text);
userNameText = (TextView) findViewById(R.id.user_name_text);
userNameText.setText(userNameValue);
userEmailText.setText(userEmailValue);
welcomeText.setTypeface(typeface);
userNameText.setTypeface(typeface);
userEmailText.setTypeface(typeface);
}
}
- Try running the code now.You can able to see the the profile information of user we just fetched in the previous screen
- If you have followed all steps carefully, build and run your application. You will be able fetch the user information of the account you have chosen.You can use that information as per your requirement. Cheers!