buildToolsVersion 和compileSdkVersion 已被弃用

Mar*_*ler 97 android android-gradle-plugin deprecation-warning

我刚刚在库模块中注意到,该模块compileSdkVersion最近buildToolsVersion已被弃用。这是它抱怨的两种配置(删除文本)。目前这仅影响com.android.library,而不影响com.android.application

plugins {
    id "com.android.library"
}
android {
    defaultConfig {
        compileSdkVersion 31
        buildToolsVersion "31.0.0"
    }
}
Run Code Online (Sandbox Code Playgroud)

截图:build.gradle

单击时,人们可能会注意到它com.android.build.gradle.AndroidConfig已被弃用BaseExtension(无论这对于匹配意味着什么build.gradle):

截图:蕨花


显然compileSdkVersion现在被称为compileSdk(这部分似乎有效)......并且buildToolsVersion必须被定义为属性。但是在构建时(不是我没有尝试过),属性buildToolsVerson = "31.0.0"是未知的:

android {
    defaultConfig {
        compileSdk 31
        buildToolsVersion = "31.0.0"
    }
}
Run Code Online (Sandbox Code Playgroud)

如何让它在没有弃用警告的情况下构建?

Mor*_*ori 73

如果您看到此错误,则“compileSdkVersion”已被弃用 对于新的 SDK 33:

apply plugin: 'com.android.application'

android {
   // compileSdkVersion 33
    defaultConfig {
        compileSdk 33
    }
...}
Run Code Online (Sandbox Code Playgroud)

  • @bic55,您好,您可以安全地删除 `buildToolsVersion = '30.0.3'`,因为根据文档:_如果您使用 Gradle 3.0.0 或更高版本的 Android 插件,您的项目会自动使用默认版本的构建工具该插件指定._ (10认同)
  • 这消除了我的警告。但是,我不明白背后的逻辑:对于“minSdkVersion”和“targetSdkVersion”,“Version”后缀是可以的(对我来说非常有意义)。但是在“compileSdkVersion”中,“Version”后缀是一个问题。Google 产品负责人吸毒了吗? (7认同)
  • @Amc 感谢您提及文档。为了让其他人也能阅读它,提供您喜欢的链接通常会很有帮助。我相信您在这里找到了它 https://developer.android.com/build/releases/past-releases/agp-3-0-0-release-notes 但措辞更改为:`Build Tools 26.0.2 或更高版本。通过此更新,您不再需要指定构建工具的版本 - 插件默认使用所需的最低版本。因此,您现在可以删除 android.buildToolsVersion 属性。 (2认同)

Mar*_*ler 31

它的构建方式与此类似(需要更改库模块):

plugins {
    id "com.android.application"
}
android {
    defaultConfig {
        compileSdkVersion 31
    }
}

plugins {
    id "com.android.library"
}
android {
    defaultConfig {
        // compileSdkVersion 31
        compileSdk 31
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用 Gradle 3.0.0 或更高版本的 Android 插件,则不再需要指定 **buildToolsVersion**,因为您的项目会自动使用插件指定的构建工具的默认版本。 (13认同)

Her*_*uez 20

compileSdk 33
buildToolsVersion = '33.0.1'
Run Code Online (Sandbox Code Playgroud)

  • 我已将“compileSDKversion”更改为“compile SDK”,但 buildToolsVersion = '30.0.3' 仍然不推荐使用 (3认同)

Chr*_*ris 9

如果您使用的是 kts gradle 文件 ( build.gradle.kts)

plugins {
    id("com.android.library")
}
android {
    defaultConfig {
        // compileSdkVersion 31
        compileSdk = 31
    }
}
Run Code Online (Sandbox Code Playgroud)


Wil*_*Guy 6

apply plugin: 'com.android.application'

android {
    defaultConfig {
        buildToolsVersion = '30.0.3'
        compileSdk = 31
    }
}
Run Code Online (Sandbox Code Playgroud)


Fra*_*zoa 6

buildToolsVersion可以将其删除,因为不再需要它,并且compileSdkVersion 应该compileSdk根据 Google Android 自己的文档将其替换为 plain。

https://developer.android.com/reference/tools/gradle-api/8.2/com/android/build/api/dsl/CommonExtension#compileSdkVersion(kotlin.Int)

该函数已被弃用。被compileSdk取代