Firebase Authentication in Android Studio with Kotlin (including Google Authentication)

Meriç Alkışla
6 min readJun 11, 2024

--

Hi, 🖐 in this article I will talk about how to use Firebase Auth, the authentication process in many applications, with Kotlin on Android Studio.

What is Firebase?

In essence, Firebase is a comprehensive mobile and web application development platform backed by Google. It offers a suite of tools and services that streamline various aspects of app development, including authentication, real-time database management, hosting, and analytics. With Firebase, developers can build high-quality apps faster, scale effortlessly, and engage users effectively through personalized experiences.

Let’s see how we can use Firebase Authentication in our app.

Prerequisites

  1. Install Android Studio or update to the latest version.
  2. Ensure your project meets these requirements (note that some products may have stricter requirements):
  • Targets API level 19 (KitKat) or higher.
  • Running Android 4.4 or later.
  • Must use Jetpack (AndroidX), including meeting the following version requirements:

com.android.tools.build:gradle 7.3.0 or later.

compileSdkVersion 28 or higher.

3. Set up a physical device or use an emulator to run your app.

4. Note that Firebase SDKs with dependencies on Google Play services require Google Play Services to be installed on the device or emulator.

5. Sign in to Firebase using your Google Account.

Step 1: Create a New Project in Android Studio

  1. Open Android Studio.
  2. Select “Start a new Android Studio project”.
  3. Choose a suitable template for your project.
  4. Configure your project name and save location.
  5. Make sure the language is set to Kotlin.
  6. Click “Finish”.

Sample Design:

Step 2: Connect Your App to Firebase

Create a Firebase Project

  1. Go to the Firebase console.
  2. Click on “Add project”.
  3. To add Firebase resources to an existing Google Cloud project, enter the project name or select the project name from the drop-down menu. To create a new project, enter the desired project name.
  4. Optionally, edit the project ID shown below the project name.
  5. Click “Continue” and follow the prompts to complete the project creation process.

Register Your App in Firebase

  1. In the Firebase console, click the Android icon in the center of the project overview page or click “Add app” to start the installation workflow.
  2. Enter the package name of your app in the Android package name field. The package name uniquely identifies your app on the device and in the Google Play Store. Find your app’s package name in your module (app level) Gradle file (usually app/build.gradle) (example package name: com.yourcompany.yourproject). Note that the package name value is case sensitive and cannot be changed for this Firebase Android app once it is saved in your Firebase project.
  3. Enter other app information: App alias and SHA-1 signature certificate debug. Debug SHA-1 signature certificate: SHA-1 hash required when using Firebase Authentication (when using Google Sign-in or phone number sign-in) and Firebase Dynamic Links.
  4. Click “Register app”.

Step 3: Add Firebase Configuration File

  1. Download the Firebase Android configuration file (google-services.json) and add it to your app:
  • Click “Download google-services.json" to get your Firebase Android configuration file.
  • Move your configuration file to the module (application level) root directory of your application.

You need the Google services Gradle plugin (google-services) to make the values in your google-services.json configuration file accessible to Firebase SDKs.

2. Add the Google services plugin to your project:

  • Add the Google services plugin as a dependency to your root-level (project-level) Gradle file (<project>/build.gradle.kts or <project>/build.gradle):
buildscript{
dependencies {
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.5.3")
}
}

plugins {

// ...
id("com.google.gms.google-services") version "4.4.2" apply false
}
  • Add the Google services plugin to your module (application level) Gradle file (<project>/<app-module>/build.gradle.kts or <project>/<app-module>/build.gradle):
plugins {
// ...

id("com.google.gms.google-services")
id("androidx.navigation.safeargs.kotlin")
}

dependencies {
// ...

implementation(platform("com.google.firebase:firebase-bom:33.1.0"))
implementation("com.google.firebase:firebase-auth")
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-firestore")
//Google Auth
implementation("com.google.android.gms:play-services-auth:21.0.0")
}

3. After adding its dependencies, synchronize your Android project with Gradle files.

From the Gradle menu, click on “Execute Gradle Task” and enter singingReport in the search field. It will give us the SHA-1 signature certificate key we need.

Update the Firebase Android configuration file (google-services.json) by entering the SHA-1 key, download it again and replace it with the google-services.json in your application.

Step 4: Sign-in Method

  1. Since we will be using email and password or Google auth for authentication, enable Email/Password and Google Sign-in method in the Firebase console under Authentication:
  • Go to the Firebase console.
  • Select “Authentication” from the left-hand menu.
  • Click on the “Sign-in method” tab.
  • Enable “Email/Password” and “Google” sign-in methods.

Step 5: Let’s look at other required coding

You should use these codes, as an example of how to apply them to your application.

You can also use the codes here as source.

Note:

  • default_web_client_id should be automatically created in values.xml. If not, you can access the key by selecting your project from Google Cloud Console, navigating to "APIs & Services" > "Credentials" and under "OAuth 2.0 Client IDs" find "Web client (auto created by Google Service)".

Video of the application:

I hope this article was helpful in guiding you through the process of implementing Firebase Authentication in your Android application using Kotlin. Feel free to check out the Github repository for the complete project. See you in the next article. Take care of yourselves. 🖐

--

--