AndroidManifest.xml

   Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory. The manifest presents essential information about the application to the Android system, information the system must have before it can run any of the application's code. Among other things, the manifest does the following:

  • It names the Java package for the application. The package name serves as a unique identifier for the application.
  • It describes the components of the application — the activities, services, broadcast receivers, and content providers that the application is composed of. It names the classes that implement each of the components and publishes their capabilities (for example, which Intent messages they can handle). These declarations let the Android system know what the components are and under what conditions they can be launched.
  • It determines which processes will host application components.
  • It declares which permissions the application must have in order to access protected parts of the API and interact with other applications.
  • It also declares the permissions that others are required to have in order to interact with the application's components.
  • It lists the Instrumentation classes that provide profiling and other information as the application is running. These declarations are present in the manifest only while the application is being developed and tested; they're removed before the application is published.
  • It declares the minimum level of the Android API that the application requires.
  • It lists the libraries that the application must be linked against.

 

每个应用程序必须有个AndroidManifest.xml文件在它的根目录(必须是这个名字)。在运行任何这个应用程序的代码之前,必须通过Androidmanifest.xml将这个应用程序的基本信息告诉android系统,简单的说,就是像系统注册一些信息。具体的一些操作如下:

1,为应用程序命名一个java包名,这个包名是对应用程序是唯一的,系统不允许装两个一样包名的应用程序。

2,描述了这个应用程序的组件,包括activities,services,broadcast receivers,和content providers.这里声明这些组件类的名称以及它们能力(比如说,能处理的Intent信息的类别).这里的声明让android系统知道什么样的组件在什么情况下被调用。

3,它声明了这些应用程序的组件分别在哪个进程中

4,它声明了一些权限permissions,为了让应用程序可以使用受保护的api和其他的应用程序进行交换

5,它还声明了一些权限permissions,为了其他的应用程序使用到了我们受保护的组件应该有的权限。

6,它列举了用来提供研究应用程序运行起来的一些信息的Instrumentation类,这些声明只有在应用程序的开发和测试阶段,不应该出现在应用程序发布后。

7,它声明了最小的api level。

8,它声明了应用程序链接的库。

Structure of the Manifest File

The diagram below shows the general structure of the manifest file and every element that it can contain. Each element, along with all of its attributes, is documented in full in a separate file. To view detailed information about any element, click on the element name in the diagram, in the alphabetical list of elements that follows the diagram, or on any other mention of the element name.

 

 

File Conventions

Some conventions and rules apply generally to all elements and attributes in the manifest:

 

Elements
Only the <manifest> and <application> elements are required, they each must be present and can occur only once. Most of the others can occur many times or not at all — although at least some of them must be present for the manifest to accomplish anything meaningful.

If an element contains anything at all, it contains other elements. All values are set through attributes, not as character data within an element.

Elements at the same level are generally not ordered. For example, <activity>, <provider>, and <service> elements can be intermixed in any sequence. (An <activity-alias> element is the exception to this rule: It must follow the <activity> it is an alias for.)

Attributes
In a formal sense, all attributes are optional. However, there are some that must be specified for an element to accomplish its purpose. Use the documentation as a guide. For truly optional attributes, it mentions a default value or states what happens in the absence of a specification.

Except for some attributes of the root <manifest> element, all attribute names begin with an android: prefix — for example, android:alwaysRetainTaskState. Because the prefix is universal, the documentation generally omits it when referring to attributes by name.

Declaring class names
Many elements correspond to Java objects, including elements for the application itself (the <application> element) and its principal components — activities ( <activity>), services ( <service>), broadcast receivers ( <receiver>), and content providers ( <provider>).

If you define a subclass, as you almost always would for the component classes (Activity, Service, BroadcastReceiver, and ContentProvider), the subclass is declared through a name attribute. The name must include the full package designation. For example, an Service subclass might be declared as follows:

However, as a shorthand, if the first character of the string is a period, the string is appended to the application's package name (as specified by the <manifest> element's package attribute). The following assignment is the same as the one above:

When starting a component, Android creates an instance of the named subclass. If a subclass isn't specified, it creates an instance of the base class.

Multiple values
If more than one value can be specified, the element is almost always repeated, rather than listing multiple values within a single element. For example, an intent filter can list several actions:

Resource values
Some attributes have values that can be displayed to users — for example, a label and an icon for an activity. The values of these attributes should be localized and therefore set from a resource or theme. Resource values are expressed in the following format,

 

@[package:]type:name

where the package name can be omitted if the resource is in the same package as the application, type is a type of resource — such as "string" or "drawable" — and name is the name that identifies the specific resource. For example:

Values from a theme are expressed in a similar manner, but with an initial '?' rather than '@':

?[package:]type:name

String values
Where an attribute value is a string, double backslashes (' //') must be used to escape characters — for example, ' //n' for a newline or ' //uxxxx' for a Unicode character.

 

文件约定(一个书写习惯)

一些约定和规矩是几乎是针对所有的manifest的元素和属性

 

