Testing and debugging your application is an important part of the Android app development process. It lets you – the developer – resolve errors, bugs, and other execution issues before releasing the application to the outside world.

Each time your application experiences an error, Android Studio creates an error message and displays it on its Logcat Monitor. These error messages contain all the data you need to resolve the error and get your application working again — you simply need to know how to decode them!

In this article, we will investigate the five most common Android Manifest error messages that you may experience when building up any Android application. We’ll investigate what the messages in this class of errors imply, analyze the possible reasons for these errors, and examine the solutions to resolve them.

But first, what exactly is the Android Manifest, and why is it so important?

An Overview of Android Manifest

Every app project must have an AndroidManifest.xml file (with precisely that name) at the root of the project source set. 

The Android Manifest is an XML file that contains important metadata about the Android app. This metadata includes the package name, activity names, main activity (the entry point to the app), Android version support, hardware features support, permissions, and other configurations.

For more information about the Android Manifest file, see the Android Developer documentation on Android Manifests.

Possible Manifest Errors

Here are the five most common errors that may arise thanks to an improperly declared app manifest:

1. Tag <manifest> attribute package has invalid character ‘ ‘ 

This error occurs when the package name is declared with an illegal character. Consider the following snippet from an application’s AndroidManifest.xml file:

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

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

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

        <application
            android:name="com.example.applozic.test2.app.AppController"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.applozic.test2.LoginActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop"
                android:windowSoftInputMode="adjustPan" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.applozic.test2.RegisterActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop"
                android:windowSoftInputMode="adjustPan" />
            <activity
                android:name="com.example.applozic.test2.MainActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop" />
            <activity
                android:name="com.example.applozic.test2.UploadToServerActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop" />
        </application>

    </manifest>

This error occurs due to a space in the package name in line 3:

package="com.exampl e.applozic.test2"

After removing space:

package="com.example.applozic.test2"

Similarly, this error could occur due to the presence of characters that are not recognized by the Java package naming convention.

2. ActivityNotFoundException

An ActivityNotFoundException occurs when a call to startActivity(Intent) fails because the activity can’t execute the given intent.

The most common cause of an ActivityNotFoundException is forgetting to declare or improperly declaring an activity in your manifest. To solve this error, check that you’ve declared all your activities in your AndroidManifest.xml file. You should also check that you’ve declared each activity correctly, using either a fully qualified class name or a full stop as a shorthand for the package name. For example, both of the following are valid:

<activity android:name=".MainActivity">

<activity android:name="com.applozic.demo.MainActivity">

However, the following declaration is incorrect, as two activities cannot be combined in this manner:

<activity
        android:name=".MainActivity"
        android:name=".DisplayMessageActivity"

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The correct way to achieve the above is:

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

If you can’t spot any problems with your app manifest, then try to clean and rebuild your project in Android Studio.

3. Parser Exception

A Parser exception implies that Android Studio cannot compile the manifest XML file due to syntax errors. Parse errors happen when the code you’ve written doesn’t conform to the syntax or language rules for the language in which you’re working – which can be either Java or Kotlin.

The following manifest file gives the error: Parser exception for C:\Documents and Settings\XXX\workspace\XXXX\AndroidManifest.xml: Attribute name “android:name” associated with an element type “activity” must be followed by the ‘ = ‘ character.

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 
<activity android:name=".SettingsEditActivity"  
      android:label="@string/app_name"/>  
<activity android:name=".ChooserActivity"  
      android:label="@string/app_name"/>  
<activity android:name=".UploadActivity"  
      android:label="@string/app_name"/>  
<activity android:name=".GameActivity"  
      android:label="@string/app_name"/>  
<activity android:name=".HighScoresActivity"  
      android:label="@string/app_name"/>  
</application>

The error in the above manifest declaration is that there is a closing activity tag before all other activities and the closing tag hasn’t a corresponding opening tag. Here is how a manifest should look.

The corrected manifest:

<manifest>
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity> 
    <activity android:name=".SettingsEditActivity"  
          android:label="@string/app_name"/>  
    <activity android:name=".ChooserActivity"  
          android:label="@string/app_name"/>  
    <activity android:name=".UploadActivity"  
          android:label="@string/app_name"/>  
    <activity android:name=".GameActivity"  
          android:label="@string/app_name"/>  
    <activity android:name=".HighScoresActivity"  
          android:label="@string/app_name"/>  
</application>
</manifest>

4. Security Exception

A SecurityException exception is thrown when the application does not have the permissions required to access a resource. Every Android app runs in a limited-access sandbox. If your app needs to use resources or information outside of its own sandbox, you can declare a permission and set up a permission request that provides this access. 

If you declare any dangerous permissions, and if your app is installed on a device that runs Android 6.0 (API level 23) or higher, you must request the dangerous permissions at runtime 
In the following app manifest, even though android.permission.SEND_SMS is requested using the proper <uses-permission> syntax, a java.lang.SecurityException: Sending SMS message: uid 10092 does not have android.permission.SEND_SMS is thrown.

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-feature android:name="android.hardware.telephony"
    android:required="true"/>

<application
    android:exported="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The main reason why your app may not be not getting a certain permission is could be because your project has a targetSdkVersion of 23 or higher, and the permission that you are requesting is “dangerous”. For these permissions, not only does your targetSdkVersion 23+ app need to have the <uses-permission> element(s), but you also have to ask for those permissions at runtime from the user on Android 6.0+ devices, using methods like checkSelfPermission() and requestPermissions(). The Android documentation has a page dedicated to this topic.

5. Manifest Merger failed

Your application’s APK file can contain just one AndroidManifest.xml file, but your Android Studio project may contain several—provided by the main source set, build variants, and imported libraries. So when building your app, the Gradle build merges all manifest files into a single manifest file that’s packaged into your APK.

One of the most common reasons behind manifest merge failures is the coexistence of  AndroidX libraries and support libraries in the app manifest. To solve this problem, it is advisable to migrate the project to AndroidX by following these steps:

  • Add the following to the gradle.properties file:
android.useAndroidX=true
android.enableJetifier=true
  • Add the following tag to the AndroidManifest.xml file:
tools:replace="android:appComponentFactory"
  • Migrating to AndroidX
    • Firstly, update the build tools to v3.2.0, Gradle to v4.6, and the dependent libraries to v28.0.0.
    • Select Refactor → Migrate to AndroidX from the menu bar.

For more information on AndroidX, check out the official Android documentation.

Conclusion

We began this article by discussing how testing and debugging is an important step in the Android app development journey, and delved into the importance of the app manifest and the various errors that can arise from an improperly configured AndroidManifest.xml file. We also examined the possible reasons behind these errors, and how we can resolve them.

If you have any questions or would like to enquire solutions for other app manifest errors, please leave a comment below!

Author

Akash is a Developer Evangelist and an amateur programmer who loves to demystify technology, one article at a time.