The AndroidManifest.xml File

AndroidManifest详解
本文详细介绍了Android应用程序中核心配置文件AndroidManifest.xml的作用与结构。该文件不仅指定了应用的Java包名,还描述了应用组件(如活动和服务)、权限需求、最低API级别等关键信息。

http://developer.android.com/guide/topics/manifest/manifest-intro.html

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:

每个程序在根目录都必须都有一个AndroidManifest.xml (名称必须准确)

  • 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, whichIntentmessages 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 theInstrumentationclasses 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.

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.

下图展示了manifest文件的通常结构和可以包含的每个元素。

<?xml version="1.0" encoding="utf-8"?>

<manifest>

    <uses-permission />
    <permission />
    <permission-tree />
    <permission-group />
    <instrumentation />
    <uses-sdk />
    <uses-configuration />  
    <uses-feature />  
    <supports-screens />  
    <compatible-screens />  
    <supports-gl-texture />  

    <application>

        <activity>
            <intent-filter>
                <action />
                <category />
                <data />
            </intent-filter>
            <meta-data />
        </activity>

        <activity-alias>
            <intent-filter> . . . </intent-filter>
            <meta-data />
        </activity-alias>

        <service>
            <intent-filter> . . . </intent-filter>
            <meta-data/>
        </service>

        <receiver>
            <intent-filter> . . . </intent-filter>
            <meta-data />
        </receiver>

        <provider>
            <grant-uri-permission />
            <meta-data />
        </provider>

        <uses-library />

    </application>

</manifest>

All the elements that can appear in the manifest file are listed below in alphabetical order. These are the only legal elements; you cannot add your own elements or attributes.

<action>
<activity>
<activity-alias>
<application>
<category>
<data>
<grant-uri-permission>
<instrumentation>
<intent-filter>
<manifest>
<meta-data>
<permission>
<permission-group>
<permission-tree>
<provider>
<receiver>
<service>
<supports-screens>
<uses-configuration>
<uses-feature>
<uses-library>
<uses-permission>
<uses-sdk>

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 anandroid: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, andContentProvider), the subclass is declared through anameattribute. The name must include the full package designation. For example, anServicesubclass might be declared as follows:

<manifest . . . >
    <application . . . >
        <service android:name="com.example.project.SecretService" . . . >
            . . .
        </service>
        . . .
    </application>
</manifest>

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'spackageattribute). The following assignment is the same as the one above:
<manifest package="com.example.project" . . . >
  <application . . . >
    <service android:name=".SecretService" . . . >
      . . .
    </service>
    . . .
  </application>
</manifest>
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:
<intent-filter . . . >
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.INSERT" />
    <action android:name="android.intent.action.DELETE" />
    . . .
</intent-filter>

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 thepackagename can be omitted if the resource is in the same package as the application,typeis a type of resource — such as "string" or "drawable" — andnameis the name that identifies the specific resource. For example:

<activity android:icon="@drawable/smallPic" . . . >

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.

File Features

The following sections describe how some Android features are reflected in the manifest file.

Intent Filters

The core components of an application (its activities, services, and broadcast receivers) are activated byintents. An intent is a bundle of information (anIntentobject) describing a desired action — including the data to be acted upon, the category of component that should perform the action, and other pertinent instructions. Android locates an appropriate component to respond to the intent, launches a new instance of the component if one is needed, and passes it the Intent object.

Components advertise their capabilities — the kinds of intents they can respond to — throughintent filters. Since the Android system must learn which intents a component can handle before it launches the component, intent filters are specified in the manifest as<intent-filter>elements. A component may have any number of filters, each one describing a different capability.

An intent that explicitly names a target component will activate that component; the filter doesn't play a role. But an intent that doesn't specify a target by name can activate a component only if it can pass through one of the component's filters.

For information on how Intent objects are tested against intent filters, see a separate document,Intents and Intent Filters.

Icons and Labels

