AndroidManifest.xml: Overview
Beginners usually ignore or forget about the AndroidManifest.xml file but its role in app development is huge. In this blog, we will try to get to know this important AndroidManifest.xml file better.
First, let’s look at what is XML file.
XML stands for eXtensible Markup Language, and it is a markup language used to store and transport data. An XML file is a text file that contains structured data in the form of tags and attributes. It is designed to be both human-readable and machine-readable, making it a popular choice for data exchange and storage. It doesn’t contain any code.
Remember XML File Does Not DO Anything
If an XML file doesn’t do anything, why are we even discussing it?
Yes, XML alone doesn’t do anything in itself but it contains essential information about our app to the Android build tools, Android OS, and Google Play. It acts as the container of information for our build tools to use. It acts like a notebook which is used by the system to build and give information about our app.
As the information is used to build our app, it is crucial to be able to read, write and manipulate this information to step up our app development journey.
AndroidManifest.xml is a required file in the android application at the root of the project source set. Every app project must have an AndroidManifest.xml file at the root of the project source set.
The manifest file can be used to do a ton of things. Some of the basic and most important things the manifest file does are:
- Describes the components of the application
Every app component that we create must be declared in the manifest file otherwise our app won’t recognize that app component and we will be unable to use those app components. App components include Activity, Service, BroadcastReceiver, and ContentProvider. Each app component has a specific tag assigned to it. <activity>, <service>,<receiver> and <provider> tags are assigned for Activity, Service, BroadcastReceiver, and ContentProvider respectively.
2. Declares the Permissions
The app requires permission from the system for some of its features. For example, if the app requires internet access to work properly, it must be declared in the manifest file. Android system has a unique label for each feature for the app to ask permission for.
3. Device Compatibility
In the app manifest, the feature required by the app to function properly is declared and thus only the system having those features available may install the app, thus describing if the app is compatible with the android system or not.
Structure of the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest >
<uses-sdk/>
<uses-permission />
<application>
<activity>
<intent-filter>
<action />
<category />
<data />
</intent-filter>
<meta-data />
</activity>
<service>
<intent-filter> . . . </intent-filter>
<meta-data/>
</service>
<receiver>
<intent-filter> . . . </intent-filter>
<meta-data />
</receiver>
<provider>
<grant-uri-permission />
<meta-data />
<path-permission />
</provider>
</application>
</manifest>
Sample
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyVpnApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<receiver android:name=".ConnectionReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
Let’s look at some of the tags and attributes of the manifest file:
1 . <manifest>
It is the root element of the manifest file. It must contain <application> tag element and specify xmlns:android
and package
attributes.
Attributes
xmlns:android
: This attribute should always be set to “http://schemas.android.com/apk/res/android". When using prefixes in XML, a namespace for the prefix must be defined. Instead of callingandroid:id
, the xml will use ‘http://schemas.android.com/apk/res/android:id ’( it’s a URI Uniform Resource Identifier ) to be unique.package
: It is the unique name for the app. It has to be formatted as a full Java-language-style package name.android:installLocation
: It is used to define the default install location for the app. It can have three values, “internal only”, “auto” and “preferExternal”.
2. <application>
It is used to define the properties and components of an Android application. It declares the application. The components of the app are declared in it.
Attributes
android:label
: It is a user-readable label/name for the app as a whole.android:icon
: It defines the icon for the app as a whole.android:name
: It points to the subclass of the Application class that we created. The subclass is optional, most applications won’t need one. When an application starts, this class is instantiated before any other application component.
3. <uses-permission>
It specifies the system permission that the user must grant for the app to function correctly.
Attributes
android:name
: It is the name of the permission being requested. For example “android.permission.INTERNET” to use the internet.
4. <activity>
It is used to define the activity that we have created. Activity can’t be recognized by the system if it is not defined under this tag. It can contain <intent-filter> ,<meta-data> and <layout>
Attributes
android:name
: It points to the subclass of the Activity class that we created.android:exported
: It contains a boolean value. If “true”, the activity is accessible to any app and is launchable by its exact class name. If “false”, it can only be launched by components of the same application.android:theme
: It is a reference to the style resources for the activity to use.android:configChanges
: It contains a list of configuration changes that the activity will handle. It prevents the activity from restarting when the defined config change takes place as the activity is restarted by default on configuration changes.android:launchMode
: It instructs how the activity should be launched. It has 5 modes. They are:“standard
","singleTop
", "singleTask
", "singleInstance
", and "singleInstancePerTask
".
5. <service>
It is used to declare a Service subclass. All services must be declared by this tag. It can contain <intent-filter>
and <meta-data>
. Service run on background independent of any user interface.
Attributes
android:name
: It points to the subclass of the Service class that we created.
6. <receiver>
It declares the BroadcastReceiver subclass. Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running.
android:name
: It points to the subclass of BroadcastReceiver class that we created.
7. <provider>
It contains a reference to the subclass of ContentProvider subclass. ContentProvider is necessary if we require to share data between multiple apps.
Attributes
android:authorities
: It is the list of the URI that can be used to share data across the application. Multiple authorities are listed by separating their names with a semicolon.android:name
: It points to the subclass of ContentProvider class that we created.android:permission
: It is the permission that the app must have to use it.
8. <intent-filter>
It informs the system about the intent that the app components can respond to. It opens the component receiving intent. It must contain <action>
and may contain <category>
and<data>.
An Intent is a message or a request for the app component to perform some action. An app can have multiple intent filters.
9. <action>
It is the action that the app components can perform and is contained inside <intent-filter>
. For example,<action android:name=”android.intent.action.SEND” /> to send data to another application.
Attribute
android:name
: It is the name of the action to be performed.
10. <category>
It provides additional information on which the app components can perform an action. For example, the <category android:name=”android.intent.category.LAUNCHER” /> indicates that an activity should be listed in the launcher as an entry point for the application while first opening the app.
Attribute
android:name
: It is the name of the category.
There are many other amazing tags and attributes for your need. I only explore the surface. For more detail and to find the tag that you need head over to the android official site .
Thanks for reading