How to write AndroidManifest.xml?

AndroidManifest.xml is the central configuration file for the android Application. Android Manifest presents essential information about the application to the Android system.There will be only one manifest file per application and it is the required file for any android application placed in its root directory. In my previous articles I have explained about setting up the android environment and how to write a simple android application. This article explores one of the most important component of the android development, writing the  AndroidManifest.xml. If you have any questions, please post it in the comments section.

AndroidManifest.xml contains the following:

  • Declarations for components like Activities, Services, broadcast Receivers, content Providers.
  • Entries for different user permissions.
  • Information about the packages,Libraries needed for the applications.
  • IntentFilters which describes where and when the activity needs to be started.
  • Minimum API level required for the application

Below is the list of elements that are allowed in a manifest file.

<action>
<activity>
<activity-alias>
<application>
<category>
<data>
<grant-uri-permission>
<instrumentation>
<intent-filter>
<manifest>
<meta-data>
<permission>
<permission-group>
<permission-tree>
<provider>
<receiver>
<service>
<supports-screens>
<uses-configuration>
<uses-feature>
<uses-library>
<uses-permission>
<uses-sdk>

You cannot add any other elements into the manifest other than the above mentioned elements. The structure of the AndroidManifest.xml is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest>

    <uses-permission />
    <permission />
    <permission-tree />
    <permission-group />
    <instrumentation />
    <uses-sdk />
    <uses-configuration />
    <uses-feature />
    <supports-screens />
    <compatible-screens />
    <supports-gl-texture />

    <application>
        <activity>
            <intent-filter>
                <action />
                <category />
                <data />
            </intent-filter>
            <meta-data />
        </activity>

        <activity-alias>
            <intent-filter>
             . . .
            </intent-filter>
            <meta-data />
        </activity-alias>

        <service>
            <intent-filter>
             . . .
            </intent-filter>
            <meta-data/>
        </service>

        <receiver>
            <intent-filter>
            . . .
            </intent-filter>
            <meta-data />
        </receiver>

        <provider>
            <grant-uri-permission />
            <meta-data />
        </provider>

        <uses-library />
    </application>
</manifest>

Brief explanation of some of the Elements and the attributes that your elements have when an helloWorld application is created.These Elements can have many other additional attributes too.

<manifest> on AndroidManifest.xml

This is the Root element of the AndroidManifest.xml file. It must contain a <application> element in it. This can appear only once in a manifest file.
The sample <manifest> element is as below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="my.android.app"
      android:versionCode="1"
      android:versionName="1.0">

These are the basic attributes that your manifest has when you create a HelloWorld application.

  • xmlns:android : It defines the android Namespace. It should always be set to “http://schemas.android.com/apk/res/android”
  • package : The package name for the application. It should be unique.
  • android:versionCode : Internal version number used to determine whether one version is more recent than the other version. Higher version number indicates the more recent one. It is an Integer. This is not the version number shown to the user.
  • android:versionName : The version number shown to the user. It is set to a raw string or as a reference to a string resource.This is the version number simply displayed to the user. The main internal version is set in android:versionCode.

<application> on AndroidManifest.xml

This is the declaration of the Application. It cantains many subelements like <activity>,<activity-alias>,<service>,<provider>,<receiver>,<uses-library> and has attributes that can effect all the subelements. The sample <application> element is as below:

 <application android:icon="@drawable/icon" android:label="@string/app_name">

The <application> element in the above code contains the following attributes.

  • android:icon : An icon for the application as whole,and the default icon for each of the application’s components. this attribute must be set as a reference to a drawable resource containing the image.
  • android:label : A user-readable label for the application as whole and the default label for each of the application’s components.It is set to a raw string or as a reference to a string resource.

<application> element can contain many other attributes like

  • android:description=”string resource”
  • android:enabled=[“true” | “false”]
  • android:hasCode=[“true” | “false”]

<activity> on AndroidManifest.xml

This is contained in <application> element. This declares an activity.All activities used in an application must be declared inside the <manifest> element using <activity> element. Those which are not listed will not be seen by the system and will never be run.

<activity> element can contain the following subelements,

  •  <intent-filter>
  • <meta-data>

The sample <activity> element is as below.

<activity android:name=".helloWorld"
          android:label="@string/app_name">

android:name : This is the name of the acivity i.e. the name of the class that implements Activity class(subclass of Activity). There is no default name. This name must be specified. The value is set to fully qualified class name(i.e package name followed by the activity name) or if the activity name starts with a period(ex:  .helloworld) then this is appended to the package name specified in the <manifest> element.

