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

Have you not yet distinguished between "fog" and "haze"?

Is it fog or haze? Environmental Science Autumn a...

How Momo plays the social networking game—product analysis report!

Purpose of analysis How does Momo do stranger soc...

Zhihu marketing promotion strategy!

Many people like it, and I have made many new fri...

2021 Teacher Xiaofei's Stock Theory Practical Training Camp 14th

1. This "Tip Chan Theory Practical Training ...

Marketing Trends in the Second Half of 2020

As usual, I will do a marketing inventory and revi...

The list is released! The App Store keyword lock list has been released.

At 9:30 am on January 6, 2017 Beijing time, the k...