如何解读Android Studio合并清单并修复清单合并失败错误
问题描述
将build.gradle中应用的targetSdkVersion从30修改为33、minSdkVersion修改为21后,构建APK时出现错误提示:Manifest merger failed with multiple errors, see logs,需通过分析AndroidManifest.xml及合并后的manifest日志修复问题。
合并Manifest的错误与警告信息
Merge Errors Error: android:exported needs to be explicitly specified for element <activity#id.platinum.android.feature.splash.SplashActivity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. lovpos_f.app main manifest (this file), line 79 Error: android:exported needs to be explicitly specified for element <receiver#com.freshchat.consumer.sdk.receiver.FreshchatReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. lovpos_f.app main manifest (this file) Error: android:exported needs to be explicitly specified for element <receiver#com.freshchat.consumer.sdk.receiver.FreshchatNetworkChangeReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. lovpos_f.app main manifest (this file) Merge Warnings Warning: Element uses-permission#android.permission.BLUETOOTH at AndroidManifest.xml:12:5-68 duplicated with element declared at AndroidManifest.xml:5:5-68 lovpos_f.app main manifest (this file), line 11
AndroidManifest.xml片段
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="id.platinum.android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-feature android:name="android.hardware.camera" android:required="true" /> <application android:name="id.platinum.android.MyApplication" android:allowBackup="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher" android:label="@string/app_name" android:largeHeap="true" android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true" android:theme="@style/AppTheme" android:requestLegacyExternalStorage="true" android:usesCleartextTraffic="true" tools:targetApi="n"> <!-- This meta-data tag is required to use Google Play Services. --> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.gms.ads.AD_MANAGER_APP" android:value="true"/> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> <meta-data android:name="firebase_crashlytics_collection_enabled" android:value="${crashlyticsEnabled}"/> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_logo_white"/> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorPrimary"/> <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/> <provider android:name="androidx.core.content.FileProvider" android:authorities="id.platinum.android.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" tools:replace="android:resource" /> </provider> <service android:name="id.platinum.android.services.notification.FirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT"/> </intent-filter> </service>
解决方案
1. 修复android:exported缺失问题
Android 12(API 31)及以上版本强制要求:所有包含intent filter的组件(Activity、Receiver、Service等)必须显式声明android:exported属性,取值为true或false:
- 若组件需要被其他应用调用(比如启动页SplashActivity,通常带有主入口的intent filter),设置为
android:exported="true" - 若组件仅在当前应用内部使用,设置为
android:exported="false"
具体操作:
- 找到Manifest第79行附近的
SplashActivity声明,添加属性:<activity android:name="id.platinum.android.feature.splash.SplashActivity" android:exported="true"> <!-- 保留原有intent filter内容 --> </activity> - 针对FreshChat的两个Receiver,可在自身Manifest中补充属性,或通过
tools:replace强制覆盖:
也可以尝试升级FreshChat SDK到最新版本,新版本通常已适配Android 12+的<!-- 处理FreshchatReceiver --> <receiver android:name="com.freshchat.consumer.sdk.receiver.FreshchatReceiver" android:exported="false" tools:replace="android:exported" /> <!-- 处理FreshchatNetworkChangeReceiver --> <receiver android:name="com.freshchat.consumer.sdk.receiver.FreshchatNetworkChangeReceiver" android:exported="false" tools:replace="android:exported" />exported要求。
2. 修复重复权限警告
Manifest中重复声明了android.permission.BLUETOOTH(第5行和第12行),直接删除其中一行即可。
内容的提问来源于stack exchange,提问作者Mck
相关产品推荐
相关产品推荐

