Module plugin-google-gms

Set up your Google Cloud project for applications with Google Services (Google Auth)

To access Google APIs, generate a unique client_id for your app in the Google API Console. Add the client_id to your app’s code and complete the required Cloud Console setup steps:

Steps

  1. Go to the Google Cloud Console and open the project selector page.
  2. Click on “Create Project” to start creating a new Cloud project.
  3. Go to the Credentials page.
  4. On the Credentials page, click on “Create credentials” and choose “OAuth Client ID”.
  5. In the “Application Type” option, select “Android”.
  6. Set your application package name (Use “com.openmobilehub.android.auth.sample” if you are following the starter-code)
  7. Update the debug/release SHA-1 certificate fingerprint for Android’s Client ID. Note: The debug build is automatically signed with the debug keystore. Obtain the certificate fingerprint from it by following the guidelines in the official Google Developers documentation: “Using keytool on the certificate”.
  8. In the OAuth consent screen add the test users that you will be using for QA and development. Without this step you won’t be able to access the application while it’s in testing mode.
  9. You’re all set!

Add the Client ID to your app

You should not check your Client ID into your version control system, so it is recommended storing it in the local.properties file, which is located in the root directory of your project. For more information about the local.properties file, see Gradle properties files.

  1. Open the local.properties in your project level directory, and then add the following code. Replace YOUR_GOOGLE_CLIENT_ID with your API key. GOOGLE_CLIENT_ID=YOUR_GOOGLE_CLIENT_ID
  2. Save the file and sync your project with Gradle.

Gradle configuration

To incorporate the Google GMS and non-GMS plugin into your project, you have two options: directly include the Android OMH Client libraries dependencies or utilize the Android OMH Core Plugin.

Directly including the Google GMS and non-GMS dependencies

If you want to incorporate Google plugin into your project without using the Android OMH Core plugin, you have to directly include the Google GMS and non-GMS plugins as a dependency. In the build.gradle.kts, add the following implementation statement to the dependencies{} section:

implementation("com.openmobilehub.android.auth:plugin-google-gms:2.0.4")
implementation("com.openmobilehub.android.auth:plugin-google-non-gms:2.0.4")

Save the file and sync your project with Gradle.

Using the Android OMH Core plugin

Please see the advanced documentation on how to use the Android OMH Core Plugin.

Provide the Google OMH Auth Client

In the SingletonModule.kt file in the :auth-starter-sample module add the following function to provide the Google OMH Auth Client:

@Provides
fun providesGoogleAuthClient(@ApplicationContext context: Context): OmhAuthClient {
    val omhAuthProvider = OmhAuthProvider.Builder()
        .addNonGmsPath("com.openmobilehub.android.auth.plugin.google.nongms.presentation.OmhAuthFactoryImpl")
        .addGmsPath("com.openmobilehub.android.auth.plugin.google.gms.OmhAuthFactoryImpl")
        .build()

    return omhAuthProvider.provideAuthClient(
        scopes = listOf("openid", "email", "profile"),
        clientId = BuildConfig.GOOGLE_CLIENT_ID,
        webClientId = BuildConfig.GOOGLE_WEB_CLIENT_ID,
        context = context
    )
}

We’d recommend to store the client as a singleton with your preferred dependency injection library as this will be your only gateway to the OMH Auth SDK and it doesn’t change in runtime at all.

Escape Hatch

This plugin provides an escape hatch to access the native Google Android SDK. This allows developers to use the underlying provider’s API directly, should they need to access a feature of the provider that is not supported by the OMH plugin.

You can obtain the Google client instances by casting the result of getProviderSdk to GoogleSignInClient:

import com.google.android.gms.auth.api.signin.GoogleSignInClient
...
authClient.getProviderSdk() as GoogleSignInClient