A number of elements haveiconandlabelattributes for a small icon and a text label that can be displayed to users. Some also have adescriptionattribute for longer explanatory text that can also be shown on-screen. For example, the<permission>element has all three of these attributes, so that when the user is asked whether to grant the permission to an application that has requested it, an icon representing the permission, the name of the permission, and a description of what it entails can all be presented to the user.

In every case, the icon and label set in a containing element become the defaulticonandlabelsettings for all of the container's subelements. Thus, the icon and label set in the<application>element are the default icon and label for each of the application's components. Similarly, the icon and label set for a component — for example, an<activity>element — are the default settings for each of the component's<intent-filter>elements. If an<application>element sets a label, but an activity and its intent filter do not, the application label is treated as the label for both the activity and the intent filter.

The icon and label set for an intent filter are used to represent a component whenever the component is presented to the user as fulfilling the function advertised by the filter. For example, a filter with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings advertises an activity as one that initiates an application — that is, as one that should be displayed in the application launcher. The icon and label set in the filter are therefore the ones displayed in the launcher.

Permissions

Apermissionis a restriction limiting access to a part of the code or to data on the device. The limitation is imposed to protect critical data and code that could be misused to distort or damage the user experience.

Each permission is identified by a unique label. Often the label indicates the action that's restricted. For example, here are some permissions defined by Android:

android.permission.CALL_EMERGENCY_NUMBERS
android.permission.READ_OWNER_DATA
android.permission.SET_WALLPAPER
android.permission.DEVICE_POWER

A feature can be protected by at most one permission.

If an application needs access to a feature protected by a permission, it must declare that it requires that permission with a<uses-permission>element in the manifest. Then, when the application is installed on the device, the installer determines whether or not to grant the requested permission by checking the authorities that signed the application's certificates and, in some cases, asking the user. If the permission is granted, the application is able to use the protected features. If not, its attempts to access those features will simply fail without any notification to the user.

An application can also protect its own components (activities, services, broadcast receivers, and content providers) with permissions. It can employ any of the permissions defined by Android (listed inandroid.Manifest.permission) or declared by other applications. Or it can define its own. A new permission is declared with the<permission>element. For example, an activity could be protected as follows:

<manifest . . . >
    <permission android:name="com.example.project.DEBIT_ACCT" . . . />
    <uses-permission android:name="com.example.project.DEBIT_ACCT" />
    . . .
    <application . . .>
        <activity android:name="com.example.project.FreneticActivity"
                  android:permission="com.example.project.DEBIT_ACCT"
                  . . . >
            . . .
        </activity>
    </application>
</manifest>

Note that, in this example, theDEBIT_ACCTpermission is not only declared with the<permission>element, its use is also requested with the<uses-permission>element. Its use must be requested in order for other components of the application to launch the protected activity, even though the protection is imposed by the application itself.

