升级Firebase依赖后Gradle构建报错:Content is not allowed in prolog
Hey there, let's work through this frustrating build error you're hitting after updating your Firebase dependencies and the Google Services plugin. I've looked over your configs and logs, and here are the key fixes to try, ordered by how likely they are to resolve the issue:
1. Fix the Syntax Typo in Your App-level Gradle Script
First up, I spotted a small but critical typo in your build.gradle(Module:app) file. The last line is missing a closing single quote, which can throw off Gradle's parsing entirely:
// Wrong (missing closing quote) apply plugin: 'com.google.gms.google-service // Correct version apply plugin: 'com.google.gms.google-services'
2. Inspect Your google-services.json File
The "Content is not allowed in prolog" error almost always points to a malformed JSON/XML file, and your Firebase google-services.json is the prime suspect here. Check for these common issues:
- Leading whitespace or hidden characters: Open the file in a plain text editor (like VS Code or Notepad++) and make sure the first character is
{—no extra spaces, newlines, or hidden byte order marks at the start. - Corrupted download: If the file was saved incorrectly from Firebase Console, re-download a fresh copy and replace the one in your
app/directory. - Incorrect placement: Ensure the file is directly in the
app/folder (same level as your app'sbuild.gradle), not nested in a subfolder.
3. Upgrade the Google Services Plugin Version
Your current plugin version (3.3.1) is quite outdated and likely incompatible with the newer Firebase dependencies you're using (15.x series). Update it to a version that matches your Firebase library versions. For 15.x Firebase dependencies, a compatible plugin version is 4.3.10:
In your project-level build.gradle, change this line:
classpath 'com.google.gms:google-services:3.3.1' // Replace with classpath 'com.google.gms:google-services:4.3.10'
4. Clean and Rebuild Your Project
After making the above changes, clear any cached bad data with a full clean:
- Go to
Build > Clean Projectin Android Studio - Then run
Build > Rebuild Project - If the error persists, delete the
.gradlefolders (both in your project root and theapp/module), then clickFile > Sync Project with Gradle Filesto start fresh.
5. Check Other XML Config Files
Occasionally, malformed XML files in your project (like AndroidManifest.xml, strings.xml, or layout files) can trigger this error too. Double-check these files to ensure they have valid XML syntax, no leading spaces, and aren't corrupted.
内容的提问来源于stack exchange,提问作者Dhruv Verma

