AndroidManifest. xml configuration file parsing

Source: Internet
Author: User

AndroidManifest. xml configuration file is a very important basic knowledge for Android Application Development. This article aims to summarize the key usage in this configuration file for future reference. The following is a standard AndroidManifest. xml file sample. Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest>
<! -- Basic configuration -->
<Uses-permission/>
<Permission/>
<Permission-tree/>
<Permission-group/>
<Instrumentation/>
<Uses-sdk/>
<Uses-configuration/>
<Uses-feature/>
<Supports-screens/>
<Compatible-screens/>
<Supports-gl-texture/>
<! -- Application configuration -->
<Application>
<! -- Activity configuration -->
<Activity>
<Intent-filter>
<Action/>
<Category/>
<Data/>
</Intent-filter>
<Meta-data/>
</Activity>
<Activity-alias>
<Intent-filter>... </intent-filter>
<Meta-data/>
</Activity-alias>
<! -- Service configuration -->
<Service>
<Intent-filter>... </intent-filter>
<Meta-data/>
</Service>
<! -- Cycler configuration -->
<Cycler>
<Intent-filter>... </intent-filter>
<Meta-data/>
</Cycler>
<! -- Provider configuration -->
<Provider>
<Grant-uri-permission/>
<Meta-data/>
</Provider>
<! -- Required class library configuration -->
<Uses-library/>
</Application>
</Manifest>

