DevOps | Cloud | Analytics | Open Source | Programming





How To Use the Android Manifest File and Its Common Settings?



In this post, we will explore How To Use the Android Manifest File and Its Common Settings. The AndroidManifest.xml file is required for every app project and is located at the root of the project source set. The manifest file contains important information about the app for the Android build tools, operating system, and Google Play.

  • The manifest file must declare the components of the app, such as activities, services, broadcast receivers, and content providers, along with their properties and capabilities
  • The manifest file must also declare the permissions that the app needs and any permissions required by other apps to access content from this app
  • The manifest file must specify the hardware and software features required by the app, which affects which devices can install the app from Google Play
Android Studio creates the manifest file and adds most of the essential elements as the app is built, especially when using code templates Let's explore some of the most Common Settings of the Android Manifest File or AndroidManifest.xml file  

1. Various App Component:

  • The manifest file must declare the app's components, such as activities, services, broadcast receivers, and content providers.
  • It must also declare the permissions the app needs to access protected parts of the system or other apps, as well as the hardware and software features the app requires.
  • In Android Studio, the manifest file is created automatically and most of the essential elements are added when using code templates.
  • App permissions must be requested in the manifest file using the element, and the user can approve or reject some permissions at runtime on Android 6.0 (API level 23) and higher.
  • The manifest file can also declare compatibility with certain hardware and software features using the element, and specify the minimum and target API levels using the element.
  • The manifest file follows certain conventions for elements and attributes, such as using the android: prefix for attribute names, repeating elements for multiple values, and using resource values for user-facing strings and icons.
  • Intent filters in the manifest file allow app activities, services, and broadcast receivers to be activated by intents, and allow the system to locate the matching component to handle the intent.
  • App components, such as activities, services, broadcast receivers, and content providers, must be declared in the manifest file using the corresponding XML elements, and must specify the name of the subclass using the full package designation or by using a period followed by the relative package name.
 

  • The use of a leading period in the name attribute is a convenient way to specify the app's namespace, especially when the app has multiple components that are in different packages within the app. It allows you to avoid having to repeat the full package name for each component.
  • It's important to note that the name attribute must always be a fully-qualified name, meaning it must include the package name in addition to the class name. The use of the leading period is simply a shorthand way to specify the app's package name. If the name attribute does not include a period, it is assumed to be a fully-qualified name. If it includes a period but does not begin with a leading period, it is assumed to be a fully-qualified name for a component in a sub-package of the app.
   

2. Permissions Setting:

  • Android apps must request permission to access sensitive user data or system features
  • Permissions are identified by a unique label and declared in the manifest file using the element
  • Beginning with Android 6.0 (API level 23), users can approve or reject some app permissions at runtime, but all permission requests must still be declared in the manifest
  • Apps can also protect their own components with permissions and can use any of the permissions defined by Android or define their own with the element
  • If a permission is granted, the app can use the protected feature; if not, attempts to access the feature will fail
  The code below is an example of how to declare multiple permission requests in the Android manifest file. The element is used to request a specific permission. In this case, the app is requesting the following permissions:

  • android.permission.INTERNET: Allows the app to access the internet
  • android.permission.FOREGROUND_SERVICE: Allows the app to create and use a foreground service
  • android.permission.READ_EXTERNAL_STORAGE: Allows the app to read from external storage
  • android.permission.GET_TASKS: Allows the app to get information about the currently running tasks

<manifest ... > 

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND\_SERVICE" />
<uses-permission android:name="android.permission.READ\_EXTERNAL\_STORAGE" />
<uses-permission android:name="android.permission.GET\_TASKS" />

</manifest>

 

3. Compatibility with Android Devices :

The manifest file declares the hardware and software features required by the app, which affects its compatibility with different devices

  • - This element allows you to declare hardware and software features that the app needs, and the android:required attribute specifies whether the feature is required for basic functionality
The below code is an example of how to declare a hardware feature that the app requires in the Android manifest file. The element is used to declare hardware and software features that the app needs. In this case, the app is requesting the android.hardware.camera feature, which allows the app to access the device's camera. It's important to note that this is just an example and you would need to include other elements in the manifest file to fully describe your app. You can also include multiple elements to request multiple hardware and software features.


<uses-feature android:name="android.hardware.camera"/>

  • - This element specifies the minimum and target API levels required by the app. The manifest file must include the tag and its minSdkVersion attribute. But these values can be overridden by properties in the build.gradle file. So if you're using Android Studio, you must specify the minSdkVersion and targetSdkVersion values there instead
 


