Android 12(Target SDK31)签名构建报错:需显式指定android:exported
解决Android 12(Target SDK 31)Manifest合并失败问题
错误信息
生成签名构建时出现以下错误:
Manifest merger failed : android:exported needs to be explicitly specified for element <service#com.marianhello.bgloc.sync.AuthenticatorService>. 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.
项目配置
Android Target SDK: 31
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.newapp.appdriver"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/> <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" /> <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" android:theme="@style/AppTheme"> <uses-library android:name="org.apache.http.legacy" android:required="false"/> <activity android:name=".MainActivity" android:label="@string/app_name" tools:node="merge" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" android:exported="true" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyAqSC9FdXG8r-JAKJS4883jdsd9"/> <!-- Change the value to true to enable pop-up for in foreground on receiving remote notifications (for prevent duplicating while showing local notifications set this to false) --> <meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground" android:value="true"/> <!-- Change the resource name to your App's accent color - or any other color you want --> <meta-data android:name="com.dieam.reactnativepushnotification.notification_color" android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color --> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" /> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" /> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/> </intent-filter> </receiver> <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> </application> </manifest>
Build.gradle
buildscript { ext { buildToolsVersion = "31.0.0" minSdkVersion = 21 compileSdkVersion = 31 targetSdkVersion = 31 ndkVersion = "21.4.7075529" playServicesVersion = "17.0.0" androidMapsUtilsVersion = "2.3.0" googlePlayServicesVersion = "+" // default: "+" firebaseMessagingVersion = "21.1.0" // default: "21.1.0" androidXCore = "1.7.0" } repositories { google() mavenCentral() } dependencies { classpath("com.android.tools.build:gradle:7.1.3") classpath 'com.google.gms:google-services:4.3.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
解决方案
错误根源是com.marianhello.bgloc.sync.AuthenticatorService这个第三方库的Service未显式声明android:exported属性,而Android 12及以上要求所有带intent-filter的组件必须指定该属性。
方案1:升级第三方库
找到项目中引入的react-native-background-geolocation(或提供该Service的对应库),将其版本升级到支持Android 12的最新版本,新版本库会在自身Manifest中添加android:exported属性,无需手动修改。
方案2:手动在Manifest中声明该Service
如果暂时无法升级库,可在自己的AndroidManifest.xml的<application>标签内添加以下代码:
<service android:name="com.marianhello.bgloc.sync.AuthenticatorService" android:exported="false" tools:replace="android:exported" />
android:exported="false":该Service无需被外部应用调用,设为false即可;若业务需要外部调用则设为true。tools:replace="android:exported":确保合并Manifest时,用你设置的属性覆盖库中未指定的配置。
额外优化
检查Manifest中其他未指定android:exported的组件,比如RNPushNotificationActions和RNPushNotificationPublisher这两个Receiver,建议添加android:exported="false"避免后续问题:
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" android:exported="false" /> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" android:exported="false" />
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

