Debug School

rakesh kumar
rakesh kumar

Posted on

Role of Google Services Gradle Plugin in Flutter

google-services-plugin
Question

what is the funtion of google-services-plugin

  1. Process the google-services. json file .
  2. produce Android resources that can be used in your application's code.
  3. Add dependencies for basic libraries required for the services you have enabled

1.Process the google-services.json file and produce Android resources that can be used in your application's code. See Adding the JSON File more information.
2.Add dependencies for basic libraries required for the services you have enabled. This step requires that you apply the Google Services Gradle plugin in your app/build.gradle file, like so:
apply plugin: 'com.google.gms.google-services'

You can see the result of this step by running ./gradlew :app:dependencies.

How to add dependencies basic libraries required for the services you have enabled

Add dependencies for basic libraries required for the services you have enabled. This step requires that you apply the Google Services Gradle plugin in your app/build.gradle file, like so:

apply plugin: 'com.google.gms.google-services'
Enter fullscreen mode Exit fullscreen mode
You can see the result of this step by running ./gradlew :app:dependencies.
Enter fullscreen mode Exit fullscreen mode

Expalin element of Processing the JSON File

The google-services.json file has the following basic structure:


{
  "project_info": {...},
  "client": [...],
}
Enter fullscreen mode Exit fullscreen mode

The** project_info** object contains general information about your project, while each member of the client array contains information about the clients (Android apps) that you have added to the project.

When processing the JSON file for your Android app, the plugin only uses the client object that matches your package name (for the current build type) based on the following logic:

For each member of the client array:
Check the value of client_info/android_client_info/package_name
If the package name matches this value, return the member object.
If none of the members of client match the package name, an exception is thrown.
For the rest of this document we will use {YOUR_CLIENT} to refer to the member of the client array determined by the procedure above.

The main result of the JSON processing is to produce two XML files which you can reference as Android resources in your Java code. Below is an example of each file:

app/build/generated/res/google-services/{build_type}/values/values.xml
Enter fullscreen mode Exit fullscreen mode

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <! -- Present in all applications -->
    <string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>

    <! -- Present in applications with the appropriate services configured -->
    <string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
    <string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
    <string name="ga_trackingId" translatable="false">UA-65557217-3</string>
    <string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
    <string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="project_id" translatable="false">mydemoapp</string>

</resources>
app/build/generated/res/google-services/{flavor}/{build_type}/xml/global_tracker.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ga_trackingId" translatable="false">UA-65557218-3</string>
</resources>
Enter fullscreen mode Exit fullscreen mode

Every value in the XML files is present in the google-services.json file at the locations below. If your Android project has some configuration that prevents you from using the google-services plugin, you can safely recreate the XML files manually using these values:

google_app_id:


{YOUR_CLIENT}/client_info/mobilesdk_app_id
gcm_defaultSenderId:
Enter fullscreen mode Exit fullscreen mode

project_info/project_number

default_web_client_id:
Enter fullscreen mode Exit fullscreen mode
{YOUR_CLIENT}/oauth_client/client_id (client_type == 3)
ga_trackingId:
Enter fullscreen mode Exit fullscreen mode
{YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id
firebase_database_url:
Enter fullscreen mode Exit fullscreen mode

project_info/firebase_url

google_api_key:
Enter fullscreen mode Exit fullscreen mode

{YOUR_CLIENT}/api_key/current_key

google_crash_reporting_api_key:
Enter fullscreen mode Exit fullscreen mode

{YOUR_CLIENT}/api_key/current_key

project_id:
Enter fullscreen mode Exit fullscreen mode

project_info/project_id

Top comments (0)