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

Xamarin集成OneSignal编译报错:MessengerCompat重复添加

Fixing "java.lang.IllegalArgumentException: already added : Lcom/google/android/gms/iid/MessengerCompat;" in Xamarin + OneSignal

Hey there, I’ve run into this exact conflict before when setting up OneSignal push notifications in Xamarin.Android projects—let’s break down how to fix it.

What’s causing this error?

This duplicate class error happens because multiple NuGet packages are pulling in the same MessengerCompat class from Google Play Services or Android Support libraries. Typically, OneSignal’s embedded dependencies clash with existing versions of Xamarin.GooglePlayServices.* or Xamarin.Android.Support.* packages in your project. Uninstalling Xamarin.Android.Support.v4 makes things worse because OneSignal actually relies on classes from that package for core functionality.

Step-by-step solutions

1. Align all Google Play Services and Support Library versions

The most reliable fix is to ensure every Google-related NuGet package in your project uses the exact same version:

  • Open your project’s NuGet Package Manager (right-click project → Manage NuGet Packages).
  • Go to the "Installed" tab and look for all packages starting with Xamarin.GooglePlayServices. and Xamarin.Android.Support..
  • Check the version of the Google Play Services packages that OneSignal depends on (you can find this in the OneSignal NuGet package details under "Dependencies").
  • Upgrade/downgrade all your Google Play Services and Support Library packages to match that version. For example, if OneSignal requires Google Play Services 12.0.1, make sure every related package in your project uses 12.0.1.

2. Install OneSignal with ignored dependencies (then manually add matched libraries)

If you want more control over your dependencies, install OneSignal without its bundled libraries, then add compatible versions manually:

  • Open the Package Manager Console (Tools → NuGet Package Manager → Package Manager Console).
  • Run this command to install OneSignal while skipping its dependencies:
    Install-Package OneSignal -IgnoreDependencies
    
  • Next, install the specific versions of Xamarin.GooglePlayServices.Base, Xamarin.GooglePlayServices.Gcm, and Xamarin.Android.Support.v4 that match your project’s existing library versions (or pick a consistent version that works for all).

3. Clean and rebuild your project (clear cached conflicts)

Sometimes old build artifacts cause false duplicate errors:

  • Right-click your project → Clean.
  • Navigate to your project’s root folder and delete the bin and obj directories.
  • Restart Visual Studio, then rebuild the project from scratch.

4. Check for conflicting push notification libraries

If you have other push-related packages (like Firebase Messaging) installed alongside OneSignal, they might be pulling in duplicate Google library classes. Remove any unused push libraries to eliminate the conflict.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:52:05