错误:<intent-filter>升级到 Android 3.1 后发现未知元素



今天我升级到Android Studio 3.1.4,我遇到了第一个错误

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..

然后,我按照建议将android.enableAapt2更改为 true。

之后我有新的错误AAPT2错误

error: unknown element <intent-filter> found.
Message{kind=ERROR, text=error: unknown element <intent-filter> found., sources=[C:FILEAndroid StudioUltraGreekUltraGreekv.4.7appbuildintermediatesmanifestsfulldebugAndroidManifest.xml:37], original message=, tool name=Optional.of(AAPT)}

我在app/srs/main/AndroidManifest.xml中的清单是

<?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>

AAPT2是AAPT的替代品。如果您用谷歌搜索与AAPT2相关的任何内容,有人会建议您禁用它。不要这样做。它已弃用并计划删除,这意味着您无论如何都会收到这些错误。

为了学习,让我们分解错误消息。它的格式为 JSON,这意味着每个等号标记一个新元素。这是两个相关的:

text=error: unknown element <intent-filter> found.
sources=[C:FILEAndroid StudioUltraGreekUltraGreekv.4.7appbuildintermediatesmanifestsfulldebugAndroidManifest.xml:37]

这意味着您在第 37 行的清单中有一个与意图过滤器相关的异常。如果元素位于错误的位置,它将显示为未知元素。请参阅迁移指南。

现在,我没有行号,因为堆栈溢出不包括它们。但是如果你看一下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 ...>
<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>只允许在活动下进行,并且(来自前面链接的迁移指南(:

以前版本的 AAPT 中,嵌套在 Android 清单中不正确节点中的元素将被忽略或导致警告。[...]

这意味着错误的节点现在阻止编译。这就是为什么您在激活 AAPT2 时收到错误,但在激活 AAPT 时却没有。

解决方案是将<intent-filter>移动到支持节点,这意味着您需要将其包装在活动标记中。我不知道你想要它,所以我不会为此提供任何准确的代码。但intent-filter标记需要位于活动标记内,如下所示:

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

您可以尝试使用接收器标签:

<application ...>
<activity android:name=".WebViewer"/>
<receiver android:name="">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</receiver>
</application>

发生这种情况是因为我们不会在意图过滤器之前关闭活动选项卡,您确保它会在 intet 过滤器之后

最新更新