【问题标题】:Manifest merger failed targeting Android 12针对 Android 12 的清单合并失败
【发布时间】:2021-08-11 17:19:48
【问题描述】:

使用 Android Studio 4.2.1,在我的 build.gradle 文件中将 sdk 目标更改为 Android 12 后,我收到 Manifest merger failed with multiple errors, see logs 错误。

Merged Manifest标签显示的错误如下:

Merging Errors: 
Error: 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. My_App.app main manifest (this file) 
Error: 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. My_App.app main manifest (this file) 
Error: 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. My_App.app main manifest (this file) 

但是android:exported 标签已经应用在我的 AndroidManifest.xml 文件中。我只有一项活动。没有服务或广播接收器。见下文:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.mydomain.myapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:name="com.mydomain.myapp.MyApplication"
        android:allowBackup="false"
        tools:replace="allowBackup"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.mydomain.myapp.ui.MainActivity"
            android:exported="true">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

我的 build.gradle(:app) 文件:

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

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

知道如何解决这个问题吗?

【问题讨论】:

  • 您确定“合并清单”选项卡中显示的清单不包含其他组件吗?图书馆可以贡献活动、服务和接收者。
  • @CommonsWare 这个问题似乎确实是由第三方库缺少android:exported 标签引起的。 Merged Manifest 选项卡显示为空(“Nothing to show”),因为合并失败,所以我不得不使用低于 Android 12 的目标进行重建以进行调查。谢谢!
  • 这很有趣,感觉就像 Studio 中的错误/限制。即使它不能提供普通合并清单选项卡的所有功能,它至少应该显示合并清单包含的内容,仅适用于像这样的场景。
  • @CommonsWare 同意。如果右侧面板中的错误可以指出是哪个组件导致了错误,那就太好了。为了增加混淆,在右侧面板的顶部,它显示:“其他清单文件(包含在合并中,但没有贡献任何元素)”我将其解释为“其他清单文件不是错误的来源.
  • @CommonsWare 项目所需但未通过指定android:exported 标志进行更新以针对 Android 12 进行测试的第三方库呢?我正在开发一个具有很多依赖项的大型框架,并希望针对 Android 12 进行测试。我知道一些库不会很快更新清单。我们是否应该查看失败的清单合并并明确覆盖所有尚未设置其exported 标志的意图?

标签: android android-studio android-manifest android-12


【解决方案1】:

此问题是由 3 个活动在 androidx.test:core 库版本 1.3.0 中缺少 android:exported 属性引起的。升级到版本 1.4.0-beta01 解决了这个问题。

如果您在针对 Android 12 后遇到错误,最简单的调试方法是:

  • 降级到之前的 sdk 版本
  • 重建项目
  • 构建成功后,打开项目的AndroidManifest.xml
  • 在窗口底部,单击Merged Manifest 选项卡
  • 查找任何包含&lt;intent-filter&gt; 标记且缺少android:exported 属性的&lt;activity&gt;

如果您想确保这些活动是问题所在,请将它们直接添加到项目的 AndroidManifest.xml 文件中,并添加缺少的 android:exported 属性,然后尝试重新构建项目。

因此,如果 &lt;activity android:name="com.domain.ProblemActivity"&gt; 缺少 android:exported 属性,请将其添加到您的 AndroidManifest.xml 文件中,如下所示:

<activity 
 android:name="com.domain.ProblemActivity"
 android:exported="true" >

针对 Android 12 重新构建,如果它有效,那么你发现了这个错误!

感谢@MikePenz 为我指明了正确的方向。

【讨论】:

  • 感谢您的提示。我能够找出缺少导出属性的活动。这确实是一个来自第三方库的活动,它被自动添加到清单中。
  • 这些步骤肯定有帮助。我忘记了使用导航组件中的&lt;nav-graph&gt; 也会生成&lt;intent-filter&gt; 标记。您还需要将导出的属性添加到这些活动中。
  • 当我编辑清单的第三方库以添加 export 时,它不会保存文件,知道为什么吗?我是否需要手动将所有活动包含到我的清单中?
  • 在我的项目中不起作用。我没有没有导出属性的意图过滤器的活动,但我仍然有这个编译错误!如何找到没有导出属性的活动的确切位置?
  • 我能够通过运行gradlew processDebugAndroidTestManifest --debug 并向上滚动到显示“面向 Android 12 及更高版本的应用程序...”文本的位置来找出导致项目问题的原因。在我的情况下,它上面的 INFO 行显示需要更新到 3.4.0 的合并浓缩咖啡。另一个需要更新的是 WorkManager。
【解决方案2】:

如果您的应用面向 Android 12 或更高版本并包含使用 Intent 过滤器的活动、服务或广播接收器,则您必须为这些应用组件显式声明 android:exported 属性。为了解决这个问题,我们需要遵循以下步骤:

  1. 我们需要在主文件夹中找到 AndroidManifest.xml。

android&gt;app&gt;src&gt;main&gt;AndroidManifest.xml

  1. 我们必须添加android:exported="" 并在这些引号内设置一个布尔值。现在您可能会问,我什么时候需要将android:exported="true"android:exported="false" 添加到使用意图过滤器的活动、服务或广播接收器中。 如果应用组件包含 LAUNCHER 类别,请将 android:exported 设置为 true。在大多数其他情况下,将 android:exported 设置为 false。

  2. 这是一个示例,说明它在 AndroidManifest.xml 中的外观

<service android:name="com.example.app.backgroundService"
         android:exported="false">
    <intent-filter>
        <action android:name="com.example.app.START_BACKGROUND" />
    </intent-filter>
</service>

您可以通过以下链接查看有关此主题的更多信息:

Safer component exporting for Android 12

【讨论】:

    【解决方案3】:

    我遇到了这个问题,通过以下方式找到它:

    • 如果您的 AndroidManifest 文件中有任何活动、服务、接收者或提供者没有 exported 属性,则在该活动、服务、接收者中添加以下属性, 或提供者

    android:exported="false or true"

    【讨论】:

      【解决方案4】:

      别忘了把它也放入服务标签中

          <service
              android:name=".service.MyIME"
              android:exported="true"
              android:permission="android.permission.BIND_INPUT_METHOD">
              <meta-data
                  android:name="android.view.im"
                  android:resource="@xml/method" />
      
              <intent-filter>
                  <action android:name="android.view.InputMethod" />
              </intent-filter>
          </service>
      

      【讨论】:

        【解决方案5】:

        清理和重建项目对我有用

        【讨论】:

          猜你喜欢
          • 2023-01-04
          • 2021-07-28
          • 2022-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多