元素   只有<manifest>和<application>这两个元素是必须的,他们必须出现并且只能出现一次(我亲自试过书写了两个application在里面,前一个好使,好像也没报错啊,不过有一个好使倒是认真的)。其他的大多数会出现很多次或者是不出现---其他的一些元素应该出现,这样manifest才会有意义

 

If an element contains anything at all, it contains other elements. All values are set through attributes, not as character data within an element. 如果这个元素要包含任何东西,它就需要包含其他元素,也就是它想要表达一个东西必须通过子元素来表达。所有要设置的值是通过属性,而不是一个元素的字符值。

 

同一级别的元素排列是乱序的,比如说<activity>,<provider> and<service>等元素可以任意搭配序列(有一个特殊的,<activity-alias>这个元素,它必须在它要起别名的<activity>后面)

 

属性 从某种角度上来讲,所有的属性都是可选的。然而,必须有些属性被指定,这样才能达到要这个元素的目的。可以用文档做一个向导。对于所有可选的属性,可以看到缺省值或者省略代表的意义。

       和一些属性

 

 

 

android:debuggable 模式在发布时改成false

 

 

### 如何修改 AndroidManifest.xml 文件 在 Android 开发过程中,有时需要动态修改 `AndroidManifest.xml` 文件的内容。以下是几种常见的场景以及对应的解决方案。 #### 场景一:通过 Gradle 动态修改 `AndroidManifest.xml` Gradle 提供了一种机制,可以在构建期间动态修改 `AndroidManifest.xml` 的内容。以下是一个具体的例子: ```groovy android.applicationVariants.all { variant -> def manifestPath = "${buildDir}/intermediates/merged_manifests/${variant.dirName}/AndroidManifest.xml" variant.mergeResources.doLast { ant.replace(file: manifestPath, token: "@versionCode@", value: variant.versionCode.toString()) ant.replace(file: manifestPath, token: "@versionName@", value: variant.versionName) } } ``` 此代码片段展示了如何替换 `AndroidManifest.xml` 中的占位符(如 `@versionCode@` 和 `@versionName@`),并将其更新为实际的版本号[^1]。 --- #### 场景二:使用 DOM 解析器手动修改 `AndroidManifest.xml` 如果需要更复杂的操作,可以借助 Java 或 Groovy 的 DOM 解析库来读取和修改 XML 文件。下面是一段示例代码: ```groovy import groovy.util.XmlParser import groovy.xml.StreamingMarkupBuilder def modifyManifest(String manifestFilePath) { File file = new File(manifestFilePath) def xml = new XmlParser().parse(file) // 获取 application 节点 def applicationNode = xml.'application'[0] // 添加自定义属性或节点 applicationNode.attributes()['customAttribute'] = 'value' // 将修改后的 XML 写回文件 def writer = new StringWriter() new StreamingMarkupBuilder().bind { mkp.yield xml }.writeTo(writer) file.text = writer.toString() } // 调用函数 modifyManifest("${buildDir}/intermediates/manifests/full/debug/AndroidManifest.xml")[^2] ``` 这段代码演示了如何使用 Groovy 的 `XmlParser` 来解析和修改 `AndroidManifest.xml` 文件中的 `<application>` 节点。 --- #### 场景三:解决 `uses-sdk` 警告 当遇到 `Not targeting the latest versions of Android; compatibility modes apply.` 这样的警告时,可以通过调整 `minSdkVersion`、`targetSdkVersion` 和 `compileSdkVersion` 的值来解决问题。例如: ```groovy android { compileSdkVersion 33 defaultConfig { minSdkVersion 21 targetSdkVersion 33 } } ``` 这样可以确保应用的目标 SDK 版本是最新的,并减少兼容性模式的影响[^3]。 --- #### 场景四:动态设置 Dialog 样式 虽然这不是直接修改 `AndroidManifest.xml` 的方法,但如果涉及 UI 配置,也可以通过样式文件间接影响行为。例如,设置对话框标题样式的代码如下所示: ```xml <style name="DialogWindowTitle"> <item name="android:textSize">22sp</item> <item name="android:textColor">@color/font_dark_grey</item> </style> ``` 然后在创建对话框时指定该样式即可[^4]。 --- #### 场景五:动态编译多渠道 APK 并修改 Manifest 对于多渠道打包需求,可以利用 Gradle 的产品变体功能动态修改 `AndroidManifest.xml` 的内容。例如: ```groovy android.applicationVariants.all { variant -> variant.outputs.each { output -> def apkFile = output.outputFileName def newName = apkFile.replace(".apk", "_v${defaultConfig.versionName}-${releaseTime()}.apk") outputFileName = newName // 修改 manifest 属性 variant.mergedFlavor.manifestPlaceholders.put("channelId", variant.flavorName) } } ``` 在此基础上,还可以进一步扩展逻辑以支持更多动态配置[^5]。 --- ### 总结 以上提供了多种方式用于修改 `AndroidManifest.xml` 文件,具体选择取决于项目需求和技术背景。无论是简单的字符串替换还是复杂的数据结构处理,都可以找到合适的工具完成任务。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值