当迁移到AndroidX时无法解决Gradle版本错误问题

7

当我尝试将自己的项目迁移到AndroidX时,一个错误对话框上出现了错误消息:

The gradle plugin version in your project build.gradle file needs to be set to at least com.android.tools.build:gradle:3.2.0 in order to migrate to AndroidX.

然后我在互联网上搜索了这个错误。找到了一个说法,它说必须向build.gradle中添加classpath 'com.android.tools.build:gradle:3.2.0'。因此,我按照以下方式将该行添加到build.gradle中:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

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

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:support-core-utils:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

然而,同样的错误仍然发生了,我是不是漏掉了什么?

1个回答

12

检查项目级别(而不是模块级别)的build.gradle文件。

并更改Android Gradle插件的版本:

buildscript {
    ...

    dependencies {
        ...
        classpath 'com.android.tools.build:gradle:3.4.1' // it should be more than 3.2.0
    }
}

2
那是正确的答案。这个网站只是太不清楚了,它从来没有告诉我要更改项目级别... 不过还是谢谢你! - Sunny Leung
这不是Gradle版本,而是Android Gradle插件的版本。 - Gabriele Mariotti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接