tools:targetApi 在清单文件中和 build.gradle 中的 target sdk 有什么区别?

huangapple go评论76阅读模式
英文:

what is the difference between tools:targetApi in the Manifest and target sdk in build.gradle?

问题

你是否需要将tools:targetApi="31">更改为tools:targetApi="33">

英文:

I developed an app in Android and I want to limit the Android versions allowed to use it. So, in the build.gradle file, I put:

android {
    namespace 'wamboo.example.videocompressor'
    compileSdk 33

    defaultConfig {
        applicationId "wamboo.example.videocompressor"
        minSdk 30
        targetSdk 33
        versionCode 2
        versionName "2.0"

but I also saw this in the AndroidManifest file:

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.VideoCompressor"
    tools:targetApi="31">

I was wondering if I need to change that tools:targetApi="31"> to tools:targetApi="33">?

答案1

得分: 1

tools:targetApi="x" 用于由 Lint 指定此元素(在您的情况下是应用程序)支持的 API 级别。

tools:targetApi 文档

这告诉工具,您相信此元素及其任何子元素仅在指定的 API 级别或更高级别上使用。这可以防止 Lint 在您指定的 minSdkVersion 上不可用的 API 级别上警告您,如果该元素或其属性不可用。

在您的情况下,application 具有 dataExtractionRules 属性,该属性在 API 31 中添加。由于您的 minSdk 是 30,如果没有 tools:targetApi,您将看到 Lint 警告。

回答您的问题,将其保留为 31 是可以的,因为它是application 元素中所有属性所需的最低版本。尽管将其设置为 33 也没有问题,因为它仅供 Lint 使用,不会影响应用程序的运行。

英文:

tools:targetApi="x" is used by lint to specify which API level is supported by this element (in your case application)

tools:targetApi documentation

> This tells the tools that you believe this element and any children are used only on the specified API level or higher. This stops lint from warning you if that element or its attributes are not available on the API level you specify as your minSdkVersion.

In your case, application has dataExtractionRules attribute, which was aded in API 31. Since your minSdk is 30, without tools:targetApi you will see a lint warning.

Answering your question, it's fine to leave it as 31, since it's the minimum version required by all attributes in the application element. Though it wouldn't harm to set it to 33 as well because it's only used by lint and won't affect the app in runtime.

huangapple
  • 本文由 发表于 2023年2月18日 22:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定