Flutter添加Intent Filter后android:exported报错排查求助
android:exported属性不生效问题排查 开发Flutter应用时,为AndroidManifest.xml的MainActivity添加新的intent-filter后,出现如下错误:
android:exported needs to be explicitly specified for element <activity#com.example.myApp.MainActivity>. Apps targeting Android 12 and higher are required to specify an explicit value for
android:exportedwhen the corresponding component has an intent filter defined.
已尝试为包含intent-filter的MainActivity添加android:exported="true"属性,但问题仍未解决,完整的AndroidManifest.xml代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.myApp"> <application android:label="myApp" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:label="@string/myApp"> android:exported="true" android:launchMode="singleTop" android:taskAffinity="" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" tools:ignore="MissingClass"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <!-- Intent Filter for launching the app --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- Intent Filter for App Links --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="my-app.com" android:scheme="app" /> </intent-filter> </activity> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> <!-- Required to query activities that can process text --> <queries> <intent> <action android:name="android.intent.action.PROCESS_TEXT" /> <data android:mimeType="text/plain" /> </intent> </queries>
问题原因
XML语法错误导致属性未被系统识别:你在定义<activity>标签时,第一行末尾错误使用了>闭合标签,导致后续的android:exported="true"变成了<activity>的子元素,而非标签本身的属性。Android系统无法读取这个位置的属性,因此仍判定android:exported未被显式指定。
修复方法
将<activity>标签第一行末尾的>改为,把android:exported="true"移到
<activity android:name=".MainActivity" android:label="@string/myApp" android:exported="true" android:launchMode="singleTop" android:taskAffinity="" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" tools:ignore="MissingClass">
内容的提问来源于stack exchange,提问作者Lady_Hangaku

