Android 如何覆盖清单中导出的标签?

Android How can i override exported tag in manifest?

我想将目标 sdk 从 30 升级到 31。但是我收到了那个错误:

Manifest merger failed : android:exported needs to be explicitly specified for . 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.

当我环顾清单合并时。我看到我在应用程序中使用的第三方库之一没有添加 android:exported 标签。

 <service android:name="com.****.MessagingService" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

所以这是我的问题。我如何将此服务覆盖为导出的错误,如下所示?

<service android:exported="false"
 android:name="com.****.MessagingService" >
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service> 

您可以替换整个节点

        <service
            tools:node="replace"
            android:name=".MessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

或者只是您需要替换的属性

        <service
            tools:replace="exported"
            android:name=".MessagingService"
            android:exported="false"/>

您可以找到有关此主题的更多信息here

Android Studio 将合并项目及其依赖项的所有清单文件。

上层清单始终可以优先并覆盖依赖项中的值。检查这个 paragraph in the documentation.

由于依赖项根本没有指定 exported 值,您可以像这样覆盖顶级清单文件中的每个服务:

<service android:exported="false"
 android:name="com.****.MessagingService" />

这将合并 com.****.MessagingService 的服务标签值并应用导出的值。