If, in the same example, thepermissionattribute was set to a permission declared elsewhere (such asandroid.permission.CALL_EMERGENCY_NUMBERS, it would not have been necessary to declare it again with a<permission>element. However, it would still have been necessary to request its use with<uses-permission>.

The<permission-tree>element declares a namespace for a group of permissions that will be defined in code. And<permission-group>defines a label for a set of permissions (both those declared in the manifest with<permission>elements and those declared elsewhere). It affects only how the permissions are grouped when presented to the user. The<permission-group>element does not specify which permissions belong to the group; it just gives the group a name. A permission is placed in the group by assigning the group name to the<permission>element'spermissionGroupattribute.

Libraries

Every application is linked against the default Android library, which includes the basic packages for building applications (with common classes such as Activity, Service, Intent, View, Button, Application, ContentProvider, and so on).

However, some packages reside in their own libraries. If your application uses code from any of these packages, it must explicitly asked to be linked against them. The manifest must contain a separate<uses-library>element to name each of the libraries. (The library name can be found in the documentation for the package.)


<think>我们正在解决ARCore依赖项中缺少minimumversioncode的问题,具体错误是"ARCoredependencymissingminimumversioncodecom.google.ar.core.min_apk_version"。根据引用[3]中的信息,错误可能出现在AndroidManifest.xml文件中,提示在package元素上缺少'package'键属性。但我们的问题更具体:缺少min_apk_version。在ARCore应用中,需要在AndroidManifest.xml中声明min_apk_version,以确保设备上的ARCore服务(GooglePlayServicesforAR)满足最低版本要求。解决方案步骤:1.AndroidManifest.xml的<application>标签内,添加一个<meta-data>元素来指定min_apk_version。2.该<meta-data>元素的name属性应为"com.google.ar.core.min_apk_version",value属性应设置为一个整数,表示所需的最低ARCoreAPK版本。如何确定min_apk_version的值?通常,这个值应该与你在build.gradle中引入的ARCoreSDK版本所要求的最低APK版本一致。你可以参考ARCoreSDK的发布说明或文档,但通常的做法是使用当前SDK版本对应的APK版本。例如,如果你使用的是ARCoreSDK1.40.0,那么min_apk_version可能是210304000(这是一个示例,具体值需要查阅文档)。另一种方法是,如果你不确定具体的版本号,可以先尝试构建应用,然后查看错误日志,有时会提示期望的版本号。或者,你可以访问ARCore的官方文档查看版本对应关系。示例代码(在AndroidManifest.xml中):```xml<application...>...<meta-dataandroid:name="com.google.ar.core.min_apk_version"android:value="210304000"/>...</application>```注意:这个<meta-data>元素必须放在<application>标签内。另外,根据引用[2],还需要确保项目依赖的ARCoreSDK版本与设备上安装的ARCoreAPK版本兼容。如果设备上的ARCoreAPK版本过低,用户会被提示更新,但我们可以通过设置min_apk_version来要求一个最低版本。如果添加后问题仍然存在,请检查:-是否在正确的AndroidManifest.xml文件中(如果是多模块项目,确保在主模块的manifest文件中)。-是否拼写错误(如android:name的值必须是"com.google.ar.core.min_apk_version")。此外,引用[1]中提到的检查设备是否支持ARCore的代码也是必要的,这样可以在运行时处理不支持的情况。如果上述方法不能解决问题,可能需要检查ARCoreSDK的版本,并尝试降低版本(如引用[2]所述),以确保与设备上安装的ARCore版本兼容。因此,总结解决方案:1.AndroidManifest.xml中添加min_apk_version的<meta-data>。2.确保ARCoreSDK版本与min_apk_version匹配。3.如果问题依旧,尝试调整ARCoreSDK版本。注意:min_apk_version的值是一个整数,代表ARCore服务的最低版本,不能设置为变量或字符串。如果不知道具体数值,可以暂时设置为0,但这样会跳过版本检查,不推荐。最好设置一个合适的值。如何获取正确的min_apk_version?可以查看ARCoreSDK的更新日志,或者查看ARCore客户端库中的常量。在ARCoreSDK的AAR包中,可能包含一个version.properties文件,里面可能有相关信息。但更直接的方法是查阅官方文档或示例项目。例如,在ARCore的官方示例中,AndroidManifest.xml通常会有这个设置。你可以参考:https://github.com/google-ar/arcore-android-sdk在示例项目中查找AndroidManifest.xml,例如在samples/hello_ar项目里,通常会有:```xml<meta-dataandroid:name="com.google.ar.core.min_apk_version"android:value="202340000"/>```所以,根据你使用的ARCoreSDK版本,找到对应的min_apk_version值。另外,也可以根据ARCore版本号计算:ARCore版本号格式为1.XX.YY,对应的min_apk_version为20XXYY000(将主版本1去掉,然后小版本和补丁版本各取两位,最后三位补零)。例如,ARCore1.40.0->2040000->20400000?但实际上示例中1.40.0对应的min_apk_version是210304000(这个计算方式可能不对,所以最好还是查官方示例)。实际上,ARCore的版本号与min_apk_version并没有简单的数学关系,所以建议直接查看官方示例中的设置。由于官方示例可能更新,这里提供一个参考:在ARCoreSDK1.40.0的示例中,min_apk_version设置为210304000(这是2023年4月发布的版本)。因此,解决方案的核心是添加正确的<meta-data>。步骤:1.打开AndroidManifest.xml(在app/src/main目录下)。2.在<application>标签内添加:```xml<meta-dataandroid:name="com.google.ar.core.min_apk_version"android:value="210304000"/>```3.将value的值替换为适合你SDK版本的值(参考官方示例或文档)。如果构建后还有问题,请检查ARCoreSDK的依赖版本,并尝试同步项目。另外,引用[3]中的错误提示“Missing‘package’keyattributeonelementpackage”,可能是另一个问题,但与我们当前的问题无关。如果你同时遇到那个问题,需要在<manifest>标签中指定package属性,例如:<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.your.package">。因此,请确保你的AndroidManifest.xml的<manifest>标签有package属性。总结:添加min_apk_version的<meta-data>并确保manifest的package属性存在。如果问题解决,则完成;如果仍然报错,请根据错误信息进一步调整。</think>### 解决 ARCore 依赖缺少 `min_apk_version` 的问题 当 AndroidManifest.xml 报错 **"ARCore dependency missing minimum version code com.google.ar.core.min_apk_version"** 时,表明项目未声明 ARCore 所需的最低 APK 版本。以下是完整解决方案: #### 步骤 1:添加 `min_apk_version` 到 AndroidManifest.xml 在 `<application>` 标签内添加以下元数据: ```xml <application> ... <!-- 添加此行 --> <meta-data android:name="com.google.ar.core.min_apk_version" android:value="202304000" /> ... </application> ``` - **`android:value` 取值规则**: - 参考 ARCore 官方版本号(如 1.40.0 对应 `202304000`) - 最新值需查阅 [ARCore Release Notes](https://developers.google.com/ar/releases) - **作用**:指定设备上 ARCore 服务的最低兼容版本[^1] #### 步骤 2:同步依赖版本 1. 检查 `build.gradle` 中的 ARCore 版本: ```gradle dependencies { implementation 'com.google.ar:core:1.40.0' // 确保与 min_apk_version 匹配 } ``` 2. **版本一致性原则**: - 模块依赖版本(如 1.40.0) → Manifest 中的 `min_apk_version`(如 202304000) - 设备安装的 ARCore APK 版本必须 ≥ 此值[^2] #### 步骤 3:验证设备兼容性 在 Activity 中添加运行时检查: ```java // 参考引用[1]的代码 try { if (Session.isSupported(this)) { // ARCore 可用 } else { // 处理不支持的情况 } } catch (ArcoreInstallException e) { // 捕获安装异常 e.printStackTrace(); } ``` #### 常见错误排查 | 错误现象 | 解决方案 | |---------|----------| | `UnavailableUserDeclinedInstallationException` | 降低 ARCore SDK 版本至与设备 APK 版本一致[^2] | | `Missing 'package' key attribute` | 在 Manifest 根标签添加 `package="com.your.app.id"`[^3] | | 安装后闪退 | 确保设备已安装 [Google Play Services for AR](https://play.google.com/store/apps/details?id=com.google.ar.core) | #### 最佳实践 1. **版本映射工具**: 使用 ARCore 提供的 [Version API](https://developers.google.com/ar/develop/java/version-checking) 动态检查兼容性 2. **降级策略**: 在 `build.gradle` 中添加最低 SDK 限制: ```gradle defaultConfig { minSdk 24 // ARCore 要求 Android 7.0+ } ``` > ⚠️ **重要提示**:若问题仍未解决,清理项目缓存(`File > Invalidate Caches`)并重新同步 Gradle。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值