我用的是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!项目,真的不应该这么难。


当前回答

我的Android Studio Arctic Fox 2020.3.1补丁2 与 下载了Android SDK中的Android 12和Android SDK Build-Tools 31

使用buildToolsVersion "31.0.0"

构建。gradle(项目)

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

构建。gradle(模块)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "com.example.android12"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

gradle-wrapper.properties

#Fri Sep 10 13:52:36 ICT 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

其他回答

我的Android Studio Arctic Fox 2020.3.1补丁2 与 下载了Android SDK中的Android 12和Android SDK Build-Tools 31

使用buildToolsVersion "31.0.0"

构建。gradle(项目)

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

构建。gradle(模块)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "com.example.android12"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

gradle-wrapper.properties

#Fri Sep 10 13:52:36 ICT 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

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

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

在app/build.gradle文件中:

buildToolsVersion "31.0.0-rc2"

对于那些正在使用Azure DevOps并需要30.0.3版本的人来说,你需要卸载31.0.0版本

我将此添加到我的管道中,以使我的构建再次工作。

  - bash: |
      $ANDROID_SDK_ROOT/tools/bin/sdkmanager --uninstall 'build-tools;31.0.0'

环境变量$ANDROID_SDK_ROOT在预设变量中

这是一个与Android studio和SDK 31冲突的问题。

转换您的构建。Gradle文件是这样的:

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

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

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