ERROR: Attribute application@name is too present

ERROR: Attribute application@name is too present

您好,我正在开发 2 个模块 A 和 B,B 依赖于 A。

最后两个有一个应用程序class,用于静态获取上下文。

并且在两个模块中有它们的清单。

模块 A 清单:

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

    <application android:name=".AA"/>

</manifest>

模块 B 清单:

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

    <application
        android:name=".BApp">
    </application>

</manifest>

当我尝试执行 Junit 时,出现以下错误:

Manifest merger failed : Attribute application@name value=(apps.b.BApp) from [__tested_artifact__::blibrary] AndroidManifest.xml:14:18-72
    is also present at [:alibrary] AndroidManifest.xml:11:18-78 value=(apps.com.a.AApplication).
    Suggestion: add 'tools:replace="android:name"' to <application> element at manifestMerger2718201306699938665.xml:7:5-9:19 to override.

关于如何解决这个问题的任何想法,我会把 'tools:replace="android:name"' 是正确的,还是可能是错误的?

是的,您可以使用 tools:replace="android:name" 来解决这个问题

根据docs

Your APK file can contain just one AndroidManifest.xml file, but your Android Studio project may contain several—provided by the main source set, build variants, and imported libraries. So when building your app, the Gradle build merges all manifest files into a single manifest file that's packaged into your APK.

The manifest merger tool combines all XML elements from each file by following some merge heuristics and by obeying merge preferences that you have defined with special XML attributes. This page describes how manifest merging works and how you can apply merge preferences to resolve merge conflicts.


tools:replace="attr, ..."

Replace the specified attributes in the lower-priority manifest with those from this manifest. In other words, always keep the higher-priority manifest's values.

在AndroidMainifest.xml 添加 2 行

android:name=".Application"
tools:replace="android:name"

我通过添加

解决了这个问题
tools:replace="name"

清单文件中我的应用程序名称下方