Android Developer: merge list files

Source: Internet
Author: User
Tags configuration settings

Android Developer: merge list files

Android Studio is built based on Gradle. Each App can contain configuration files in multiple locations, such as productFlavor, library, and Android ARchive (AAR) in the src/main folder) bundles of Android Library Project, and third-party dependencies. During the building process, multiple AndroidMainfest. xml settings in your app are merged into one, and the APK inventory file is generated for app packaging and publishing. The configuration of the configuration file is merged Based on the configuration priority, depending on the location of the configuration file. Combine your app inventory file with the elements, attributes, and child element build of the build variant inventory file.

Merge conflict rules

------------------------------------------------------------------

The merging conflict occurs when the listing file contains the same listing element and has different attribute values. The default merging rule cannot be used to resolve the conflict. Conflict markers and selectors can also define custom merging rules. For example, a database that allows an import has a minSdkVersion greater than the version defined in other higher-priority lists.

The list merging priority determines which list settings are retained in the merge conflicts. Use the high-priority order file settings to overwrite the low-priority list. The following list details which configuration sets the highest priority during the merge process:

  • Highest priority: buildType Configuration

  • High priority: productFlavor Configuration

  • Medium priority: the app project is listed in the src/main/directory.

  • Low priority: dependency and library configuration settings

    List conflict merging is resolved at the XML node and attribute level based on the following merging rules:


    Listing merging rules are not supported:

    • The default value of uses-feature android: required; and use-library android: required is true, and one or both of them are used. In this way, all required functions and libraries are included in the generated APK.

    • If no statement is made, Element, minSdkVersion, and targetSdkVersion. The default value is 1. When a merge conflict occurs, the higher-priority list file value is used;

    • Import a src/main/configuration file containing the minSdkVersion value higher than the app. The configuration produces an error unless the overrideLibrary conflict mark is used;

      NOTE: If no explicit declaration is made, targetSdkVersion defaults to minSdkVersion. When no element appears in any inventory file or build. gradle file, the default value of minSdkVersion is 1.

      • When importing a library containing the src/main configuration file whose targetSdkVersion value is lower than the app, the inventory merging process explicitly grants permissions and ensures that the correct library function is imported.

      • The manifest element only merges CHILD List elements.

      • The intent-filter element never changes and is always added to the parent node of the same merged list.

        Important: After the configuration file is merged, use any settings in the build. gradle file to overwrite the final configuration. For more details, see Configure Gradle Builds.

        Merge conflict tags and selectors

        ------------------------------------------------------------------

        The configuration tag and Selector overwrite the default rules by creating a conflict resolution. For example, use a conflicting identifier to merge the minSdkVersion of a library list with a higher priority list, or merge the Activity with the same list but with different android: theme values.

        Merge conflicting identifiers

        A Merge conflict identifier is a specified Element in the Android tool space. It defines a specified merge Conflict scheme. Create a conflict identifier to avoid merge conflict errors that are not resolved using the default merge rules. The following table lists the supported merge conflict identifiers:

        Merge

        Merge attributes when there are no merging rule conflicts. The default merge action.

        Replace

        Use a high-priority list to replace attributes in a low-priority list;

        Strict

        Merge-only

        Allows merging of low-priority attributes;

        Remove

        Delete the low-level elements specified in the merged list;

        Remove-All

        Delete all low-priority elements of the same node type in the merged list;

        By default, the merge process of the list applies the merge conflict identifier at the node level. By default, all declared configuration attributes are strict merge policies.

        To set a merging conflict identifier, first declare the namespace in the AndroidManifest. xml file. Then, enter a specified merge conflict action for the merged conflict identifier in the list. In this example, the replace identifier is inserted to set a replace action to solve the conflict between the elements in the android: icon and android: lable list.

               
                             ...  
               

        Tag attributes

        The conflicting identifiers use the tools: node and tools: attr attributes to perform conflicting actions at the XML node or attribute level.

        Tools: the attr identifier only uses restrict, remove, and replace. Multiple tools: the value of the attr identifier can be applied to a specified element. For example, you can use tools: replace = "icon, lable, theme" to extract lower-priority icons, lable, and theme attributes.

        Inject build values into the inventory file

        -----------------------------------------------------------------

        Configuration File merging can also use a list placeholder to inject attribute values to the configuration property from the build. gradle file.

        The configuration placeholder uses this syntax $ {name} to set the attribute value. The name is the build. gradle attribute injected, And the build. gradle file uses the manifestPlaceholders attribute to define the placeholder value.

        Note: unresolved placeholder names in the app may cause building failure. Warnings are generated for unresolved placeholders in the library and must be resolved when the library is imported into an app.

        In this example, the placeholder $ {applcationId} is used to map the applicationID property value of build. gradle to the android: name property value.

        Note: Android Studio provides a default $ {applicationID} placeholder for the build. gradle applicationId value, which is not displayed in the build file. When creating an AAR (Android ARchive) package for the library module, you do not need to provide an automatic @ {applicationID} placeholder in the configuration merging settings. Instead, use a different placeholder, such as @ {libApplicationID}, and provide a value for the archive library if you want to include the appliction Id.

        List entity:

              
                                
                 
        Build file:

        android {    compileSdkVersion 22    buildToolsVersion "22.0.1"     productFlavors {        flavor1 {            applicationId = "com.mycompany.myapplication.productFlavor1"        } } 
        Merge list values:

         
        The placeholder configuration file and the manifestPlaceholders attribute of the build file can be used to inject other configuration file values. For applicationId dependency attributes, the manifestPlaceholders attributes are explicitly declared in the build. gradle file. This example shows how to inject the activityLabel value into the placeholder list.

        Gradle build file:

        android {     defaultConfig {         manifestPlaceholders = [ activityLabel:"defaultName"]     }     productFlavors {         free {         }         pro {             manifestPlaceholders = [ activityLabel:"proName" ]         }     } 
        Build file:

        android {     defaultConfig {         manifestPlaceholders = [ activityLabel:"defaultName"]     }     productFlavors {         free {         }         pro {             manifestPlaceholders = [ activityLabel:"proName" ]         }     } 

        Placeholders in the list file:

         
        Note:Placeholder values support partial value injection, such as android: authority = "com. acme. $ {localApplicationId}. foo ".

        Combine and list files using Product Flavor

        -----------------------------------------------------------------

        When the GroupbleProductFlavor attribute is used, the merging priority of any configuration files in the Product flavor group depends on the order in the build files. The configuration file merging process is based on the build variant build configuration, creating a separate merge configuration file for the product flavor group.

        For example, if a build varian from their respective product flavor group ABI, Density, API and Prod refer to product flavors x86, mdpi, 21, and paid, in build. the gradle file is arranged in this order, and then the configuration file is sorted by product flavors in the order of priority in the build file.

        To illustrate this example, the following identifier shows the product flavor list in each product flavor group. The combination of product flavors and group defines build variant.

        Product Flavor Group

        Product Flavor

        ABI

        X86

        Density

        Mdpi

        API

        22

        Prod

        Paid

        Order of list merging:

        • Prod-paid AndroidManifest. xml (low priority) is merged into AndroidManifest. xml API-22

        • API-22 AndroidManifest. xml merged into density-mpi AndroidManifest. xml

        • Density-mpi AndroidManifest. xml merge to ABI-x86 AndroidManifest. xml (high priority)

          Implicit permission

          -----------------------------------------------------------------

          Import an Android running library that supports implicit permission granting. This permission may be automatically added to the merged list. For example, if an application whose targetSdkVersion is 16 is imported into a database whose targetSdkVersion is 2, Andorid Studio adds the WRITE_EXTERNAL_STORAGE permission to ensure compatibility across SDK versions.

          Note: More current Android versions replace implicit permission declarations.

          Importing this library version

          Declares this permission in the manifest

          TargetSdkVersion <2

          WRITE_EXTERNAL_STORAGE

          TargetSdkVersion <4

          WRITE_EXTERNAL_STORAGE, READ_PHONE_STATE

          Declared WRITE_EXTERNAL_STORAGE

          READ_EXTERNAL_STORAGE

          TargetSdkVersion <16 and using the READ_CONTACTS permission

          READ_CALL_LOG

          TargetSdkVersion <16 and using WRITE_CONTACTS permission

          WRITE_CALL_LOG

          Handle configuration merging and building errors

          ------------------------------------------------------------------

          During the build process, the configuration merging process is in the mainfest-merge- The -report.txt file records each merged transaction. Build variants of each module generates different log files.

          When a configuration merge build error occurs, the build process records the error message describing the merge conflict in the log file. For example, a build error android: screenOrientation merge conflict occurs between the following lists.

          High-priority list declaration:

           
          Low-priority list declaration:

           
          Error Log:

          /project/app/src/main/AndroidManifest.xml:3:9 Error:  Attribute activity@screenOrientation value=(portrait) from AndroidManifest.xml:3:9  is also present at flavorlib:lib1:unspecified:3:18 value=(landscape)  Suggestion: add 'tools:replace="icon"' to  element at AndroidManifest.xml:1:5 to override 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.