From the above sample code, we can see that the Android configuration file uses XML as the description language. Each XML tag has different meanings, and most of the configuration parameters are placed in the tag attributes, next, we will learn how to use the main elements and labels in the Android configuration file in sequence in the preceding configuration file sample.
<Manifest>
The root element of the AndroidManifest. xml configuration file must contain a <application> element and specify the xlmns: android and package attributes. Xlmns: android specifies the Android namespace. The default value is. Of course, there are some other common attributes that need to be noted. For example, android: versionCode is used to identify the version of the device program. It must be an integer that indicates how many times the app has been updated. android: versionName is used to view the version and must be readable, for example, "1.0.0. <Manifest> the tag syntax example is as follows.
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "string"
Android: sharedUserId = "string"
Android: sharedUserLabel = "string resource"
Android: versionCode = "integer"
Android: versionName = "string"
Android: installLocation = ["auto" | "internalOnly" | "preferExternal"]>
......
</Manifest>
<Uses-permission>
To ensure the security of Android applications, the application framework has developed a strict permission system. An application must declare the correct permissions to use the corresponding functions, for example, you need to configure "android. permission. INTERNET, and if you want to use the camera function of the device, you need to set "android. permission. CAMERA. <Uses-permission> is the most frequently used permission setting tag. We declare the corresponding permission name by setting the android: name attribute, for example, in a Weibo application instance, we declare the corresponding permissions based on the functions required by the application. The relevant code is as follows.
<Manifest...>
......
<! -- Network-related functions -->
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
<! -- Read the phone status -->
<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
<! -- Notification-related functions -->
<Uses-permission android: name = "android. permission. VIBRATE"/>
......
</Manifest>
<Permission>
The permission declaration label defines the specific permissions that are provided to <uses-permission>. Generally, we do not need to declare a permission for our application, unless you need to provide callable code or data to other applications, you need to use the <permission> label. This tag provides the android: name permission name tag, permission icon android: icon, permission description android: description, and other attributes, it can also be used with <permission-group> and <permission-tree> to construct a more hierarchical and targeted permission system. <Permission> the tag syntax example is as follows.
<Permission android: description = "string resource"
Android: icon = "drawable resource"
Android: label = "string resource"
Android: name = "string"
Android: permissionGroup = "string"
Android: protectionLevel = ["normal" | "dangerous" | "signature" | "signatureOrSystem"]/>
<Instrumentation>
It is used to declare the Instrumentation Test class to monitor the behaviors of Android applications and apply them to relevant functional tests. The important attributes include: test function switch android: functionalTest, profiling debugging function switch android: handleProfiling: The target android: targetPackage of the test case. In addition, we need to note that the Instrumentation object is instantiated before the application components, which should be taken into account when organizing the test logic. <Instrumentation> the tag syntax example is as follows.
<Instrumentation android: functionalTest = ["true" | "false"]
Android: handleProfiling = ["true" | "false"]
Android: icon = "drawable resource"
Android: label = "string resource"
Android: name = "string"
Android: targetPackage = "string"/>
<Uses-sdk>
This parameter is used to specify the version of the SDK required for an Android application. For example, if your application must run on the system SDK of Android 2.0 or later, you must specify the minimum SDK version of the application as 5; of course, each SDK version corresponds to a specified integer. For example, the number of Android 2.2.x versions that we use most frequently is 8. Of course, in addition to specifying the lowest version, the <uses-sdk> tag can also specify the highest and target versions. The syntax example is as follows.
<Uses-sdk android: minSdkVersion = "integer"
Android: targetSdkVersion = "integer"
Android: maxSdkVersion = "integer"/>
<Uses-configuration> and <uses-feature>
Both labels are used to describe the hardware and software features required by the application to Prevent the application from being installed on devices without these features. <Uses-configuration> tags. For example, if some devices have special hardware such as D-pad or Trackball, set the android: reqFiveWayNav attribute to true; if some devices have hardware keyboards, android: reqHardKeyboard needs to be set to true. In addition, if the device needs to support bluetooth, you can use <uses-feature android: name = "android. hardware. bluetooth"/> to support this function. The two tags are mainly used to support applications on some special devices. The syntax examples of the two tags are as follows.
<Uses-configuration android: reqFiveWayNav = ["true" | "false"]
Android: reqHardKeyboard = ["true" | "false"]
Android: reqKeyboardType = ["undefined" | "nokeys" | "qwerty" | "twelvekey"]
Android: reqNavigation = ["undefined" | "nonav" | "dpad" | "trackball" | "wheel"]
Android: reqTouchScreen = ["undefined" | "notouch" | "stylus" | "finger"]/>
<Uses-feature android: name = "string"
Android: required = ["true" | "false"]
Android: glEsVersion = "integer"/>
<Uses-library>
This parameter is used to specify the user library that can be used by the Android Application, except for the built-in android. app, android. content, android. view and android. in addition to default class libraries such as widgets, some applications may also need some other Java class libraries for support, in this case, we can use the <uses-library> label to let ClassLoader load its class library for Android Application runtime. <Uses-library> labels are easy to use. The following is a syntax example.
<Uses-library android: name = "string"
Android: required = ["true" | "false"]/>
Tips:
When running a Java program, first run the JVM (Java Virtual Machine) and then load the Java class into the JVM for running. This part is called ClassLoader. Of course, ClassLoader consists of multiple parts, and each part is responsible for loading. When running a program, the JVM starts and runs BootstrapClassLoader. This ClassLoader loads the java core API (ExtClassLoader and AppClassLoader are also loaded at this time), and then calls ExtClassLoader to load the extended API, finally, AppClassLoader loads the Class defined under the CLASSPATH Directory, which is the most basic loading process of a Java program.
<Supports-screens>
For some applications or games, the <supports-screens> label can be used to specify the supported screen features only for certain screen-sized devices or for certain devices. Among them, more important attributes include: screen adaptive attributes android: resizeable, small screen (android: smallScreens), middle screen (android: normalScreens), large screen (android: largeScreens) and special screen (android: xlargeScreens) supports attributes: rendering images by screen attributes android: anyDensity and minimum screen width attributes android: requiresSmallestWidthDp. The syntax example of the <supports-screens> label is as follows.
<Supports-screens android: resizeable = ["true" | "false"]
Android: smallScreens = ["true" | "false"]
Android: normalScreens = ["true" | "false"]
Android: largeScreens = ["true" | "false"]
Android: xlargeScreens = ["true" | "false"]
Android: anyDensity = ["true" | "false"]
Android: requiresSmallestWidthDp = "integer"
Android: compatibleWidthLimitDp = "integer"
Android: largestWidthLimitDp = "integer"/>
<Application>
The root element of the application configuration, which is located in the lower layer of <manifest> and contains all elements related to the application. Its attributes can be used as the default attributes of sub-elements. common attributes include: app name android: label, app icon android: icon, app topic android: theme, etc. Of course, the <application> tag also provides other rich configuration attributes, which are not listed due to space reasons. You can open the Android SDK documentation for further study. The following is a syntax example.
<Application android: allowTaskReparenting = ["true" | "false"]
Android: backupAgent = "string"
Android: debuggable = ["true" | "false"]
Android: description = "string resource"
Android: enabled = ["true" | "false"]
Android: hasCode = ["true" | "false"]
Android: hardwareAccelerated = ["true" | "false"]
Android: icon = "drawable resource"
Android: killAfterRestore = ["true" | "false"]
Android: label = "string resource"
Android: logo = "drawable resource"
Android: manageSpaceActivity = "string"
Android: name = "string"
Android: permission = "string"
Android: persistent = ["true" | "false"]
Android: process = "string"
Android: restoreAnyVersion = ["true" | "false"]
Android: taskAffinity = "string"
Android: theme = "resource or theme">
......
</Application>
<Activity>
The Declaration label of the Activity component (interface controller component). Every Activity in the Android Application must be declared in the AndroidManifest. xml configuration file. Otherwise, the system will not recognize or execute the Activity. <Activity> common attributes of a tag include: android: name of the Activity class, android: theme of the topic, android: launchMode of the loading mode (see section 2.1.3.4), and android: windowSoftInputMode. For details about other attributes, refer to the Android SDK documentation. In addition, the <activity> tag can contain the <intent-filter> element used for message filtering, and the <meta-data> element used to store predefined data, the following is a syntax example of the <activity> tag.
<Activity android: allowTaskReparenting = ["true" | "false"]
Android: alwaysRetainTaskState = ["true" | "false"]
Android: clearTaskOnLaunch = ["true" | "false"]
Android: configChanges = ["mcc", "mnc", "locale ",
"Touchscreen", "keyboard", "keyboardHidden ",
"Navigation", "orientation", "screenLayout ",
"FontScale", "uiMode"]
Android: enabled = ["true" | "false"]
Android: excludeFromRecents = ["true" | "false"]
Android: exported = ["true" | "false"]
Android: finishOnTaskLaunch = ["true" | "false"]
Android: hardwareAccelerated = ["true" | "false"]
Android: icon = "drawable resource"
Android: label = "string resource"
Android: launchMode = ["multiple" | "singleTop" | "singleTask" | "singleInstance"]
Android: multiprocess = ["true" | "false"]
Android: name = "string"
Android: noHistory = ["true" | "false"]
Android: permission = "string"
Android: process = "string"
Android: screenOrientation = ["unspecified" | "user" | "behind" |
"Landscape" | "portrait" |
"Sensor" | "nosensor"]
Android: stateNotNeeded = ["true" | "false"]
Android: taskAffinity = "string"
Android: theme = "resource or theme"
Android: windowSoftInputMode = ["stateUnspecified ",
"StateUnchanged", "stateHidden ",
"StateAlwaysHidden", "stateVisible ",
"StateAlwaysVisible", "adjustUnspecified ",
"AdjustResize", "adjustPan"]>
......
</Activity>
<Activity-alias>
The Declaration label of the Activity component alias is simply the Activity shortcut. The attribute android: targetActivity indicates the relevant Activity name. Of course, it must be the previously declared Activity. In addition, other common attributes include Activity alias name android: name, alias switch android: enabled, permission control android: permission, etc. In addition, we also need to note that the Activity alias is also an independent Activity and can have its own <intent-filter> and <meta-data> elements. The syntax example is as follows.
<Activity-alias android: enabled = ["true" | "false"]
Android: exported = ["true" | "false"]
Android: icon = "drawable resource"
Android: label = "string resource"
Android: name = "string"
Android: permission = "string"
Android: targetActivity = "string">
......
</Activity-alias>
<Intent-filter> and <action>, <category>, <data>
<Intent-filter> declares the Intent message filter. In the previous section 2.1.3.2, we have provided a detailed introduction to the Intent message in the Android Application Framework, we understand that Intent messages are a very important "adhesive" for Android Application Systems ", <intent-filter> elements can be placed in <activity>, <activity-alias>, <service>, and <receiver ER> element tags, to differentiate the Activity controller, Service, and Broadcast Receiver that can be used to process messages. In addition, we also know that Intent messages also contain several important attributes, such as name, action, data, and category. This also has a certain relationship with the syntax of this tag. For example, <intent-filter> must contain <action> elements, that is, the name used to describe a specific message; <category> A tag is used to indicate the category of the message component that can be processed, that is, the category that the Action complies with. The <data> element is used to describe the data format that the message needs to be processed, we can even use regular expressions to limit the data source. Of course, we still need to learn the specific usage of these elements and tags. The following is a syntax example of the Standard <intent-filter> element tag.
<Intent-filter android: icon = "drawable resource"
Android: label = "string resource"
Android: priority = "integer">
<Action android: name = "string"/>
<Category android: name = "string"/>
<Data android: host = "string"
Android: mimeType = "string"
Android: path = "string"
Android: pathPattern = "string"
Android: pathPrefix = "string"
Android: port = "string"
Android: scheme = "string"/>
</Intent-filter>
<Meta-data>
Used to store pre-defined data, similar to <intent-filter>, <meta-data> can also be placed in the <activity>, <activity-alias>, <service>, and <Cycler> element tags. Meta data usually appears in the form of key-value pairs, and there is no limit on the number, and the data will be put into a Bundle object, in the program, we can use the metaData attribute of the ActivityInfo, ServiceInfo, or even ApplicationInfo object to read. Assume that a <meta-data> element is defined in an Activity. The usage example is as follows.
<Activity...>
<Meta-data android: name = "testData" android: value = "Test Meta Data"> </meta-data>
</Activity>
ActivityInfo info = this. getPackageManager ()
. GetActivityInfo (getComponentName (), PackageManager. GET_META_DATA );
String testData = info. metaData. getString ("testData ");
System. out. println ("testData:" + testData );
<Service>
The Declaration label of the Service component, which is used to define and describe a specific Android Service. The main attributes include: Service class name android: name, Service icon android: icon, Service description android: label and Service Switch android: enabled. For more information about the concepts and usage of Service components, see section 2.1.4.2. The following is a syntax example of the <service> label.
<Service android: enabled = ["true" | "false"]
Android: exported = ["true" | "false"]
Android: icon = "drawable resource"
Android: label = "string resource"
Android: name = "string"
Android: permission = "string"
Android: process = "string">
......
</Service>
<Cycler>
The Declaration label of the Boardcast Receiver component, which is used to define and describe a specific Android broadcast Receiver. Its main attributes are similar to the <service> label: Boardcast Receiver class name android: name, receiver icon android: icon. Receiver description android: label and receiver switch android: enabled. For more information about the concepts and usage of the Boardcast Receiver radio receiver component, see section 2.1.4.3. The following is a syntax example of the <Receiver ER> label.
<Cycler android: enabled = ["true" | "false"]
Android: exported = ["true" | "false"]
Android: icon = "drawable resource"
Android: label = "string resource"
Android: name = "string"
Android: permission = "string"
Android: process = "string">
......
</Cycler>
<Provider> and <grant-uri-permission>
In addition to Activity, Service, and Boardcast uploader, another "four major components" are the Declaration labels of Content Provider. For more information about the concept and usage of content provider components, see section 2.1.4.4. <Provider> in addition to the basic attributes such as android: name, android: icon, and android: label of other components, a tag also provides special attributes that support its functions, such: the content provider ID is android: authorities. The permission ID android: grantUriPermission and specific read and write permissions are granted to the specified URI, such as android: readPermission and android: writePermission. Of course, we still need to learn the specific usage of these attributes. The following is a syntax example of the <provider> tag.
<Provider android: authorities = "list"
Android: enabled = ["true" | "false"]
Android: exported = ["true" | "false"]
Android: grantUriPermissions = ["true" | "false"]
Android: icon = "drawable resource"
Android: initOrder = "integer"
Android: label = "string resource"
Android: multiprocess = ["true" | "false"]
Android: name = "string"
Android: permission = "string"
Android: process = "string"
Android: readPermission = "string"
Android: syncable = ["true" | "false"]
Android: writePermission = "string">
......
</Provider>

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.