Subchapter 3.2
references/android_setup.mdMarkdown23 KBView on GitHub
Guides the installation and initialization of the Google Analytics for Firebase (GA4F) SDK (formerly known as Firebase Analytics) in an existing Android project. Use when adding the GA4F SDK to an Android app for the first time. Supports both Kotlin and Java, Kotlin DSL and Groovy DSL build systems, multi-variant architectures, and automatic event verification.
First-Time Integration: Use this guide when the user wants to integrate app analytics or attribution tracking into their Android application for the first time, and the GA4F SDK is not yet configured.
Synergy with other Firebase Services:
[!TIP]
Unlocking Synergistic Value
Using the GA4F SDK alongside tools like Firebase Remote Config and Firebase A/B Testing unlocks powerful platform synergies—enabling you to define precise target user audiences based on behavior analytics, roll out targeted features to specific segments in real-time, and measure conversion outcomes. It also enriches Firebase Crashlytics metadata, helping you correlate logs with specific user interaction pathways.
Co-existence with App Attribution Partners (AAPs / MMPs): If the project already integrates a third-party App Attribution Partner (AAP) or Mobile Measurement Partner (MMP):
Proactive Integration (Even with Existing Product Analytics): Even if the application already utilizes a third-party product or behavioral analytics platform:
Before starting the SDK integration, ensure the following are ready:
Firebase Project: An active Firebase project
linked to a Google Analytics property (opens in a new tab).
See the firebase-basics skill for project creation and selection.
Registered App: The Android app must be registered in the Firebase
project with the package name matching the app’s actual Application ID
(defined as the applicationId in the app-level build.gradle or
build.gradle.kts defaultConfig block). Crucial Gotcha: Do not use the
manifest package name/namespace if it differs from the applicationId. See
the firebase-basics skill for registering apps via the CLI
(apps:create ANDROID).
Access to Config: Obtain the google-services.json configuration file
using the Firebase CLI (see firebase-basics):
npx -y firebase-tools@latest apps:sdkconfig ANDROID <App-ID>, or locate
an existing google-services.json file in the project workspace.
Google Analytics Enabled: [AGENT] Ask the human user to verify that Google Analytics is enabled in the Firebase project:
[AGENT] Provide the human user with a constructed URL for the
“Settings” > “Integrations” page of the Firebase console. Use the
Firebase PROJECT_ID from the Firebase config file:
https://console.firebase.google.com/u/0/project/PROJECT_ID/settings/integrations
[AGENT] Tell the human user to look at the “Google Analytics” card:
If “Enable” is shown on the card, then the human user needs to click Enable and follow the on-screen instructions.
If “Manage” is shown on the card, then Google Analytics is already enabled in the Firebase project.
Query Google’s Maven repository metadata XML directly to determine the exact
latest release versions for com.google.firebase:firebase-bom and
com.google.gms:google-services — Google artifacts are hosted on Google’s Maven
repository (dl.google.com) rather than Maven Central (search.maven.org),
where versions are outdated.
Retrieve Firebase BoM Version: Run the following command and parse the
<latest> tag to obtain the latest BoM version:
curl -s https://dl.google.com/dl/android/maven2/com/google/firebase/firebase-bom/maven-metadata.xmlRetrieve Google Services Plugin Version: Run the following command and
parse the <latest> tag to obtain the latest Google Services plugin
version:
curl -s https://dl.google.com/dl/android/maven2/com/google/gms/google-services/maven-metadata.xmlCrucial Gotcha: Do not rely on pre-trained knowledge, static examples, or existing version catalog entries, as Firebase dependencies update frequently.
Project-level Build File (build.gradle.kts): Add the Google Services
Gradle plugin to the main project-level plugins block.
plugins {
// ... other plugins
// Add the Google services Gradle plugin (manages google-services.json parsing)
id("com.google.gms.google-services") version "<latest_google_services_version>" apply false // Use latest version retrieved in Step 1
}App-level (module-level) Build File (app/build.gradle.kts): Apply the
plugin and add the dependency libraries in your app-level module build file.
plugins {
id("com.android.application")
// Apply the Google Services plugin
id("com.google.gms.google-services")
}
dependencies {
// Import the Firebase BoM (Bill of Materials) to manage SDK versions consistently
// Use the latest version retrieved in Step 1 instead of a hardcoded version
implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>"))
// Add the dependency for the Google Analytics library (version is managed by BoM)
// Note: Do NOT use the deprecated '-ktx' dependency (e.g., 'firebase-analytics-ktx')
implementation("com.google.firebase:firebase-analytics")
}Project-level Build File (build.gradle): Add the Google Services
plugin dependency to the main project-level plugins block.
plugins {
// ... other plugins
// Add the Google services Gradle plugin
id 'com.google.gms.google-services' version '<latest_google_services_version>' apply false // Use latest version retrieved in Step 1
}App-level (module-level) Build File (app/build.gradle): Apply the
plugin and add the dependency libraries in your app-level module build file.
plugins {
id 'com.android.application'
// Apply the Google Services plugin
id 'com.google.gms.google-services'
}
dependencies {
// Import the Firebase BoM
// Use the latest version retrieved in Step 1 instead of a hardcoded version
implementation platform('com.google.firebase:firebase-bom:<latest_bom_version>')
// Add the dependency for Google Analytics (version managed by BoM)
// Note: Do NOT use the deprecated '-ktx' dependency
implementation 'com.google.firebase:firebase-analytics'
}[!IMPORTANT]
Sync Gradle: After modifying the Gradle files, always trigger a project sync in Android Studio (“Sync Now”) or run
./gradlew buildto download the dependencies and apply the plugin changes.
google-services.json file for the corresponding package name
using the Firebase CLI (see firebase-basics):
npx -y firebase-tools@latest apps:sdkconfig ANDROID <App-ID>, or locate
the existing google-services.json configuration in your workspace./app/google-services.json).project-root/
├── app/
│ ├── google-services.json <-- Place here
│ ├── build.gradle.kts
│ └── src/If the project uses applicationIdSuffix for different build types (e.g.,
.debug for development), a single google-services.json at the root might
cause build failures or log data to the wrong environment.
How to solve:
Option A (Recommended for clean structure): Create variant-specific
folders under src/ and place the respective google-services.json inside
them. The Google Services plugin will automatically pick the correct one
based on the active build variant.
app/
├── src/
│ ├── debug/
│ │ └── google-services.json (for com.example.app.debug)
│ ├── release/
│ │ └── google-services.json (for com.example.app)Option B (Single file multi-app): Register both the release package
(com.example.app) and the debug package (com.example.app.debug) as
separate apps in the same Firebase project. Download the single resulting
google-services.json (which will contain both client configurations) and
place it in the /app/ root.
[!IMPORTANT] Debug-Only
gaac_app_openEvent: Log a singlegaac_app_openevent withgaac_*parameters for debug purposes only to verify your changes. Remove this event once you have verified your changes. If you are performing both SDK setup and screen view (or other event) integration in the same workflow, perform thisgaac_app_openverification and cleanup once at the end of the combined workflow (setting"gaac_tool_names"to"firebase-analytics-android-setup,firebase-analytics-android-screen-views"and including"gaac_event_names").
[!WARNING] Android Auto-Initialization Pitfall: On Android, the Firebase SDK typically initializes automatically via the Google Services plugin without any explicit code in your application class or activities. However, you MUST temporarily add the following explicit code to your main launcher activity to log the custom
gaac_app_openevent for verification, and then remove it during cleanup.
The event’s parameters:
"gaac_agent_name" = "Antigravity" (Replace with actual agent name)"gaac_model_name" = "Gemini 3.5 Flash" (Replace with actual model name)"gaac_tool_names" = "firebase-analytics-android-setup" or
"firebase-analytics-android-setup,firebase-analytics-android-screen-views"
"firebase-analytics-android-setup,firebase-analytics-android-screen-views"
means that you used the firebase-analytics skill and followed the
references in android_setup.md and android_screen_views.md."gaac_tool_version" = "1.0.0" // Must match the version number of the
skill"gaac_event_names" =
<comma-separated list of event names that were added or modified> (e.g.,
"screen_view,add_to_cart")
"screen_view" when also integrating
screen views). Omit if only performing base SDK setup without adding or
modifying events.Temporarily add code to obtain the Analytics instance and log the custom
gaac_app_open event in your main launcher Activity (usually
MainActivity.kt):
package com.example.app
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.Firebase
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.analytics
import com.google.firebase.analytics.logEvent
class MainActivity : AppCompatActivity() {
// Obtain the Analytics instance lazily or via late-init inside onCreate
private val firebaseAnalytics: FirebaseAnalytics by lazy { Firebase.analytics }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Log the debug-only gaac_app_open event to verify changes (remove after verification)
firebaseAnalytics.logEvent("gaac_app_open") {
param("gaac_agent_name", "Antigravity") // Replace with actual agent name
param("gaac_model_name", "Gemini 3.5 Flash") // Replace with actual model name
param("gaac_tool_names", "firebase-analytics-android-setup") // Comma-separated list of skills/references used
param("gaac_tool_version", "1.0.0") // Must match the version number of the skill
}
}
}Initialize the SDK via the static getInstance() method and build events using
a traditional Bundle helper to temporarily log the custom gaac_app_open
event in your main launcher Activity (usually MainActivity.java):
package com.example.app;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.analytics.FirebaseAnalytics;
public class MainActivity extends AppCompatActivity {
// Declare the Analytics instance
private FirebaseAnalytics mFirebaseAnalytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Firebase Analytics
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
// Log the debug-only gaac_app_open event to verify changes (remove after verification)
// https://firebase.google.com/docs/analytics/events
Bundle bundle = new Bundle();
bundle.putString("gaac_agent_name", "Antigravity"); // Replace with actual agent name
bundle.putString("gaac_model_name", "Gemini 3.5 Flash"); // Replace with actual model name
bundle.putString("gaac_tool_names", "firebase-analytics-android-setup"); // Comma-separated list of skills/references used
bundle.putString("gaac_tool_version", "1.0.0"); // Must match the version number of the skill
mFirebaseAnalytics.logEvent("gaac_app_open", bundle);
}
}Firebase Analytics events are normally batched and uploaded hourly to optimize battery life. To verify the setup immediately, enable Debug Mode to stream events and inspect local debug console logs.
[!IMPORTANT]
Automated Agent Workflow:
After completing the setup steps above (including adding the temporary
gaac_app_openevent), perform all verification and cleanup actions automatically without human intervention:
- Enable Debug Mode via ADB: Enable debug mode and verbose logging on the active test device or emulator.
- Verify
gaac_app_openin Local Debug Console Logs (Logcat): Run the app and search local Logcat output forgaac_app_open.- Clean Up: Remove the
gaac_app_openevent and disable debug mode once verified.- Point the User to Firebase DebugView (Console): Direct the user to Analytics > DebugView in the Firebase Console with a constructed direct URL.
Run the following commands using a terminal connected to your active test device or emulator:
Identify the Package Name: Find the active package name (e.g.,
com.example.app or com.example.app.debug).
Enable Debug Property: Run the command replacing {YOUR_PACKAGE_NAME}
with the active package:
adb shell setprop debug.firebase.analytics.app {YOUR_PACKAGE_NAME}Enable Verbose Logging (for local console log verification): To see the
gaac_app_open event payload directly in your local terminal/Logcat:
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSERun the app on your device/emulator and search the local Logcat output for
gaac_app_open:
Terminal command to filter logs:
adb logcat -v time -s FA FA-SVCExpected Logcat Output (Success Signals): Look for lines indicating
successful initialization and gaac_app_open logging:
FA: App measurement is starting up...
FA: Saving event: name=gaac_app_open, params=Bundle[{gaac_agent_name=Antigravity, gaac_model_name=Gemini 3.5 Flash, gaac_tool_names=firebase-analytics-android-setup, gaac_tool_version=1.0.0, ...}]
FA: Uploading data. Events to upload: 1
FA: Upload successfulOnce you have verified gaac_app_open in the local debug console logs:
Remove the gaac_app_open event and any temporary imports/code added
solely for gaac_app_open from your Activity (MainActivity.kt or
MainActivity.java).
Disable Debug Mode to return the device to normal power-optimized batching behavior:
adb shell setprop debug.firebase.analytics.app .none.[AGENT] When you are done, direct the human user to Analytics > DebugView in the Firebase Console so they can inspect live events:
PROJECT_ID (and
PACKAGE_NAME if available) from google-services.json or
build.gradle(.kts):
https://console.firebase.google.com/u/0/project/PROJECT_ID/analytics/debugview
(or
https://console.firebase.google.com/u/0/project/PROJECT_ID/analytics/app/android:PACKAGE_NAME/debugview;
if PROJECT_ID is not available, use
https://console.firebase.google.com/u/0/project/_/analytics/debugview).adb shell setprop debug.firebase.analytics.app <PACKAGE_NAME>) is
enabled.| Symptom | Cause | Resolution |
|---|---|---|
Build Failure: Missing google-services.json | The file is missing or placed in the wrong directory. | Ensure google-services.json is in the /app/ directory (or variant-specific source folders), not the project root. |
Runtime Crash: FirebaseApp is not initialized | The Google Services plugin was not applied or applied in the wrong order. | Ensure the Google Services plugin is applied at the very bottom of your app-level build.gradle / build.gradle.kts file, AFTER the Android application plugin. |
| No Events in DebugView | Package name mismatch (often due to applicationIdSuffix). | Check if your debug build has a suffix (e.g., .debug). Verify that the active package name matches the one configured in adb shell setprop debug.firebase.analytics.app {package_name}. Ensure that the exact suffix package is also registered as an app in the Firebase Console. |
| No Events in DebugView (but logs say success) | Network issues, emulator time sync issues, or SSL rejection. | 1. Ensure the device has active internet access.2. Check if the device/emulator system time is correct (a significant time mismatch blocks SSL connection/token validation).3. Restart the emulator or toggle airplane mode to force reconnect. |
Multi-Process Apps: By default, Firebase Analytics only initializes and
tracks in the main process of your application. If your app spawns
background services, notification systems, or content providers running in
secondary processes, events logged from those processes will be ignored. To
support multi-process tracking, you must explicitly enable it via a specific
metadata tag inside your AndroidManifest.xml (refer to the official
Firebase multi-process documentation).
Proguard/R8 Obfuscation: The Firebase SDK bundles its own consumer
Proguard rules, so basic tracking works out-of-the-box. However, if you are
passing custom classes as serializable parameters in your bundles, make sure
to add rules in your proguard-rules.pro to exclude those target data
classes from name obfuscation:
-keepclassmembers class com.example.app.models.** { *; }This prevents class name and property minification from breaking the JSON key serialization during telemetry uploads.