当我尝试同步我的 Gradle 文件时出现错误

Posted

技术标签:

【中文标题】当我尝试同步我的 Gradle 文件时出现错误【英文标题】:When I try to sync my Gradle files I get an error 【发布时间】:2020-03-19 21:11:06 【问题描述】:

我目前正在构建一个 Flutter 应用程序,到目前为止几乎一切正常,直到我开始收到此错误。我在整个互联网上搜索了一个解决方案,但我没有找到任何可以为我解决它的东西。

ERROR: Gradle DSL method not found: 'classpath()'
Possible causes:
The project 'android' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 3.5.2 and sync project

The project 'android' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file

The build file may be missing a Gradle plugin.
Apply Gradle plugin

我无法按“将插件升级到版本 3.5.2 并同步项目”,因为它失败了。

当我尝试构建我的应用程序时出现此错误

Launching lib\main.dart on SM G973F in debug mode...
Initializing gradle...
Resolving dependencies...
* Error running Gradle:
ProcessException: Process "C:\Users\tbsvst18tedbom\AndroidStudioProjects\tab_truth_true\android\gradlew.bat" exited abnormally:

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\tbsvst18tedbom\AndroidStudioProjects\tab_truth_true\android\build.gradle' line: 15

* What went wrong:
A problem occurred evaluating root project 'android'.
> Could not find method classpath() for arguments [com.android.tools.build:gradle:3.5.2] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 6s
  Command: C:\Users\tbsvst18tedbom\AndroidStudioProjects\tab_truth_true\android\gradlew.bat app:properties

Finished with error: Please review your Gradle project setup in the android/ folder.

这是项目杠杆 build.gradle 文件:


buildscript 
    ext.kotlin_version = '1.3.50'
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:3.5.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.3'
    

dependencies 
    classpath 'com.android.tools.build:gradle:3.5.2'




allprojects 
    repositories 
        google()
        jcenter()
    


rootProject.buildDir = '../build'
subprojects 
    project.buildDir = "$rootProject.buildDir/$project.name"

subprojects 
    project.evaluationDependsOn(':app')


task clean(type: Delete) 
    delete rootProject.buildDir


这是应用级别的 build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) 
    localPropertiesFile.withReader('UTF-8')  reader ->
        localProperties.load(reader)
    


def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) 
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")


def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) 
    flutterVersionCode = '1.0.0'


def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) 
    flutterVersionName = '1.0'



android 
    compileSdkVersion 29

    sourceSets 
        main.java.srcDirs += 'src/main/kotlin'
    

    lintOptions 
        disable 'InvalidPackage'
    

    defaultConfig 
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.tab.tab_truth_true"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    

    buildTypes 
        release 
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        
    


flutter 
    source '../..'


dependencies 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.firebase:firebase-ads:18.3.0'


apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

奇怪的是我没有任何 Gradle 设置,见图片 LINK this is my gradle settings, as you see, all I get is this

我曾尝试迁移到 Android X,但没有成功。我只是说“没有找到用途”

【问题讨论】:

【参考方案1】:

在项目级别build.gradle有一个重复的类路径:

dependencies 
    classpath 'com.android.tools.build:gradle:3.5.2'

请删除buildscript 块之外的第二个匹配项并尝试重新构建。

【讨论】:

以上是关于当我尝试同步我的 Gradle 文件时出现错误的主要内容,如果未能解决你的问题,请参考以下文章

当我尝试使用新的 Gradle 7 版本目录时出现未知属性错误

尝试生成签名 Apk 时出现 Gradle 构建错误

为啥我在构建项目时出现 Flutter gradle 错误?

将 libgdx 项目导入 Eclipse 时出现 Gradle 错误

当我尝试在 Android 11 中读取文件时出现电容器文件系统错误

找不到 Gradle DSL 方法:'flavorGroups()'