You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加com.google.gms.google-services插件后Gradle同步失败求助

解决Gradle Sync错误:For input string: " 12"(Firebase集成问题)

嘿,我帮你分析下这个问题——你遇到的Gradle同步错误,核心原因是项目级和应用级(app)build.gradle的内容完全搞混了,这直接导致Google Services插件解析配置时出错。下面是一步步的修复方案:

第一步:修正两个build.gradle的结构

你现在把项目级的配置(比如buildscript、allprojects块)写到了app模块的build.gradle里,而把app模块的配置(比如android、dependencies块)写到了项目级的build.gradle里,这是完全错误的结构,必须调整:

正确的项目级build.gradle

buildscript {
    ext.objectboxVersion = '1.5.0'
    repositories {
        jcenter()
        google()
        mavenCentral()
        maven { url "http://objectbox.net/beta-repo/" }
        maven { url 'https://maven.google.com' }
        maven { url 'https://jitpack.io' }
        maven { url "https://raw.githubusercontent.com/smilefam/SendBird-SDK-Android/master/" }
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
        classpath 'io.fabric.tools:gradle:1.+'
        // 升级Google Services插件到更稳定的版本,适配你的Gradle版本
        classpath 'com.google.gms:google-services:4.3.10'
        // OneSignal插件要在这里引入,不要在app模块重复加
        classpath 'com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
        mavenCentral()
        maven { url "http://objectbox.net/beta-repo/" }
        maven { url 'https://maven.google.com' }
        maven { url 'https://jitpack.io' }
        maven { url "https://raw.githubusercontent.com/smilefam/SendBird-SDK-Android/master/" }
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

正确的应用级(app)build.gradle

apply plugin: 'com.android.application'
apply plugin: 'io.objectbox'
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        applicationId 'xxx.xxxx.xxxxx'
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 74
        versionName "1.0.5"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
        manifestPlaceholders = [
                onesignal_app_id : "xxxxx-xxxxxx-xxx",
                onesignal_google_project_number: "REMOTE"
        ]
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ['objectbox.debug': 'true']
            }
        }
    }

    dataBinding {
        enabled = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    
    // 升级Firebase依赖到兼容版本,所有Firebase组件版本要统一
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-messaging:17.6.0'
    
    // 其他依赖整理,去掉重复的compile(统一用implementation)
    implementation 'com.facebook.android:facebook-login:[4,5)'
    implementation 'com.facebook.android:facebook-share:[4,5)'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.orhanobut:logger:2.2.0'
    implementation 'joda-time:joda-time:2.9.9'
    implementation('com.afollestad:bridge:5.1.2') {
        exclude group: 'org.json', module: 'json'
    }
    implementation 'com.github.bumptech.glide:glide:4.7.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
    implementation 'jp.wasabeef:blurry:2.1.1'
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation('com.redmadrobot:inputmask:2.3.0') {
        exclude group: 'org.jetbrains', module: 'annotations'
    }
    implementation 'se.emilsjolander:stickylistheaders:2.7.0'
    implementation 'com.daimajia.easing:library:2.0@aar'
    implementation 'com.daimajia.androidanimations:library:2.3@aar'
    implementation 'com.github.javadev:underscore:1.29'
    implementation 'com.onesignal:OneSignal:[3.7.1, 3.99.99]'
    implementation 'com.mixpanel.android:mixpanel-android:5.+'
    implementation 'com.github.thomper:sweet-alert-dialog:v1.4.0'
    implementation 'com.github.faranjit:currency-edittext:1.0.1'
    implementation 'org.apache.commons:commons-lang3:3.7'
    implementation 'com.google.guava:guava:24.1-android'
    implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
        transitive = true
    }
    implementation('com.github.worker8:tourguide:1.0.19-SNAPSHOT@aar') {
        transitive = true
    }
}

// 这个插件必须放在app模块build.gradle的最后一行
apply plugin: 'com.google.gms.google-services'

第二步:清理并重新同步Gradle

完成上述修改后,按以下步骤操作:

  • 点击Android Studio工具栏的File -> Invalidate Caches / Restart,选择Invalidate and Restart
  • 等待项目重新构建,同步Gradle

为什么会出现这个错误?

For input string: " 12"这类错误,通常是Google Services插件解析配置时,读到了格式错误的版本字符串(比如带空格的版本号)。而你之前的build.gradle结构混乱,导致插件错误解析了不属于它的配置内容,从而抛出异常。

内容的提问来源于stack exchange,提问作者Henrique Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:32:28