Flutter应用AndroidManifest.xml出现10项报错,请求排查解决
Flutter项目AndroidManifest.xml 10项报错修复指南
逐个报错解析与修复
1. android:requestLegacyExternalStorage="true" - 属性不允许在此处
- 问题根源:该属性仅支持Android 10(API 29)及以上版本,若项目
targetSdkVersion低于29,会触发此报错。 - 修复方法:
- 打开
android/app/build.gradle,将targetSdkVersion设置为29或更高; - 若无需兼容旧版本存储策略,直接移除该属性即可。
- 打开
2. android:name="${applicationName}" - 未解析类{applicationName}
- 问题根源:
${applicationName}是Gradle构建占位变量,需在build.gradle中提前定义,否则IDE无法识别。 - 修复方法:
- 打开
android/app/build.gradle,在defaultConfig块中添加:manifestPlaceholders = [applicationName: "android.app.Application"] - 也可直接替换为默认Application类名:
"android.app.Application"。
- 打开
3. android:icon="@mipmap/ic_launcher" - 属性不允许在此处
- 问题根源:要么项目缺失
@mipmap/ic_launcher图标资源,要么manifest根标签的命名空间配置错误。 - 修复方法:
- 检查
android/app/src/main/res/mipmap-*目录下是否存在ic_launcher.png系列图标,缺失则补充; - 确认manifest根标签命名空间正确:
xmlns:android="http://schemas.android.com/apk/res/android"。
- 检查
4. android:name=".MainActivity" - 未解析类MainActivity
- 问题根源:Flutter未自动生成MainActivity文件,或文件路径与manifest中
package="com.example.buis_talk"不匹配。 - 修复方法:
- 执行
flutter clean后再运行flutter run,让Flutter自动生成MainActivity; - 若仍未生成,手动在
android/app/src/main/kotlin/com/example/buis_talk/(或java目录)创建:package com.example.buis_talk import io.flutter.embedding.android.FlutterActivity class MainActivity : FlutterActivity()
- 执行
5. android:usesCleartextTraffic="true" - 属性不允许在此处
- 问题根源:该属性属于
<application>标签,而非<activity>标签,位置放置错误。 - 修复方法:将此属性移动到
<application>标签内(参考下方完整修复代码)。
6. android:launchMode="singleTop" - 属性不允许在此处
- 问题根源:项目SDK版本过低(低于API 21)不支持该属性,或属性拼写错误。
- 修复方法:
- 打开
android/app/build.gradle,将compileSdkVersion和targetSdkVersion设置为21或更高; - 确认属性拼写正确,
singleTop为合法启动模式值。
- 打开
7. android:theme="@style/LaunchTheme" - 属性不允许在此处
- 问题根源:项目中未定义
LaunchTheme样式,或样式资源路径错误。 - 修复方法:
- 打开
android/app/src/main/res/values/styles.xml,添加该样式:<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:windowBackground">@drawable/launch_background</item> </style> - 确保
launch_backgrounddrawable资源存在于对应目录。
- 打开
8. android:configChanges="..." - 属性不允许在此处
- 问题根源:属性值包含当前SDK版本不支持的配置项,或SDK版本过低。
- 修复方法:
- 将
compileSdkVersion和targetSdkVersion提升至24或更高; - 移除不兼容的配置项,保留合法内容:
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
- 将
9. android:hardwareAccelerated="true" - 属性不允许在此处
- 问题根源:SDK版本低于API 11不支持该属性,或属性放置位置不符合规则(该属性可用于
<activity>或<application>标签)。 - 修复方法:
- 将
targetSdkVersion设置为11或更高; - 确认属性放置在
<activity>标签内,SDK版本达标即可正常使用。
- 将
10. android:windowSoftInputMode="adjustResize" - 属性不允许在此处
- 问题根源:SDK版本低于API 3不支持该属性,或属性拼写错误。
- 修复方法:
- 将
targetSdkVersion提升至3或更高; - 确认属性值拼写正确,
adjustResize为合法值。
- 将
修复后的完整代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.buis_talk"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:requestLegacyExternalStorage="true" android:label="buis_talk" android:name="android.app.Application" android:icon="@mipmap/ic_launcher" android:usesCleartextTraffic="true"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the user while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> </manifest>
最后检查步骤
- 执行
flutter clean清理缓存,再运行flutter pub get同步依赖; - 确认
android/app/build.gradle中compileSdkVersion和targetSdkVersion统一设置为29或更高; - 检查所有资源文件(图标、样式、drawable)均存在于对应目录。
内容的提问来源于stack exchange,提问作者Manhr Chawla
相关产品推荐
相关产品推荐

