Xamarin.Android升级targetSdkVersion至31/33报错求助
问题
将Xamarin.Android项目的targetSdkVersion从30升级至31/33(已将编译目标框架改为Android 13.0),更新NuGet包后仍出现错误,报错信息如下:
错误1
Severity Code Description Project File Line Suppression State Error (my-path).Android\obj\Debug\130\lp\104\jl\AndroidManifest.xml Warning: Namespace 'com.google.firebase.components' used in: AndroidManifest.xml, AndroidManifest.xml. (my-path).Android\obj\Debug\130\lp\116\jl\AndroidManifest.xml Warning: Namespace 'com.google.android.gms.base' used in: AndroidManifest.xml, AndroidManifest.xml. (my-path).Android\obj\Debug\130\lp\103\jl\AndroidManifest.xml Warning: Namespace 'com.google.firebase' used in: AndroidManifest.xml, AndroidManifest.xml. (my-path).Android\obj\Debug\130\lp\102\jl\AndroidManifest.xml Warning: Namespace 'com.google.firebase.icing' used in: AndroidManifest.xml, AndroidManifest.xml. (my-path).Android\obj\Debug\130\lp\117\jl\AndroidManifest.xml Warning: Namespace 'com.google.android.gms.common' used in: AndroidManifest.xml, AndroidManifest.xml. (my-path).Android\obj\Debug\130\lp\119\jl\AndroidManifest.xml Warning: Namespace 'com.google.android.gms.tasks' used in: AndroidManifest.xml, AndroidManifest.xml. android:exported needs to be explicitly specified for element <activity#crc64a3cec488f879dbb8.WebAuthenticationCallbackActivity>. 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. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. Directory 'obj\Debug\130\lp\104' is from 'Xamarin.Firebase.Components.dll'. Directory 'obj\Debug\130\lp\116' is from 'Xamarin.GooglePlayServices.Base.dll'. Directory 'obj\Debug\130\lp\103' is from 'Xamarin.Firebase.Common.dll'. Directory 'obj\Debug\130\lp\102' is from 'Xamarin.Firebase.AppIndexing.dll'. Directory 'obj\Debug\130\lp\117' is from 'Xamarin.GooglePlayServices.Basement.dll'. Directory 'obj\Debug\130\lp\119' is from 'Xamarin.GooglePlayServices.Tasks.dll'.
错误2
Severity Code Description Project File Line Suppression State Error android:exported needs to be explicitly specified for element <service#crc6494e14b9856016c30.PNFirebaseMessagingService>. 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. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
错误3
Severity Code Description Project File Line Suppression State Error android:exported needs to be explicitly specified for element <receiver#crc64df999be563077fc4.BootReceiver>. 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. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
错误提示需为带有intent filter的组件显式指定android:exported属性,但在AndroidManifest.xml中添加该属性并设置为true或false后,仍出现相同错误。当前AndroidManifest.xml内容如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="mypackage" android:versionName="0.1.19" android:versionCode="46"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" /> <application android:icon="@drawable/icon" android:label="myName" android:largeHeap="@bool/largeheap"> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" /> <activity android:name="com.facebook.FacebookActivity" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/facebook_app_name" /> <activity android:name="com.facebook.CustomTabActivity" android:exported="true"> <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:scheme="@string/fb_login_protocol_scheme" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" /> <uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" /> <uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" /> <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" /> <queries> <intent> <action android:name="android.support.customtabs.action.CustomTabsService" /> </intent> </queries> </manifest>
解决方案
这些错误的核心是:你手动修改的是自己的AndroidManifest.xml,但报错里的组件(WebAuthenticationCallbackActivity、PNFirebaseMessagingService、BootReceiver)是由NuGet包自动生成到合并后的Manifest里的,不是你自己写的,所以直接改自己的Manifest没用。按以下步骤解决:
1. 升级相关NuGet包到支持Android 12+的版本
报错里提到的Xamarin.Firebase.*、Xamarin.GooglePlayServices.*这些包,旧版本不会自动添加android:exported属性。找到这些包,升级到最新稳定版——新版本的Xamarin绑定包已经适配了Android 12+的要求,会自动给组件加上android:exported。
2. 强制覆盖合并后的Manifest属性(如果升级包无效)
如果某些包没有更新版本,或者升级后还是报错,可以在自己的AndroidManifest.xml里用tools:node="merge"或tools:replace="android:exported"来强制设置这些组件的属性:
首先,在manifest标签里添加tools命名空间:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal" package="mypackage" android:versionName="0.1.19" android:versionCode="46">
然后,分别添加对应的组件声明,指定android:exported:
- 对于
WebAuthenticationCallbackActivity:
<activity android:name="crc64a3cec488f879dbb8.WebAuthenticationCallbackActivity" android:exported="true" tools:node="merge" />
- 对于
PNFirebaseMessagingService:
<service android:name="crc6494e14b9856016c30.PNFirebaseMessagingService" android:exported="false" tools:node="merge" />
- 对于
BootReceiver:
<receiver android:name="crc64df999be563077fc4.BootReceiver" android:exported="true" tools:node="merge" />
注意:
android:exported的值要根据组件用途来定:如果组件需要接收外部应用的Intent(比如BootReceiver监听系统开机广播),设为true;如果只是内部使用(比如MessagingService),设为false。
3. 清理并重建项目
完成上述操作后,执行以下步骤:
- 右键项目 → 清理
- 删除
obj和bin文件夹 - 重建项目
4. 检查合并后的Manifest
可以在obj\Debug\130\AndroidManifest.xml(对应你的调试版本和目标SDK)里查看合并后的最终Manifest,确认android:exported属性已经正确添加到所有报错的组件上。
内容的提问来源于stack exchange,提问作者patriczi

