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

前端 未结 2 870
太阳男子
太阳男子 2020-12-23 17:30

Background

In the past, when Eclipse&ADT were the official tools to develop for Android, you could simply use \"manifestmerger.enabled=true\"

相关标签:
2条回答
  • 2020-12-23 17:58

    1. Disabling elements

    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.

    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. 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

    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. 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.

    2. Overriding attributes from library

    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. 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. Then in flavor manifests you override the values you need. Google "gradle flavors" for more info.

    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.

    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

    See Disable Manifest Merger in 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.

    4. Are manifests from dependencies merged?

    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?

    Depends on what are you trying to achive. So far it always worked for me out-of-the-box.

    • e.g. http://www.myandroidsolutions.com/2014/04/10/android-gradle-manifest-merge/
    • The manifest merger documentation (link below).

    I don't know about any videos.

    6. Anything I should be aware of?

    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.

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

    0 讨论(0)
  • 2020-12-23 18:11

    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.

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

    0 讨论(0)
提交回复
热议问题