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

配置AdMob时Gradle脚本代码块位置报错及后续问题排查

AdMob Android配置中Gradle代码块位置问题排查与解决

初始配置与错误

我正在配置Google AdMob Android SDK,错误地在模块级build.gradle中添加了buildscript和allprojects代码块,同时在依赖中加入了AdMob SDK:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
}
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

plugins {
    id 'com.android.application'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.cmg.www.cmg"
        minSdk 19
        targetSdk 33
        versionCode 1022
        versionName "1.022"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    def billing_version = "6.0.1"
    implementation "com.android.billingclient:billing:$billing_version"

    /* AdMob SDK依赖 */
    implementation 'com.google.android.gms:play-services-ads:22.5.0'

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    /* ImmutableList依赖 */
    implementation 'com.google.guava:guava:31.1-android'
    implementation 'androidx.multidex:multidex:2.0.1'
}

编译时触发错误:

Could not compile build file 'C:\wamp64\www\cmg\app\build.gradle'.
> startup failed:
  build file 'C:\wamp64\www\cmg\app\build.gradle': 14: only buildscript {}, pluginManagement {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

调整位置后的新问题

按照建议将buildscript和allprojects移至项目级build.gradle,但原有项目级代码为:

plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}
  • 将buildscript放在plugins前无问题,但allprojects放在plugins前会提示不允许;
  • 放在plugins后触发错误:
    org.gradle.api.GradleScriptException: A problem occurred evaluating root project.
    
  • 移除allprojects后同步看似正常,但未验证广告功能;
  • 移除项目级plugins块后,修改为以下代码仍报错:
    buildscript {
        repositories {
            google()
            mavenCentral()
        }
    }
    allprojects {
        repositories {
            google()
            mavenCentral()
        }
    }
    
    错误信息:
    A problem occurred evaluating root project 'CMG'.
    > Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
    
    * Exception is:
    org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'CMG'.
        ......
    Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'
    

正确解决方案

1. 修正模块级build.gradle

移除buildscript和allprojects块,保留核心配置即可:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 33

    defaultConfig {
        applicationId "com.cmg.www.cmg"
        minSdk 19
        targetSdk 33
        versionCode 1022
        versionName "1.022"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    def billing_version = "6.0.1"
    implementation "com.android.billingclient:billing:$billing_version"

    /* AdMob SDK依赖保留 */
    implementation 'com.google.android.gms:play-services-ads:22.5.0'

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation 'com.google.guava:guava:31.1-android'
    implementation 'androidx.multidex:multidex:2.0.1'
}

2. 保留项目级build.gradle原有配置

无需添加buildscript和allprojects,保持原有代码:

plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}

3. 确保settings.gradle配置正确仓库

Gradle新版本推荐在settings.gradle中统一配置仓库,内容如下:

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "CMG"
include ':app'

这里的RepositoriesMode.FAIL_ON_PROJECT_REPOS就是导致之前错误的原因——它强制要求所有仓库配置必须在settings中,禁止在build.gradle中添加仓库。

4. 验证广告功能

完成上述配置后同步项目,即可正常编译,后续按AdMob文档完成初始化代码即可验证广告功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 11:35:21