AndroidManifest.xml属性报错致程序无法运行,Kotlin新手求助
刚学Kotlin写简易Android程序,卡了5小时启动不了,你的Manifest代码和报错我看了,问题大多是AGP(Android Gradle Plugin)版本与Manifest配置不匹配导致的,下面给你具体解决步骤:
原始报错汇总
Attribute android:versionCode is not allowed here
Attribute android:versionName is not allowed here
Attribute android:protectionLevel is not allowed here
Attribute android:allowBackup is not allowed here
Attribute android:appComponentFactory is not allowed here
Attribute android:dataExtractionRules is not allowed here
Attribute android:extractNativeLibs is not allowed here
Attribute android:fullBackupContent is not allowed here
Attribute android:icon is not allowed here
Attribute android:supportsRtl is not allowed here
Attribute android:theme is not allowed here
Unresolved class 'InitializationProvider'
Attribute android:exported is not allowed here
修正后的AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.my"> <uses-sdk android:minSdkVersion="25" android:targetSdkVersion="33" /> <permission android:name="com.example.my.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION" android:protectionLevel="signature" /> <uses-permission android:name="com.example.my.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION" /> <application android:debuggable="true" android:label="@string/app_name" tools:replace="android:allowBackup,android:appComponentFactory,android:dataExtractionRules,android:extractNativeLibs,android:fullBackupContent,android:icon,android:supportsRtl,android:theme"> <activity android:name="com.example.my.MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 新手简易程序可以直接删掉这个provider节点,省得麻烦 --> <!-- <provider android:name="androidx.startup.InitializationProvider" android:authorities="com.example.my.androidx-startup" android:exported="false" tools:node="remove"> <meta-data android:name="androidx.emoji2.text.EmojiCompatInitializer" android:value="androidx.startup" /> <meta-data android:name="androidx.lifecycle.ProcessLifecycleInitializer" android:value="androidx.startup" /> </provider> --> </application> </manifest>
具体修改说明
1. 移除标签的versionCode和versionName
从AGP 7.0开始,这两个属性必须写在模块的build.gradle.kts(或build.gradle)里,不能放在Manifest中,示例配置:
// build.gradle.kts 中的android块下 defaultConfig { versionCode = 1 versionName = "1.0" }
2. 处理application标签的属性报错
AGP 8.0+要求allowBackup、icon、theme等属性优先在gradle中配置,不想迁移的话,给Manifest添加tools命名空间,然后用tools:replace属性覆盖这些配置即可避免报错。
3. 解决InitializationProvider找不到的问题
这个类属于androidx.startup初始化库,新手简易程序完全不需要自动初始化Emoji或生命周期组件,直接删掉整个<provider>节点就行;如果确实要用,就在build.gradle.kts中添加依赖:
dependencies { implementation("androidx.startup:startup-runtime:1.1.1") implementation("androidx.emoji2:emoji2:1.2.0") }
4. 解决android:exported属性报错
报错是因为AGP版本对组件的exported属性校验严格,确保activity和provider的exported属性设置正确,添加tools命名空间后即可解决,或者直接删掉provider节点(推荐新手这么做)。
内容的提问来源于stack exchange,提问作者Thomas

