我已经将模拟器版本和Android SDK版本更新到Android S (Android 12)。更新后,我无法运行项目。我不能运行Hello, World!项目(空项目),但我可以建立Gradle以及,但我不能运行项目。我总是得到错误:

Manifest合并失败:针对Android 12及更高版本的应用程序失败 需要为android指定一个显式值:当 相应的组件定义了一个意图过滤器。看到 https://developer.android.com/guide/topics/manifest/activity-element#exported 获取详细信息。

我该怎么解决呢?

以下是截图:

使用Android 12 SDK时如何解决此问题?

这个问题是应用这个问题的解决方案之后的问题,和这个问题不一样。而且,这个问题比这个更古老。


当前回答

更新androidTestImplementation 'androidx.test.ext:junit:1.1.1'到最新版本,如下:

androidtestimplemimplementation ` androidx.test.ext:junit:1.1.3'从构建。Gradle应用级别。

其他回答

如果你在你的清单中没有找到一个没有“android: exported = false”标签的活动,那么它很可能是在你的依赖项中…为了精确定位,首先将“compileSdkVersion”降级为30,将“targetSdkVersion”降级为30,这样它就可以构建了。

android {
    compileSdkVersion("android-S")
    buildToolsVersion "30.0.3"

    defaultConfig {
        ...
        minSdkVersion 23
        targetSdkVersion("S")
        ...
}

之后,在主manifest.xml窗口中有一个带有“merged manifest”的选项卡。在那里,你可以检查哪些活动没有“android: exported = false”属性。

在我的案例中,这是因为第三方工具:

文件建立。Gradle (: app):

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
//and
debugImplementation "com.github.markzhai:blockcanary-android:1.5.0"
releaseImplementation "com.github.markzhai:blockcanary-no-op:1.5.0"

此外,对于服务,我必须添加属性:

<service
    android:name=".autofillservice.MyAutofillService"
    android:exported="true"
    android:permission="android.permission.BIND_AUTOFILL">

and

<service
    android:name="com.demo.myApp.my_access.MyAccessService"
    android:enabled="true"
    android:exported="true"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">

由于我的问题是在第三方依赖,它不会很快更新,我只是添加了一个<活动>声明与标志android:exported="true"和export ="false"需要覆盖初始声明的地方,也因为我需要这个依赖在调试中,我只添加了一个新的AndroidManifest.xml文件在src/ Debug:

leak_canary:

<?xml version="1.0" encoding="UTF-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application>
        <activity
            android:name="leakcanary.internal.activity.LeakActivity"
            android:exported="true"
            android:icon="@mipmap/leak_canary_icon"
            android:label="@string/leak_canary_display_activity_label"
            android:taskAffinity="com.squareup.leakcanary.${applicationId}"
            android:theme="@style/leak_canary_LeakCanary.Base">

            <intent-filter android:label="@string/leak_canary_import_hprof_file">

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="file" />
                <data android:scheme="content" />
                <data android:mimeType="*/*" />
                <data android:host="*" />

                <data android:pathPattern=".*\\.hprof" />
                <data android:pathPattern=".*\\..*\\.hprof" />
                <data android:pathPattern=".*\\..*\\..*\\.hprof" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\.hprof" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.hprof" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\.hprof" />
                <data android:pathPattern=".*\\..*\\..*\\..*\\..*\\..*\\..*\\.hprof" />
            </intent-filter>
        </activity>

        <activity
            android:name="leakcanary.internal.RequestStoragePermissionActivity"
            android:excludeFromRecents="true"
            android:exported="false"
            android:icon="@mipmap/leak_canary_icon"
            android:label="@string/leak_canary_storage_permission_activity_label"
            android:taskAffinity="com.squareup.leakcanary.${applicationId}"
            android:theme="@style/leak_canary_Theme.Transparent" />

        <receiver
            android:name="leakcanary.internal.NotificationReceiver"
            android:exported="false" />

    </application>
</manifest>

你不妨只是使用tools:node="merge"属性,并声明android:exported=true|false作为LeoFarage善意的建议。

你的问题可能被标记为重复,因为这篇文章:针对Android 12的Manifest合并失败,尽管你的文章是在一周前发布的。我现在看不到国旗了。

为了澄清另一个答案,请注意,android:exported应该为你的主活动设置为true,否则它将不会启动,尽管android Studio有鼓励的“启动成功”消息,因为没有其他应用程序,甚至android系统本身,可以启动它。

<activity
    android:name=".MainActivity"
    android:exported="true"

对于其他意图隐藏在合并清单中的活动,这通常会被设置为false。

针对Android 12的应用

将应用程序的targetSdkVersion更改为S(32或31)以启用新行为。

然后将Manifest中的android:exported=""属性指定为true或false,具体取决于Activity。

对于启动器活动,如splash或MainActivity,使用android:exported="true",对于其余的活动,使用android:exported="false"

例如:

<!-- It's **true** for the launcher Activity -->
<activity android:name=".SplashActivity"
          android:exported="true" />

<!-- It's **false** for the rest of the Activities -->
<activity android:name=".MainActivity"
          android:exported="false" />

对我来说,这两种解决方案都不奏效,即使我在Android Studio中反复检查“合并清单”。在编译项目后,错误就出现了,我无法识别生成问题的行。

解决方案:确保您的目标是项目中的最新库。我使用Unity, StartApp和Flurry Analytics。我忘记更新这些库,升级后,错误消失了。看起来这些库使用了来自 SDK 31之前。

如果您正在使用废弃的库,那么您应该考虑替换它们。

我还必须添加android:exported="true"到我在清单中声明的所有接收器。所以我有这样的东西:

<receiver android:name=".alarms.AlarmReScheduler"
          android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <action android:name="android.intent.action.PACKAGE_REPLACED" />

        <!-- For HTC devices -->
        <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

我不认为你应该把android:exported="true"添加到所有东西。你应该只在那些Broadcast接收器需要对Android OS可见时添加它。这段代码中的Intent过滤器意味着我想让Android OS唤醒我的Android应用程序并执行一个操作。

Android .intent.action. boot_completed就是一个很好的例子,因为Android OS会向安装在设备中的每个应用程序发送广播。所以从技术上讲,这意味着任何带有动作的intent过滤器的Broadcast接收器都应该声明android:exported="true"。