AdMob的Gradle

0mkxixxg  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(110)

[Edit下面作为来自@fillobotto的答案几乎解决了我的问题]
我在google here的文档页面。
在StackOverflow上的各种帖子中,它说我需要把我的buildscriptallprojects放在plugins之前。所以我把我的gradle更新为以下内容(新的位是buildscriptallprojectsimplementation 'com.google.android.gms:play-services-ads:22.5.0'):

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"

    /* This is for adverts */
    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'
    /* This for 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


这些新的部门应该去哪里?

编辑由于@fillobotto回答:

我已经从模块级gradle中删除了buildscriptsallprojects
相反,由于@fillobotto回答,我已经把它们放在项目级别build.gradle
然而,在我的项目级gradle中,

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


所以我需要buildscriptsallprojects来解决这个问题。
我可以把buildscript部分放在plugins之前,没有问题。
但是,如果我把allprojects部分放在插件部分之前,

buildscript {
    repositories {
        google()
        mavenCentral()
    }
}
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
plugins {
    id   com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}


它说我不能把allprojects在插件之前。
如果我把allprojects之后,即:

buildscript {
    repositories {
        google()
        mavenCentral()
    }
}
plugins {
    id   com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}


我得到一个错误,

org.gradle.api.GradleScriptException: A problem occurred evaluating root project.


如果我离开所有项目部分完全,即

buildscript {
    repositories {
        google()
        mavenCentral()
    }
}
plugins {
    id   com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}


它似乎建立和同步罚款,但我还没有检查广告是否正常工作。我会尝试看看明天。
编辑二:
根据进一步的建议,我已经从我的项目gradle中删除了plugins部分,所以projectgradle是:

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


然而,当我同步时,它仍然不喜欢allprojects部分中的google(),并给出错误:

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'

yi0zb3m4

yi0zb3m41#

正如你链接的指南第一步所述,编辑需要在项目级build.gradle内部完成。不过你粘贴的是app模块build.script
正确的文件位于项目的根目录中,例如:

C:\user\documents\myproject\build.gradle

字符串
你应该编辑这个文件,使它看起来像:

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

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


此外,您应该删除从应用程序模块build.gradle添加的代码,但implementation行除外,该行仍应保留在应用程序模块的dependencies

相关问题