The few other attributes of the <activity> element are,

  • android:noHistory
  • android:parentActivityName
  • android:permission
  • android:process
  • android:icon
  • android:launchMode

<service>

This is contained in a <application> element. This declares a Service subclass. Services run in background. All services must be represented by <service> elements in a manifest file.

<service> element can contain below subelements:

  •  <intent-filter>
  • <meta-data>

The syntax of <service> element is as below:

<service android:enabled=["true" | "false"]
         android:exported=["true" | "false"]
         android:icon="drawable resource"
         android:isolatedProcess=["true" | "false"]
         android:label="string resource"
         android:name="string"
         android:permission="string"
         android:process="string" >
    . . .
</service>
  • android:name : The name of the service subclass that implements the service.This should be the fully qualified class name or it can start with a period(ex: .myservice).
  • android:process : The name of the process where the service is to run.
  • android:permission : The permission that an entity must have in order to launch a service or bind to it.

<provider> on AndroidManifest.xml

This element declares a content provider component. This provide access to data managed by an application. All the content providers used in your application must be declared by <provider> element in the manifest file. The content providers which are not declared are not seen by the system.

The content providers of other applications that you use in your applications should not be declared. Only those content providers that are part of your applications must be declared. The syntax is as below:

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>

<receiver> on AndroidManifest.xml

This declares broadcast receiver. Broadcast receiver enables applications to receive intents broadcasted by the system or by other applications even when the other application components are not running.

Broadcast receiver may contain the following subelements:

  • <intent-filter>
  • <meta-data>

Two ways of creating a broadcast receiver:

  1. declare it in a manifest file by using <receiver> element.
  2. create the receiver dynamically in code and register it using Context.registerReceiver() method.

The syntax is as below:

<receiver 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" >
    . . .
</receiver>

<intent-filter> on AndroidManifest.xml

This specifies the types of Intents that an activity,service or a broadcast receiver can respond to. This element must contain a <action> element. The elements <category> and <data> are optional.

IntentFilters describes where and when a parent component should be started. when a parent component wants to perform an action an intent object is created describing what you want to do and what data will be required. Android compares this information with the intent filters that are exposed by various applications and finds the parent component that is most appropriate to handle the request.
The syntax is as below:

<intent-filter android:icon="drawable resource"
               android:label="string resource"
               android:priority="integer" >
    . . .
</intent-filter>

<action> on AndroidManifest.xml

This adds a action to the intent filters. A <intent-filter> element should contain a <action> element failing which no intent objects gets through the filter. The syntax is as below:

<action android:name="string" />

android:name : The name of the action. The value to this attribute is the standard actions defined as constants in the Intent class. Prefix those constants with android.intent.action before assigning it to the attribute.(Ex: android.intent.action.MAIN).

<category>

This adds the category to an itent-filter. The syntax is as below:

<category android:name="string" />

android:name : The name of category. The value to this attribute is the standard categories defined as constants in Intent class(Ex: CATEGORY_LAUNCHER). Prefix this constants with android.intent.category.

Till now we have seen some of the elements for apllication components. Let us now discuss about a few elements for Application Properties.

<uses-permission>

A permission restricts access to a part of the code or data on the device. If an application wishes to access a protected feature by a permission,it must declare that it requires the permission with <uses-permission> element inside a <manifest> element. Permissions are identified by a unique label.
The syntax is as below:

<uses-permission android:name="string" />

android:name : The name of the permission.

These permissions can be:

  • permissions defined by another application
  • permissions defined using <permission> element.
  • one of the standard system permissions like “android:permission.READ_CONTACTS”

<permission>

An application can also protect its own components like activities,broadcast receivers,content providers,services and features of these components  with permissions. A permission is declared with <permission> element.

<uses-sdk>

This is used to specify the API level. API level is an integer.The API level expressed by an application will be compared to the API level of a Android system. The syntax is as below.

<uses-sdk android:minSdkVersion="integer"
          android:targetSdkVersion="integer"
          android:maxSdkVersion="integer" />

The sample AndroidManifest.xml file of a helloWorld application is as below. The above explanations are more detailed about the different components available in the AndroidManifest.xml file. The following listing is the very simple example program for the manifest file. Hope this would be more useful for you to understand it better. If you have any questions, please post it in the comments section.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
package="my.android.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".helloWorld"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Trackbacks

  1. […] How to write AndroidManifest.xml? […]

  2. […] is registered automatically. If you want to know more on Manifest file, please follow the post on Manifest. Your AndroidManifest.xml has the below […]

Speak Your Mind

*