首页 > 解决方案 > 为什么当 targetSdkVersion 为 31 时,我收到“清单合并失败:...需要为 `android:exported` 指定显式值”?

问题描述

我试图在我的物理设备上运行一个 HelloWorld 项目,当我单击一个具有空活动的新项目时,Android Studio 创建该项目,但是当我运行该项目时,我收到 Manifest 合并失败,仅当 compileSdkVersion 为 31

build.gradle(Module) 包含:

android {
    compileSdkVersion 31
    buildToolsVersion "31.0.0"

    defaultConfig {
        applicationId "com.example.helloworld"
        minSdkVersion 16
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

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

因此,当我运行项目时,我收到此错误:

Manifest merger failed: Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

问题是:为什么我在build.gradle(Module)中使用SdkVersion 29时没有出现这个错误,如下?

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.helloworld"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

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

最后,这让我产生了疑问。如果新项目是自动创建以使用最新的 SDK 版本编译的,android:exported为什么它没有自动插入到 Manifest 中?

标签: androidandroid-studiobuild.gradleandroid-manifest

解决方案


推荐阅读