Android Manifest XML file in Android Studio
Description:
The Android Manifest XML is a file that you will find at the root of your project under the name of AndroidManifest.xml and which will allow you to specify different options for your projects, such as the hardware needed to run them, some security settings, or more or less trivial information such as the name of the application and its icon. But that’s not all, it’s also the first step to master in order to be able to insert several activities within the same application.
Android Manifest XML File:
General Syntax of an android manifest file:
This file is essential for all Android projects, which is why it is created by default. If I create a new project, here is the Manifest that is generated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.buttonsdemo"> <uses-sdk android:minSdkVersion="7" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.ButtonsDemo"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Let’s take a look at what it’s all about here.
<manifest> Tag in Android Manifest file:
The root of the Manifest is a tag of type <manifest>. As with views and other resources, we start with show that we are using the android namespace:
1 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
Then, we declare in which package our application is located:
1 | package="com.example.buttonsdemo"> |
in order to be able to directly use the classes which are located in this package without having to specify each time that they are there situate. For example, in our current Manifest, you can see the following line:
android:name=”.MainActivity”. It refers to the main activity of my project: MainActivity. However, if we hadn’t specified package=”com.example.buttonsdemo”, then the android:name tag should have argue android:name=” com.example.buttonsdemo.MainActivity.”. Just imagine we had to for each activity of our application… This could be quickly tiring.
<uses-sdk> Tag in Android Manifest file:
We use this tag so that we can filter the devices on which the application is supposed to work according to their version of Android. Thus, you can indicate the minimum version of the API that the device must use:
1 | <uses-sdk android:minSdkVersion="7" /> |
Here, you will need Android version 2.1 (API 7) or higher to be able to use this application.
There is also an attribute android:targetSdkVersion which designates not the minimum version of Android requested, but rather the version from which we can fully exploit the application. Thus, you may have implemented features that are only available from versions of Android newer than the minimum version filled in with android:minSdkVersion, while making the app functional using any version of Android equal to or greater than specified in android:minSdkVersion.
You can also specify a maximum limit to respect with android:maxSdkVersion if you know that your app doesn’t work on newer platforms, but I don’t recommend it, try rendering instead your application compatible with the greatest number of terminals!
<uses-feature> Tag in Android Manifest file:
Since Android is intended for a very wide variety of different terminals, a way was needed to ensure that applications that use certain hardware aspects can only be offered for phones that have these technical capabilities. For example, you are not going to offer software for taking photographs to a phone that doesn’t have a camera (although that’s rare) or sound capture for a tablet that doesn’t have a microphone (which is already less rare). This tag can take three attributes, but I will present only two:
1 2 3 4 5 | <uses-feature android:name="material" android:required="boolean" /> |
android:name
You can specify the hardware name with the android:name attribute. For example, for the camera (and therefore the camera), we will put:
1 | <uses-feature android:name="android.hardware.camera" /> |
However, it happens that we are not only looking for a particular component but for a functionality of this component; by example to allow the application to be used only on devices that have a camera with autofocus, we will use:
1 | <uses-feature android:name="android.hardware.camera.autofocus" /> |
<supports-screens> Tag in Android Manifest file:
This one is very important, it allows you to define which screen types support your application. This attribute appears so:
1 2 3 4 5 | <supports-screens android:smallScreens="boolean" android:normalScreens="boolean" android:largeScreens="boolean" /> |
If a screen is considered small, then it will fall into the smallScreen category, if it is medium, it is a normalScreen, and large screens are largeScreen.
<application> Tag in Android Manifest file:
Perhaps the most important tag. It describes the attributes that characterize your application and lists the components of your app. By default, your application has only one component, the main activity. But let us first see the attributes of this tag. You can set your app icon with android:icon, for that you need to refer to a resource drawable: android:icon=”@drawable/ic_launcher”.
There is also an android:label attribute that allows us to define the name of our application.
Themes attribute in Application tag:
You already know how to style multiple views so that they respect the same attributes. What if we wanted that all our views respect the same style within an application? That the texts of all these views remain black by example! It would be cumbersome to apply the style to each view. This is why it is possible to apply a style to an application, in which case it is called a theme. This operation takes place in the Manifest, you just need to insert the attribute:
1 | android:theme="@style/Theme.ButtonsDemo"> |
You can also use the default themes provided by Android, for example, to make your application look like a dialog box:
1 | <activity android:theme="@android:style/Theme.NoTitleBar"> |
Now let me tell you about the concept of components. These are the elements that will make up your projects. There are five types, but you only know the activities right now, so I’m not going to confuse you any further with that. Know just that your application will ultimately be a set of components that interact with each other and with the rest of the system.
<activity> Tag in Android Manifest file:
This tag is used to describe all the activities contained in our application. As I have already told you, an activity corresponds to a screen of your application, so if you want to have several screens, you will need several activities. The only really necessary attribute here is android:name, which indicates which class is implementing the activity.
You can also specify a name for each activity with android:label, this is the word that will appear at the top of the screen about our activity. If you don’t, it’s the String populated in the android:label of the <application> tag which will be used.
You can see another tag of type <intent-filter> which indicates how this activity will start. For the moment, just know that the activity that will be launched from the main menu of Android will always contain in its Manifest these lines:
1 2 3 4 5 | <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> |
You can also set a theme for an activity like we did for an app.
Permissions Tag in Android Manifest file:
You surely know it, when you download an application on the Play Store, you are offered to look at the authorizations that this application asks for before starting the download. For example, for an application that allows you to retain your bank card number, one can legitimately ask the question of whether in its permissions is “Internet access“.
By default, no application can perform any operation that may harm other applications, the operating system, or the user. However, Android is built in such a way that apps can share. It is the role of permissions, they allow you to limit access to the components of your applications.
Use permissions Tag in Android Manifest file:
In order to be able to use certain Android APIs, such as internet access in the previous case, you must specify in the Android Manifest XML file that you are using permissions. Thus, the end-user is notified of what you want to do, it is a measure of important protection to which you must submit. You will find on this page a list of permissions that already exist. Thus, to request internet access, we will use the following line:
1 | <uses-permission android:name="android.permission.INTERNET" /> |
To access the Bluetooth in the Android Manifest XML file we can write code as:
1 | <uses-permission android:name="android.permission.BLUETOOTH"/> |
To access the Wifi in the Android Manifest XML file we can write code as:
1 | <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> |
To access the Hardware location in the Android Manifest XML file we can write code as:
1 | <uses-permission android:name="android.permission.LOCATION_HARDWARE"/> |
There are many other android Manifest permission that we can use in the android application.
Create user define permissions in Android manifest:
It is important that your application can also share its components since that is how it knows how to essential. Permission should look like this:
1 2 3 4 5 6 7 8 9 | <permission android:name="string" android:label="string resource" android:description="string resource" android:icon="drawable resource" android:protectionLevel=XXX /> |
android:name
the name that will be used in a uses-permission to refer to this permission. This name must be unique.
android:label
the name that will be shown to the user, so makes it pretty self-explanatory.
android:description
this is use for more complete description of this permission than the label.
android:icon
it is pretty self-explanatory, it’s an icon that’s supposed to represent permission. This attribute is fortunately optional.
android:protectionLevel
Indicates the degree of risk of the component linked to this permission. There are mainly three values:
- Normal: if the component is safe and does not risk degrading the behavior of the phone;
- Dangerous: if the component has access to sensitive data or can degrade the operation of the device;
- Signature: to authorize the use of the component only by the products of the same author (specifically, for an application to launch on your terminal or on the emulator, your application must be ” approved” by Android. To do this, a certificate is generated even if you don’t ask for it, the ADT doesn’t automatically occupy and the certificates of the two applications must be identical for the permission is granted).