You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android项目切换至API 34后遇receiver_exported配置错误

解决Xamarin Android API 34下广播接收器导出属性错误的方案

问题概述

将Xamarin Android项目切换至Android API 34后,触发错误:当接收器未仅针对系统广播注册时,必须指定receiver_exported或receiver_not_exported。当前使用Xamarin.Essentials NuGet包版本为1.8.1,尝试安装预览版1.8.0-pr.2108.1未成功还原,错误持续存在。项目核心代码如下:

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    Instance = this;
    //var options = new FirebaseOptions.Builder()
    //  .SetApplicationId("1:865419492128:android:4d581aa626e7df54093545")
    //  .SetApiKey("AIzaSyDpq3kXLbpzGwrtMsvH3hM3GKvGXQJMhvE")
    //  .SetDatabaseUrl("https://pianodentalclinic-839ce.firebaseio.com")
    //  .SetGcmSenderId("865419492128-vdkjet53af3kurcjsv621g00jgbdk5v5.apps.googleusercontent.com")
    //  .SetStorageBucket("pianodentalclinic-839ce.appspot.com")
    //  .Build();
    Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
    FirebaseApp.InitializeApp(this);
    Rg.Plugins.Popup.Popup.Init(this);

    this.Window.AddFlags(WindowManagerFlags.Fullscreen);
    //ZXing.Net.Mobile.Forms.Android.Platform.Init();
    var config = new FFImageLoading.Config.Configuration()
    {
        VerboseLogging = false,
        VerbosePerformanceLogging = false,
        VerboseMemoryCacheLogging = false,
        VerboseLoadingCancelledLogging = false,
        Logger = new CustomLogger(),
    };
    ImageService.Instance.Initialize(config);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

    DependencyService.Register<ToastNotification>();
    //if(Build.VERSION.SdkInt > BuildVersionCodes.Tiramisu)
    //{
        //Context context = Android.App.Application.Context;
       // IntentFilter intent;
        // BroadcastReceiver broadcastReceiver;
        //ContextCompat.RegisterReceiver(context, consoleCmdReceiver, intent, ContextCompat.ReceiverExported);
        //Context.RegisterReceiver(consoleCmdReceiver, IntentFilter, ReceiverExported);
    //}
    //RegisterReceiver(consoleCmdReceiver, new IntentFilter(Intent.ActionRun));
    ToastNotification.Init(this);
    CachedImageRenderer.Init(true);
    CachedImageRenderer.InitImageViewHandler();

    LoadApplication(new App());

    //string tkn= CrossFirebasePushNotification.Current.Token; 

    //FirebasePushNotificationManager.ProcessIntent(this, Intent);
}

解决步骤

1. 升级Xamarin.Essentials到稳定兼容版本

预览版包还原失败通常是因为版本归档或源配置问题,直接升级到Xamarin.Essentials 1.9.0及以上稳定版——这些版本已原生适配Android 13(API 34)的receiver_exported/receiver_not_exported强制要求。

操作:在NuGet包管理器中搜索Xamarin.Essentials,选择最新稳定版安装,确保所有关联项目同步升级。

2. 修复AndroidManifest.xml中的静态接收器配置

若项目通过AndroidManifest.xml手动注册广播接收器,需为<receiver>标签添加android:exported属性:

  • 若接收器需接收外部应用广播,设置android:exported="true"
  • 若仅接收本应用或系统广播,设置android:exported="false"

示例:

<receiver android:name=".YourCustomReceiver" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.YOUR_CUSTOM_ACTION" />
    </intent-filter>
</receiver>

3. 调整代码中的动态注册逻辑

对于代码中动态注册的接收器(如注释中的consoleCmdReceiver),在Android 13及以上版本需使用带导出参数的重载方法:

if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu)
{
    // 根据需求选择RECEIVER_EXPORTED或RECEIVER_NOT_EXPORTED
    RegisterReceiver(consoleCmdReceiver, new IntentFilter(Intent.ActionRun), ContextCompat.RECEIVER_NOT_EXPORTED);
}
else
{
    // 低版本保留原有调用
    RegisterReceiver(consoleCmdReceiver, new IntentFilter(Intent.ActionRun));
}

4. 检查第三方插件兼容性

项目中使用的Firebase、FFImageLoading、Rg.Plugins.Popup等第三方插件,需升级到适配API 34的版本——部分旧版插件内部未处理导出属性要求,会引发相同错误。


内容的提问来源于stack exchange,提问作者Busraaaguler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:22:35