AndroidManifest file introduction and merge conflict rules

AndroidManifest file introduction and merge conflict rules

Introduction to AndroidManifest File

AndroidManifest.xml is the manifest file of an Android application, which contains all the information of the application, including the package name of the application, declarations of components (such as activities, services, receivers, and content providers), permission requirements, minimum SDK version requirements of the application, etc. It is the entry file of the Android application, and the system will identify various information and configurations of the application based on this file.

Typically, the AndroidManifest.xml file is located in the "app/src/main" directory under the root directory of the application. In the AndroidManifest.xml file, you can declare information such as the various components of the application, permission requirements, application icons and themes, etc.

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="33" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" /> <service android:name=".MyService" /> <receiver android:name=".MyReceiver" /> <provider android:name=".MyProvider" /> </application> </manifest>

In the above example, we declared an application package named "com.example.myapp", which contains a MainActivity activity, a SecondActivity activity, a MyService service, a MyReceiver receiver and a MyProvider content provider. At the same time, we also declared that the application needs to use the INTERNET and ACCESS_NETWORK_STATE permissions, and specified the minimum SDK version of the application as 19 and the target SDK version as 33.

Merge conflict rules

In Android development, when using multiple libraries or modules, AndroidManifest.xml file merge conflicts may occur. The AndroidManifest.xml file contains the configuration information of the application. When merge conflicts occur, the system needs to determine how to handle these conflicts.

The merge conflict rules are as follows:

  1. The merging rules of Activity, Service, Receiver and other components are as follows:

If components are defined in different AndroidManifest.xml files and have the same priority, a merge conflict will occur.

If components are defined in different AndroidManifest.xml files but have different priorities, the system chooses the component with the higher priority.

  1. Permission merging rules:

If the same permissions are declared in two AndroidManifest.xml files, but the permission declarations are different, a merge conflict will occur.

If the same permissions are declared in both AndroidManifest.xml files and the permission declarations are identical, no merge conflict will occur.

  1. Merging rules for other elements:

For other elements (such as <meta-data>, <uses-library>, etc.), if the same element is defined in different AndroidManifest.xml files, a merge conflict will occur.

When dealing with merge conflicts, you need to carefully check the merged AndroidManifest.xml file to ensure that the merged configuration meets the needs of the application and does not cause unexpected conflicts.

Merge conflict markers and selectors

「Merge conflict marker」:

  • tools:replace: Indicates that existing elements should be replaced when merging.
  • tools:remove: Indicates that existing elements should be removed when merging.
  • tools:keep: Indicates that existing elements should be kept when merging.

A merge conflict selector is used to specify which version of an element should be used when there is a merge conflict. Common selectors include:

  • manifestmerger: Indicates that the default behavior provided by the manifestmerger tool should be used when merging.
  • highest: Indicates that the element with the highest version code should be selected when merging.
  • strict: indicates that the selection should be made strictly according to the rules when merging. If it cannot be determined, an error will be reported.

For example, you can use the following in your AndroidManifest.xml to mark and select merge conflicts:

 <activity android:name=".MainActivity" tools:replace="android:label" tools:node="merge"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

In the example above, the tools:replace tag is used to indicate that the android:label attribute should be replaced when merging, and the tools:node="merge" selector is used to indicate the default behavior provided by the manifestmerger tool.

For more information, please refer to: https://developer.android.google.cn/studio/build/manage-manifests?hl=zh-cn#merge_rule_markers

<<:  iOS 17.2 released with a wave of new features

>>:  Implementing Audio Graphs in SwiftUI

Recommend

How was the beautiful coral reef coast formed?

Produced by: Science Popularization China Author:...

Double Eleven video material creative guide!

This article brings you the "11.11 Video Mat...

These 7 points will help your landing page conversion rate soar!

A landing page , also known as a "landing pa...

How to do low-cost marketing?

No matter whether your business or brand is big o...

Optimization strategy for short video advertising in the tourism industry!

Summer vacation is approaching, and a new wave of...

How to efficiently display bitmaps on Android App

To create visually appealing apps, displaying ima...

Apple iOS 10 updates these, 4s really can't use

Since iOS 7 abandoned the skeuomorphic interface t...

If you just keep eating and eating, maybe you can "save the world"?

Before getting into the main topic, I would like ...

Brand promotion: 23 ways to refine product and brand selling points

The starting point of all businesses is to benefi...

Case analysis: Explosive growth of operational activities!

User Growth (UG for short), like the concept of “...