【问题标题】:How do I solve the error for the build gradle project?如何解决构建gradle项目的错误?
【发布时间】:2022-11-28 08:34:35
【问题描述】:

我正在为我的应用程序尝试 firebase,但是当我放置必要的脚本/插件并同步它时。 它给我的项目 gradle 文件一个错误:

Build file 'D:\UCLL\3de jaar\app_Comic\build.gradle' line: 6

Could not compile build file 'D:\UCLL\3de jaar\app_Comic\build.gradle'.
> startup failed:
  build file 'D:\UCLL\3de jaar\app_Comic\build.gradle': 6: all buildscript {} blocks must appear before any plugins {} blocks in the script

我的项目build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
}
buildscript {
    repositories {
        google()
        mavenCentral()  // Maven Central repository
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.13'
    }
}
allprojects {
    repositories {
        google()
        mavenCentral()  // Maven Central repository
    }
}

我的 build.gradle 应用:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    namespace 'com.example.mowmow'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.mowmow"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

dependencies {

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

    //picasso
    implementation 'com.squareup.picasso:picasso:2.8'

    //ToStringBuilder
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'

    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    //Firebase/google
    implementation 'com.google.firebase:firebase-analytics'
}

但是当我把插件放在中间时,它给了我另一个错误:

Build file 'D:\UCLL\3de jaar\app_Comic\build.gradle' line: 18

A problem occurred evaluating root project 'app_Comic'.
> Build was configured to prefer settings repositories over project repositories but repository 'Google' was added by build file 'build.gradle'

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'app_Comic'. <87 internal lines>
    at org.jetbrains.plugins.gradle.model.ProjectImportAction.execute(ProjectImportAction.java:116)
    at org.jetbrains.plugins.gradle.model.ProjectImportAction.execute(ProjectImportAction.java:42) <82 internal lines>
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' <11 internal lines>
    at jdk.internal.reflect.GeneratedMethodAccessor153.invoke(Unknown Source) <6 internal lines>
    at build_63utr2332008u9gjv762bsg3t$_run_closure1$_closure2.doCall(D:\UCLL\3de jaar\app_Comic\build.gradle:18) <18 internal lines>
    at build_63utr2332008u9gjv762bsg3t$_run_closure1.doCall(D:\UCLL\3de jaar\app_Comic\build.gradle:17)<47 internal lines>
    at build_63utr2332008u9gjv762bsg3t.run(D:\UCLL\3de jaar\app_Comic\build.gradle:16)<1 internal lines>
    ... 170 more

脚本:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()  // Maven Central repository
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.13'
    }
}
plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
}
allprojects {
    repositories {
        google()
        mavenCentral()  // Maven Central repository
    }
}

有没有人有想法,将不胜感激! 提前致谢!

【问题讨论】:

  • 请不要发布您的代码的屏幕截图或其他文本内容(例如错误消息)。而是发布实际文本,并使用 Stack Overflow 的格式化工具对其进行标记。另见:Why not upload images of code/errors when asking a question?
  • 请添加错误文本(堆栈跟踪)
  • 对不起!我马上去做!
  • 你知道我做错了什么吗?

标签: java android firebase build.gradle


【解决方案1】:

删除顶层文件中的所有 buildscript 块并添加 plugins google-services 插件:

plugins {
    id 'com.android.application' version '7.3.0' apply false
    id 'com.android.library' version '7.3.0' apply false
    
    id 'com.google.gms.google-services' version '4.3.13' apply false
}

【讨论】:

    猜你喜欢
    • 2016-11-02
    • 2021-10-10
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多