简体   繁体   English

如何使用新的清单合并(Android Studio 和 Gradle)?

[英]How to use the new manifest merger (of Android Studio and Gradle)?

Background背景

In the past, when Eclipse&ADT were the official tools to develop for Android, you could simply use " manifestmerger.enabled=true " inside the " project.properties " of the app's project, and you got it merging all of the libraries' manifests automatically (and I've posted about it here ).过去,当 Eclipse&ADT 是为 Android 开发的官方工具时,您只需在应用程序项目的“ project.properties ”中使用“ manifestmerger.enabled=true ”,它就会自动合并所有库的清单(我已经在这里发布了它)。

This worked, sometimes.这有时奏效。 It had a lot of weird issues, and I always preferred to just avoid using it, and put what is needed into the main manifest file manually.它有很多奇怪的问题,我总是宁愿避免使用它,而是手动将所需的内容放入主清单文件中。

The problem问题

Somewhere on 2014, Google announced that the new Android-Studio (0.1 I think), together with Gradle, will allow you to choose exactly how to perform merging of libraries' components. 2014 年的某个时候,Google 宣布新的 Android-Studio(我认为是 0.1)与 Gradle 一起将允许您准确选择如何执行库组件的合并。

However, the new instructions (link here ) are very complex and I really (really) tried to understand how to use them, and also didn't find samples of how to use them.然而,新的说明(链接在这里)非常复杂,我真的(真的)试图了解如何使用它们,也没有找到如何使用它们的示例。

It's not that I didn't understand anything, but I'm not sure if I understood well.不是我什么都不懂,但我不确定我是否理解得很好。

What I've found我发现了什么

On the bright side, I've found out that merging is done completely automatically, so if you have a BroadcastReceiver on the library's manifest (and as a class, of course), it will be added to the app's project that uses it.从好的方面来说,我发现合并是完全自动完成的,所以如果您在库的清单上有一个 BroadcastReceiver(当然,作为一个类),它将被添加到使用它的应用程序项目中。

The question问题

I can't simply ask everything to be explained.我不能简单地要求解释一切。 I think it will be enough to ask those questions:我认为问这些问题就足够了:

  1. How can I choose which app components (permissions, activities,...) to be ignored from being auto-merged?如何选择要忽略的应用程序组件(权限、活动等),以免自动合并?

  2. How can I point override app components (of the library) attributes (on the app's project) ?如何指向覆盖应用程序组件(库的)属性(在应用程序的项目上)? for example the theme of the activities?比如活动的主题?

  3. Is there a way to completely disable the auto-merger for the manifest files?有没有办法完全禁用清单文件的自动合并?

  4. What happens with manifests of dependencies that are inside repositories?存储库内的依赖项清单会发生什么? Are they merged too?他们也合并了吗?

  5. Are there any tutorials/samples/videos regarding this new (well new for me) feature?是否有关于这个新(对我来说是新的)功能的教程/示例/视频?

  6. Are there any things I should be aware of when using the auto-merger?使用自动合并时有什么我应该注意的事情吗?

I hope those questions are representative enough, informative enough, yet not too hard to answer for people who know.我希望这些问题具有足够的代表性、足够的信息,但对于知道的人来说,回答起来并不难。

1. Disabling elements 1.禁用元素

You can always explicitly disable permissions and features in your app's manifest and override any library values.您始终可以在应用清单中明确禁用权限和功能,并覆盖任何库值。 And i found that you can disable elements from library .我发现您可以禁用 library 中的元素

Example例子

Consider the following code from the above link:考虑上面链接中的以下代码:

<activity-alias android:name="foo.bar.alias">
    <meta-data 
        android:name="zoo"
        tools:node="remove" />
</activity-alias>

By having this code inside your manifest you ensure that the merger finds any <activity-alias> elements with android:name="foo.bar.alias" attribute and removes a <meta-data> element if it has the android:name="zoo" attribute.通过让您的清单里面这个代码可以确保合并发现任何<activity-alias>的元素android:name="foo.bar.alias"属性,并删除一个<meta-data>元素,如果有android:name="zoo"属性。 It removes just the "zoo" meta data.它只删除“动物园”元数据。 Not the activity alias.不是活动别名。 If you specify this in your main manifest it will be effective on anything that has been merged so far (elements from libraries).如果您在主清单中指定它,它将对迄今为止已合并的任何内容(库中的元素)有效。

Example #2示例#2

Since you requested an example with activities, this is what I've come up with:由于您要求提供活动示例,这就是我想出的:

<activity android:name="com.example.ui.MyActivity" tools:node="remove" />

This line will make the merger remove any activities with android:name="com.example.ui.MyActivity" attribute that have been merged so far.此行将使合并删除迄今为止已合并的具有android:name="com.example.ui.MyActivity"属性的任何活动。 So if you specify this in your main manifest it will effectively remove any com.example.ui.MyActivity entries that might have been merged from libraries.因此,如果您在主清单中指定它,它将有效地删除任何可能已从库中合并的com.example.ui.MyActivity条目。

2. Overriding attributes from library 2. 覆盖库中的属性

The order in which the values are merged are described here . 此处描述合并值的顺序。 Basically, it goes like this: libraries, then main manifest, then flavors and build types manifests if you use those.基本上,它是这样的:库,然后是主清单,然后是风味和构建类型清单,如果你使用它们的话。

What are build types?什么是构建类型?

The default are "debug" and "release".默认为“调试”和“发布”。 You can define your own and override settings like signing or proguard .您可以定义自己的和覆盖设置,如签名proguard For your purposes you could say it's the equivalent of run configurations.出于您的目的,您可以说它相当于运行配置。

It works like this: you put your default and shared values inside the main manifest.它是这样工作的:您将默认值和共享值放在main清单中。 Then in flavor manifests you override the values you need.然后在风味清单中覆盖您需要的值。 Google "gradle flavors" for more info.谷歌“gradle flavors”了解更多信息。

The following example is taken from a previous version of manifest merger documentation .以下示例取自先前版本的清单合并文档

Override an attribute coming from a library覆盖来自库的属性

Using tools:replace="x, y, z" will override x,y,z attributes from the imported library's activity XML declarations.使用 tools:replace="x, y, z" 将覆盖来自导入库的活动 XML 声明的 x,y,z 属性。

Higher Priority declaration更高优先权声明

<activity android:name="com.foo.bar.ActivityOne" android:screenOrientation="portrait" android:theme="@theme1" tools:replace="theme"/>

with a lower priority declaration :具有较低优先级声明:

 <activity android:name="com.foo.bar.ActivityOne" android:theme="@olddogtheme" android:windowSoftInputMode="stateUnchanged" android:exported="true">

will result in :将导致:

 <activity android:name="com.foo.bar.ActivityOne" android:screenOrientation="portrait" android:theme="@theme1" android:windowSoftInputMode="stateUnchanged" android:exported="true"/>

3. Disabling manifest merger altogether 3. 完全禁用清单合并

See Disable Manifest Merger in Android Gradle Build .请参阅在 Android Gradle Build 中禁用清单合并

android.applicationVariants.all { variant ->
    variant.processResources.manifestFile = file('src/main/AndroidManifest.xml')
    variant.processManifest.enabled=false
}

In what file do you put this?你把这个放在什么文件里?

At the end of your module's (not root project) build.gradle .在您的模块(不是根项目) build.gradle

4. Are manifests from dependencies merged? 4. 是否合并了依赖项的清单?

Yes they are (they're libraries).是的,它们是(它们是图书馆)。

Is there a way to block merging certain library manifests?有没有办法阻止合并某些库清单?

Not that I know of, sorry.不是我所知道的,抱歉。

5. Any tutorials? 5. 有教程吗?

Depends on what are you trying to achive.取决于你想达到什么目的。 So far it always worked for me out-of-the-box.到目前为止,它对我来说总是开箱即用的。

I don't know about any videos.我不知道任何视频。

6. Anything I should be aware of? 6. 有什么需要注意的吗?

You can check the generated manifest if you get suspicious about extra permissions etc. It's located in project/module/build/intermediates/manifests/full/[flavor]/build-type/AndroidManifest.xml .如果您对额外权限等感到怀疑,您可以检查生成的清单。它位于project/module/build/intermediates/manifests/full/[flavor]/build-type/AndroidManifest.xml

Source: https://developer.android.com/studio/build/manifest-merge来源: https : //developer.android.com/studio/build/manifest-merge

Some of the links in this thread are obsolete.此线程中的某些链接已过时。 Here's the main one that is updated related to auto merger of manifests, by gradle, for Android AARs.这是与 Android AAR 的 gradle 自动合并清单相关的主要更新。

https://developer.android.com/studio/build/manifest-merge https://developer.android.com/studio/build/manifest-merge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM