error: unknown element <intent-filter> found after upgrading to android 3.1

ultra greek picture ultra greek · Jun 14, 2018 · Viewed 7.5k times · Source

Today I upgraded to Android Studio 3.1.4 and I have the first error

The option 'android.enableAapt2' is deprecated and should not be used anymore.
Use 'android.enableAapt2=true' to remove this warning.
It will be removed at the end of 2018..

Then I change the android.enableAapt2 to true as it suggests.

After that I have new error AAPT2 error

error: unknown element <intent-filter> found.
Message{kind=ERROR, text=error: unknown element <intent-filter> found., sources=[C:\FILE\Android Studio\UltraGreek\UltraGreekv.4.7\app\build\intermediates\manifests\full\debug\AndroidManifest.xml:37], original message=, tool name=Optional.of(AAPT)}

My manifest in app/srs/main/AndroidManifest.xml is

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ultrasidelogo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

        <activity android:name=".ActivityAbout"/>
        <activity android:name=".WebViewer"/>

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

            <category android:name="android.intent.category.DEFAULT"/>

            <data android:mimeType="text/plain"/>
        </intent-filter>

        <activity
            android:name=".ActivityStats"
            android:label="Στατιστικά">
        </activity>
    </application>

</manifest>

Answer

Zoe picture Zoe · Jun 14, 2018

AAPT2 is the replacement for AAPT. If you google anything related to AAPT2, someone will suggest you disable it. Don't do it. It's deprecated and scheduled for removal, which means you'll get these errors anyways.

For the sake of learning, let's break up the error message. It's formatted as JSON, which means each equal sign marks a new element. These are the two relevant ones:

text=error: unknown element <intent-filter> found.
sources=[C:\FILE\Android Studio\UltraGreek\UltraGreekv.4.7\app\build\intermediates\manifests\full\debug\AndroidManifest.xml:37]

Which means you have an exception in the manifest on line 37 related to an intent-filter. It shows up as an unknown element if the element is in the wrong place. See the migration guide.

Now, I don't have line numbers because Stack Overflow doesn't include them. But if you look inside the application tag, you'll see this:

<activity android:name=".WebViewer"/>

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

    <category android:name="android.intent.category.DEFAULT"/>

    <data android:mimeType="text/plain"/>
</intent-filter>

The problem is that the activity tag is closed, which means it actually looks like this (pseudocode):

<application ...>
    <activity android:name=".WebViewer"/>

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

        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/plain"/>
    </intent-filter>
</application>

<intent-filter> is only allowed under an activity, and (from the migration guide linked earlier):

In previous versions of AAPT, elements nested in incorrect nodes in the Android manifest are either ignored or result in a warning. [...]

Which means wrong nodes now prevent compiling. That's why you're getting the error with AAPT2 activated, but not with AAPT.

The solution is to move <intent-filter> to a supporting node, which means you need to wrap it inside an activity tag. I don't know which one you want it in, so I'm not going to supply any accurate code for this. But the intent-filter tag needs to be inside an activity tag, like this:

<activity android:name="" android:label="">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>

        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>