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

VS2022更新至17.2.3后Xamarin Firebase报java.exe退出码1编译错误

问题原因

这个编译错误是Visual Studio 2022更新到17.2.x版本后,内置Xamarin.Android构建工具的清单合并逻辑变更导致的:当继承FirebaseMessagingService的子类同时标记[Service]和com.google.firebase.MESSAGING_EVENT类型的[IntentFilter]特性时,构建流程会重复生成服务注册条目,触发dex打包阶段报错,最终返回java.exe exited with code 1。你之前测试移除任意一个特性就能编译通过,本质就是避开了重复注册的触发条件。

修复步骤

按以下顺序操作即可解决:

  • 先移除之前手动添加的全资产排除的Xamarin.Google.Guava包引用,这个错误配置会打断Firebase相关依赖的正常传递,反而会加剧构建冲突。
  • 替换你的推送服务类的特性标记,不要使用无参数的[Service]特性,显式指定服务名称和导出规则,避免构建时自动生成重复的注册条目,参考代码如下:
using System;
using Android.App;
using Firebase.Messaging;
using Xamarin.Essentials;

namespace MyApp.Droid
{
    [Service(
        Exported = false,
        Name = "你的Android应用包名.MyFirebaseMessagingService"
    )]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        private readonly AndroidNotificationManager _androidNotification = new AndroidNotificationManager();
        
        public override void OnMessageReceived(RemoteMessage message)
        {
            var mensajeData = message.Data;
            // 增加空值校验,避免推送无对应自定义字段时触发运行时崩溃
            if (!mensajeData.TryGetValue("notiTitle", out string title) 
                || !mensajeData.TryGetValue("notiBody", out string bodymessage))
            {
                return;
            }
            _androidNotification.CreateLocalNotification(title, bodymessage);
        }

        public override void OnNewToken(string token)
        {
            base.OnNewToken(token);
            Preferences.Set("TokenFirebase", token);
        }
    }
}

代码里的你的Android应用包名替换成你项目实际在AndroidManifest.xml中配置的package属性值即可。

  • 关闭Visual Studio,删除解决方案下所有项目的bin、obj缓存文件夹,重新打开VS还原NuGet包后执行编译。
  • 如果仍有报错,打开Properties/AndroidManifest.xml检查是否存在手动添加的Firebase消息服务注册节点,如果有就删除手动添加的节点——手动注册和特性自动生成的注册重复也会触发同类错误。

不需要回退Visual Studio版本,也不需要降级Firebase相关NuGet包,修正重复注册问题后即可正常编译,推送接收逻辑不受影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 13:57:32