android {
  defaultConfig {
    applicationId 'com.example.mobileapp'

   // Minimum API level required to run the app.
   minSdkVersion 21

   // For Testing API level used.
   targetSdkVersion 33

   }
}

 

4. Manifest File Rules :

The manifest file consists of elements and attributes that follow certain conventions and rules.

Elements :

  • The Android manifest file consists of elements and attributes that provide information about the app to the Android build tools, the Android operating system, and Google Play.
  • The and elements are required and must each occur only once, while most other elements can occur zero or more times. However, some elements are necessary for the manifest file to be useful.
  • All values in the manifest file are set through attributes, rather than as character data within an element.
  • Elements at the same level are usually not ordered, but there are two exceptions: the element must follow the element it is an alias for, and the element must be the last element inside the element.
 

Multiple Values :

  • In the Android manifest file, if an element can have multiple values, it is generally repeated rather than listing multiple values within a single element.
  • This is done to make it easier to read and understand the manifest file.
  • For example, an element can list several actions using the element, as shown in the following example. In this example, the element is repeated for each element, rather than listing all the actions within a single element. This makes it easier to understand the purpose of each element.

<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>

 

String Values :

  • In the Android manifest file, if an attribute value is a string, you may need to escape certain characters using double backslashes (\).
  • This is necessary because some characters have special meanings in the context of the manifest file and must be escaped in order to be interpreted correctly.
e.g. you can use \n to escape a newline character, and \uxxxx to escape a Unicode character. Here is an example of an attribute value with a Unicode character. In this example, the Unicode character for a sun (U+2600) is escaped using \u2600. When the manifest file is processed, the \u2600 sequence is replaced with the sun symbol. It's important to note that this is just an example, and you may need to escape other characters depending on the context in which the string value is used.


<activity android:label="Hello, \\\\u2600!" ... >

 

Attributes :

  • In the Android manifest file, attributes provide additional information about elements.
  • While technically all attributes are optional, many are required for elements to accomplish their purpose. The reference documentation for each element lists the default values for truly optional attributes.
  • Except for a few attributes of the root element, all attribute names in the manifest file have the android: prefix. This prefix is universal and is generally omitted in the documentation when referring to attributes by name. The android: prefix is used to distinguish the attribute from others that may have the same name. e.g. android:alwaysRetainTaskState
 

Resource Values :

  • In the Android manifest file, some attributes have values that are displayed to users, such as the title of an activity or the app icon.
  • These values may vary based on the user's language or other device configurations, such as screen size or pixel density.
  • To provide alternative values for different device configurations, you can set these values using resources or themes, rather than hard-coding them into the manifest file.
  • Resources are expressed as values with the following format: "@[package:]type/name".
  • The package name is optional if the resource is provided by your app (including if it is provided by a library dependency).
  • The only other valid package name is android, which is used to access resources from the Android framework.
  • The type is the type of resource, such as string or drawable, and the name is the identifier for the specific resource.
  • To apply a value that is defined in a theme, you can use the following format:
    • "?[package:]type/name".
  Below is an example of using a resource to set the icon for an activity:


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

 

5. Labels\Icons :

  • The Android manifest file includes a number of elements that have attributes for displaying icons and labels to users for corresponding app components
  • The icon and label set in a parent element become the default values for all child elements For example, the icon and label set in the element are the default values for all activities in the app
  • The icon and label set in a component's are displayed to the user when that component is an option to fulfill an intent
  • By default, the icon for an is inherited from the parent component (either the or element)
  • However, you may want to change the icon for an to better indicate a unique action in the chooser dialog For more information about using icons and labels in the Android manifest file, see the documentation on Allow Other Apps to Start Your Activity.
 

6. Intent Filters :

  • In Android, activities, services, and broadcast receivers are activated by intents, which are messages that describe an action to be performed and can include data and other instructions
  • When an app issues an intent to the system, the system finds a component that can handle the intent based on intent filter declarations in the app's manifest file
  • The system launches an instance of the matching component and passes the Intent object to it
  • If multiple apps can handle the intent, the user can choose which app to use
  • An app component can have multiple intent filters (defined using the element), each describing a different capability of the component
  Hope this helps.  

Additional Posts you might want to read from this Blog -

   


what is manifest.xml in android ,android manifest file location ,use of manifest file in android ,android manifest example ,how to change android manifest file ,manifest file android ,android manifest activity ,android manifest file location in android studio ,android manifest file and its common settings ,android manifest file ,android manifest file location ,android manifest file in flutter ,android manifest file and its common settings ,android manifest file structure ,android manifest file internet permission ,android manifest file permissions ,android manifest file analysis ,android manifest file in unity ,android manifest file location in unity