我用的是Android Studio 4.2.2。我创建了一个新项目,还没有添加任何东西到启动器代码,每当我点击构建或运行,我得到这个错误:

已安装的构建工具版本31.0.0已损坏。使用SDK管理器删除并重新安装。

我看了其他帖子的建议,但这些解决方案都不起作用。以下是我的尝试:

SDK Manager → SDK Tools → check "Show package details", uncheck 31.0.0, and click "Apply" → Uninstall 31.0.0 → check 31.0.0 and reinstall it In SDK Manager, deselect 31.0.0 and try installing an older version (e.g., I've tried 30.0.3) and update "buildToolsVersion" in build.gradle to the installed version Went to Project Structure → Properties and verified that 31.0.0 is selected for "Build Tools Version" and "Compiled SDK Version" Manually remove the stuff in the build-tools folder; i.e., rm -rf /path/to/android/sdk/build-tools/31.0.0 (it doesn't end in "-rc" like some other posts have described) Restart Android Studio Reinstall Android Studio

我是一个Android新手,只是想设置一个Hello, World!项目,真的不应该这么难。


当前回答

将gradle升级到7.0.2可能会有所帮助

其他回答

这个问题有多种解决方案,但有一个解决方案应该在逻辑上解决它,即卸载构建工具31.0.0版本并重新安装。这个解决方案并不管用,Android Studio团队可能需要研究一下。

目前看来有效的方法是: 解决方案1(降低项目中使用的构建工具版本)

检查项目代码面板,然后转到Gradle Scripts - > build。gradle文件,

改变3个位置:compileSdkVersion, buildToolsVersion, targetSdkVersion从31到30 您将注意到在修改的行上出现了一个灯泡,单击并选择同步[到版本30]。Android Studio将自动下载BuildTool V30并更改项目设置。

解决方案2(从以前版本的构建工具中获取丢失的dx.bat和dx.jar文件) Build-tool 31.0.0在path-to-sdk\build-tools\31.0.0\ DX .bat和 Build-tool 31.0.0在path-to-sdk\build-tools\31.0.0\lib\ DX .jar中丢失DX

您可以尝试从以前版本的构建工具中复制它。我从30.0.3版本开始,它对我有用。

更改这些设置后,它似乎可以正常工作。我从SDK管理器下载了SDK 30版本。

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.anurag.myapplication"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

我卸载并安装31.0.0包,它仍然显示错误。

我使用了RC2构建工具,它在没有降级到SDK版本30的情况下编译和运行良好。

在app/build.gradle文件中:

buildToolsVersion "31.0.0-rc2"

更改您的Gradle构建从:

android {
    compileSdkVersion 31
    buildToolsVersion '31.0.0'

    defaultConfig {
        applicationId "com.example.eg"
        minSdkVersion 23
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

to:

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.eg"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

这是可行的。

进入Android Studio设置→Android SDK→SDK工具→。在列表中选中“谷歌Play licensing”和“Apply”。

它会下载一个文件,然后同步你的项目,这就完成了。