In today’s mobile world, making it easy for users to sign into your app is crucial. Nobody wants to create yet another account with a password they’ll forget. This is where Google Sign-In SDK Integration for Android apps comes in handy.
Google Sign-In lets your users log in using their existing Google account in just a few taps. It’s simple, secure, and fast. In this guide, we’ll walk you through everything you need to know about integrating Google Sign-In Android authentication into your Android application. Whether you’re building a new app or updating an existing one, this step-by-step tutorial will help you get it done.
Why Does Google Sign-In Matters for Your Android App?
Think about how many times you’ve abandoned an app because the registration process was too complicated. Users appreciate simple solutions. Android Google authentication solves this problem perfectly.
Here’s why Google authentication Android app integration is important:
- Faster user onboarding: Users don’t need to create new accounts. They sign in with their Google credentials.
- Better security: Google handles password security, so you don’t have to worry about it.
- User data: You get access to basic user information like name and email without asking users to fill out forms.
- Lower abandonment rates: Simple login means more users actually complete the signup process.
- Cross-platform consistency: If your users have your app on multiple devices, their login experience is seamless.
Understanding Google Sign-In Methods
Before diving into code, let’s understand what Google Sign-In offers. Google login integration Android comes in different forms:
Traditional OAuth 2.0 Flow
This is the classic way to handle Android app authentication. When users tap the sign-in button, they’re taken to Google’s login page, where they verify their identity. Once confirmed, your app gets a token to identify that user.
Google Identity Services
The newer approach for Google Identity Services Android uses streamlined methods. It’s faster, requires less code, and works better with modern Android apps.
Firebase Integration
Many developers use Firebase Google Sign-In because it connects with other Firebase services like real-time database and cloud functions. This approach ties everything together nicely.
Token-Based Authentication
This method uses Android login SDK principles where a token is generated, sent to your server, and verified there. It’s highly secure for apps with backend systems.
Before You Begin: What You Need
To follow this guide on Sign in with Google Android, you’ll need:
- Android Studio (latest version).
- A Google Cloud Project with Google Sign-In API enabled.
- A debug keystore or app signing certificate.
- Basic knowledge of Android development.
- An emulator or physical Android device for testing.
Step 1: Set Up Your Google Cloud Project
First, head to the Google Cloud Console. If you don’t have a project, create one. Then:
- Enable the Google Sign-In API for your project.
- Create OAuth 2.0 credentials (choose “Android” as the platform).
- You’ll need your app’s package name and SHA-1 fingerprint.
- To get your SHA-1 fingerprint, run this command in Terminal:
keytool -list -v -keystore ~/.android/debug.keystore -alias android debugkey -storepass android -keypass android
Step 2: Create Your Android Project
- Open Android Studio and create a new project:
- File → New → New Project.
- Choose “Empty Activity” as your starting template.
- Name your project (for example, “GoogleSignInApp”).
- Make sure the minimum SDK is at least API 21.
Step 3: Add Required Dependencies
Your Android app needs some extra tools to work with Google’s login system. Think of it like going to a shop and getting tools before you start building something.
- Open the file called “build.gradle” in your project. You’ll see a section that says “dependencies”. This is where you list all the tools your app needs.
- You need to add two things to this list. The first one helps your app talk to Google for login. The second one is the newer, faster way to do login on phones.
- After you add these two tools, save the file. Android Studio will automatically get these tools for you. Just wait a minute while it downloads.
Step 4: Update Your App’s Permission Settings
Every Android app has a file called “AndroidManifest.xml”. This file is like a list of rules that tells Android what your app is allowed to do.
- Your app needs to ask Android: “Can I use the internet?” This is important because your app needs to talk to Google’s servers to check if someone’s login is real.
- Open the AndroidManifest.xml file. Look for something that says “uses-permission”. You need to make sure it says “android.permission.INTERNET” somewhere in there. If you don’t see it, add it.
- This tells Android: “This app needs the internet to work.” Without this, your app won’t be able to connect to Google authentication Android app servers.
Step 5: Design Your Login Screen
Now let’s make the screen that users will actually see. This is the screen with the “Sign In” button. Open the file called “activity_main.xml”. This file is like a drawing where you decide what your app’s main screen looks like.
You’re going to add a few things:
- First, add some text that says “Sign in to continue”. This welcome message greets the user.
- Second, add the Google Sign-In Android button. This is a special button made by Google. When someone clicks it, they’ll see Google’s login page.
- Third, add a small area below where you’ll show the user’s name and email after they sign in.
- Finally, add a “Sign Out” button. Don’t worry – this button will be hidden at first. It will only show up after someone signs in.
When people first open your app, they’ll see the “Sign In” button. Once they sign in with their Google account, the button disappears and shows their name instead.
Step 6: Add the Login Logic
Now comes the important part – making everything work. Open your “MainActivity.java” file. This is where you write the instructions that tell your app what to do.
Here’s what needs to happen:
- When your app starts, it should check – “Is someone already signed in?” If yes, show their name and the “Sign Out” button. If no, show the “Sign In” button.
- When someone clicks the “Sign In” button, your app opens Google’s login page. They type their email and password there – not in your app.
- After they sign in, Google sends back a message to your app saying “This person is real and their name is John.” You then show that person’s name and email on your screen.
- When someone clicks “Sign Out”, your app disconnects from Google and goes back to showing the “Sign In” button.
You’ll use the tools you added earlier to do all this. These tools do most of the hard work – you just need to connect the buttons to the actions.
Step 7: Test Your App
Open your app on your phone or emulator. Here’s what should happen:
- You’ll see the “Sign In” button on the screen.
- You click the button.
- Google’s login page appears. You pick a Google account or type in your email and password.
- Your app brings you back to the main screen.
- Now you see your name and email showing on the app. The “Sign In” button is gone, and the “Sign Out” button appears instead.
In case, something goes wrong
Problem 1: You see “DEVELOPER_ERROR”
This means Google doesn’t recognize your app yet. It happens when the security code you added to Google doesn’t match your real app. Go back and check that code again. Make sure you copied it exactly right with no spaces or extra letters.
Problem 2: You see “NETWORK_ERROR”
This means your app can’t reach Google’s servers. Make sure your phone or emulator has internet. Also double-check that you added the internet permission in Step 4.
Problem 3: The emulator has no Google accounts
If you’re testing on a pretend phone (emulator), you might need to add a Google account to it first. Go to Settings in the emulator and add an account. Or use a real phone instead.
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!