Android Manifest file

What is Manifest file?

The manifest defines the structure and metadata of your application and its components.

Your app must declare all its components in this file, which must be at the root of the app project directory.

  • Each Android project includes a manifest file, AndroidManifest.xml, stored in the root of the project hierarchy.
  • In Android Studio new project wizard automatically creates a new manifest file when it creates a new project. You’ll return to the manifest as each of the application components is introduced.
  • Before the Android system can start an app component, the system must know that the component exists by reading the app’s manifest fileAndroidManifest.xml. It includes nodes for each of the following components:
    • Activities, Services
    • Service
    • Content Providers
    • Broadcast Receivers.
  • It uses Intent Filters and Permissions that determines how they interact with each other and other applications.
  • It also offers attributes to specify application metadata (like its icon or theme).
  • The ADT plug-in includes a visual Manifest Editor to manage your manifest, rather than you have to manipulate the underlying XML directly.

Sample AndroidManifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0"
    package="com.example.myapp">

    <!-- Beware that these values are overridden by the build.gradle file -->

<uses-permission android:name=”android.permission.ACCESS_LOCATION”>
</uses-permission>

    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="26" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- This name is resolved to com.example.myapp.MainActivity
             based upon the package attribute -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity" />
    </application>
</manifest>

The manifest does a number of things in addition to declaring the app’s components, such as the following:

  • Identifies any user permissions the app requires, such as Internet access or read-access to the user’s contacts.
  • Declares the minimum API Level required by the app, based on which APIs the app uses.
  • Declares hardware and software features used or required by the app, such as a camera, bluetooth services, or a multitouch screen.
  • Declares API libraries the app needs to be linked against (other than the Android framework APIs), such as the Google Maps library.

Elements of AndroidManifest.xml file

1. <manifest>

The manifest is file’s root element requires an attribute for your app’s package name (usually matching your project directory structure—the Java namespace).

For example, the following snippet shows the root <manifest> element with the package name "com.example.myapp":

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0" >
    ...
</manifest>

2. <application>

  • A manifest can contain only one application node.
  • It uses attributes to specify the metadata for your application (including its title, icon, and theme).
  • It also acts as a container that includes the Activity, Service, Content Provider, and Broadcast Receiver tags used to specify the application components.
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

....
</application>

Components of <application> tag

For each app component that you create in your app, you must declare a corresponding XML element in the manifest file:

  • <activity> for each subclass of Activity.
  • <service> for each subclass of Service.
  • <receiver> for each subclass of BroadcastReceiver.
  • <provider> for each subclass of ContentProvider.

If you subclass any of these components without declaring it in the manifest file, the system cannot start it.

  1. <activity tag>
  • For every activity displayed by your android application, an activity tag is required. You set it using android:name attribute to specify the class name.
  • It must include the main launch Activity and any other screen or dialogs that can be displayed.
  • Each Activity node supports intent-filter tags that specify which Intents will launch the Activity
<manifest ... >
    <application ... >
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2. Icons and labels

  • A number of manifest elements have icon and label attributes for displaying a small icon and a text label, respectively, to users for the corresponding app component.
  • The icon and label that are set in the <application> element are the default icon and label for each of the app’s components (such as all activities).
  • The icon and label that are set in a component’s <intent-filter> are shown to the user whenever that component is presented as an option to fulfill an intent. 
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"

3. Permissions <uses-permission> tag

Android apps must request permission to access sensitive user data. Each permission is identified by a unique label.

  • Uses-permission tags declare the permissions will be presented to the user, to grant or deny, during installation.
  • Permissions are required for many of the native Android services, particularly those with a cost or security implication (such as dialing, receiving SMS, or using the location-based services).
<manifest ... >
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
....
</manifest>

4. Device compatibility

  • The manifest file is also where you can declare what types of hardware or software features your app requires, and thus, which types of devices your app is compatible with.
  • Google Play Store does not allow your app to be installed on devices that don’t provide the features or system version that your app requires.
  1. <uses-feature>
  • The <uses-feature> element allows you to declare hardware and software features your app needs. 
<manifest ... >
    <uses-feature android:name="android.hardware.sensor.compass"
                  android:required="true" />
    ...
</manifest>

2. <uses-sdk>

  • Each successive platform version often adds new APIs not available in the previous version.
  • To indicate the minimum version with which your app is compatible, your manifest must include the <uses-sdk> tag and ts minSdkVersion attribute.
android {
  defaultConfig {
    applicationId 'com.example.myapp'

    // Defines the minimum API level required to run the app.
    minSdkVersion 15

    // Specifies the API level used to test the app.
    targetSdkVersion 28

    ...
  }
}

Related posts