Hi,
I have updated my Xamarin Forms to latest pre 4.5 and now I am getting:
Error CS0433 The type 'Worker' exists in both 'Xamarin.Android.Arch.Work.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'Xamarin.AndroidX.Work.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
Thanks,
Jassim
@jrahma can you include some additional information ?
@PureWeen I don't have a Repo because it's a business project
Here is my BuildOutput
and these are my References
@jrahma if you roll these down to rc1
`
<PackageReference Include="Xamarin.AndroidX.MediaRouter">
<Version>1.1.0-rc3</Version>
</PackageReference>
<PackageReference Include="Xamarin.AndroidX.Work.Runtime">
<Version>2.2.0-rc3</Version>
</PackageReference>
to match forms does that fix ?
@jrahma I believe this is because the Worker type exists in the AndroidX.Work namespace - in BOTH the Xamarin.Android.Arch.Work.Runtime and Xamarin.AndroidX.Work.Runtime packages. This is unfortunately a result of Google doing something weird and including AndroidX types in Support.
But, there may be a few things we can do...
The first is to see if you can just remove the Xamarin.Android.Arch.Work.Runtime package reference. It may be that you just added that but aren't using it right now. You can use the newer AndroidX Worker and related types. This is the best case as it might just be that you have both packages.
But, if there is some dependency that actually requires the Xamarin.Android.Arch.Work.Runtime because of reasons, then we can do a bit more. The first is, if it is an internal package/project, then it might be best to see if you can first update that. The Work package for Support was added and then deprecated in the span of a few months. Just use the AndroidX ones.
If it is an external package that you have no control over, then there is one thing that you _may_ be able to get away with. Just update the reference to:
<PackageReference Include="Xamarin.Android.Arch.Work.Runtime">
<Version>1.0.0.3</Version>
<ExcludeAssets>all</ExcludeAssets>
</PackageReference>
What this is doing is basically saying that NuGet should install the package to satisfy dependencies, but not actually do anything with it. This means that NOTHING from the nuget will even get loaded - and this is what we want. MSBuild must not see that package at all. When you build, the compiler will use the AndroidX version, and then update all references from support to AndroidX. Then, at runtime, all will be working.
Let me know if this helps or works.
@mattleibow Removing the Xamarin.Android.Arch.Work.Runtime still causing errors.
Here is my Build Output
Regarding updating Xamarin.Android.Arch.Work.Runtime to 1.0.0.3, it was already on this version when I am getting the above errors.
Xamarin.AndroidX.Work.Runtime
@PureWeen
Did not solve the problem
@jrahma The package Xamarin.Android.Arch.Work.Runtime has been replaced with Xamarin.AndroidX.Work.Runtime and therefore I believe you should remove:
<PackageReference Include="Xamarin.Android.Arch.Work.Runtime">
<Version>1.0.0.3</Version>
</PackageReference>
from your .csproj. There is a note in the documentation about this:
Note: WorkManager classes are already in the androidx.work package, but currently depend on non-AndroidX dependencies including the Support Library 27.1 and associated Arch component versions. A version of WorkManager with AndroidX dependencies will be released in the future.
Since the 2.0 and above version of AndroidX.Work is AndroidX compatible, you should be able to just remove the old Xamarin.Android.Arch.Work.Runtime reference from your project.
For example, here is the old package:

Notice there are android.support dependencies in it.
Here is the new package:

This has all the androidx dependencies instead.
@JonDouglas even after removing the X I still get errors

and this is the Project file:
and BuildOutput:
@jrahma Can you provide the build output? I see two project files. Additionally we will need to know where Xamarin.Android.Arch.Work.Runtime is coming from. There's another package most likely implicitly bringing it into your project.
@jrahma
https://docs.microsoft.com/en-us/visualstudio/ide/how-to-view-save-and-configure-build-log-files?view=vs-2019#to-change-the-amount-of-information-included-in-the-build-log
If you can set that to diagnostic and give the build output that would be helpful
Or from a command line if you could do
msbuild <yoursln> /t:Rebuild /bl
that will generate an msbuild.binlog file you can attach
Sorry, Updated it above
This is where the error comes:

and this is the AthanTime.cs:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using AndroidX.Work;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Plugin.LocalNotification;
using Xamarin.Essentials;
namespace Jafaria.Droid
{
public class AthanTime : Worker
{
public bool has_value = false;
DateTime fajer_time;
DateTime shurooq_time;
DateTime dhuhur_time;
DateTime maghreb_time;
public AthanTime(Context context, WorkerParameters workerParameters) : base(context, workerParameters)
{
}
public override Result DoWork()
{
Android.Util.Log.Debug("AthanWorkRequest", "DoWork Started");
try
{
PrayerTimesUpdater prayerTimeUpdater = new PrayerTimesUpdater();
prayerTimeUpdater.GetPrayerTimes(true);
Analytics.TrackEvent("PeriodicWorkRequest", new Dictionary<string, string>
{
{ "DoWork Started", DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") }
});
if (Jafaria.PrayerTimesUpdater.location_error == false)
{
Android.Util.Log.Debug("AthanWorkRequest", "location_error is false");
Task.Run(async () =>
{
fajer_time = DateTime.ParseExact(await SecureStorage.GetAsync("Fajer"), "HH:mm", CultureInfo.InvariantCulture);
shurooq_time = DateTime.ParseExact(await SecureStorage.GetAsync("Shurooq"), "HH:mm", CultureInfo.InvariantCulture);
dhuhur_time = DateTime.ParseExact(await SecureStorage.GetAsync("Dhuhur"), "HH:mm", CultureInfo.InvariantCulture);
maghreb_time = DateTime.ParseExact(await SecureStorage.GetAsync("Maghreb"), "HH:mm", CultureInfo.InvariantCulture);
}).Wait();
NotificationCenter.Current.Cancel(999);
NotificationCenter.Current.Cancel(100);
NotificationCenter.Current.Cancel(200);
NotificationCenter.Current.Cancel(300);
Android.Util.Log.Debug("AthanWorkRequest", "NotificationCenter Cancelled");
// TEST Push Notification
var test_notification = new NotificationRequest
{
NotificationId = 999,
Title = "App Started",
Description = "Test Only"
};
NotificationCenter.Current.Show(test_notification);
Android.Util.Log.Debug("AthanWorkRequest", "test_notification Shown");
// Fajer Push Notification
var fajer_notification = new NotificationRequest
{
NotificationId = 100,
Title = "صلاة الفجر",
Description = "حان الآن موعد صلاة الفجر",
// ReturningData = "Dummy data", // Returning data when tapped on notification.
Sound = Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android ? "Adhan" : "Adhan.mp3",
NotifyTime = fajer_time // Used for Scheduling local notification, if not specified notification will show immediately.
};
NotificationCenter.Current.Show(fajer_notification);
Android.Util.Log.Debug("AthanWorkRequest", "fajer_notification Scehduled at " + fajer_time.ToString());
// Dhuhur Push Notification
var dhuhur_notification = new NotificationRequest
{
NotificationId = 200,
Title = "صلاة الظهر",
Description = "حان الآن موعد صلاة الظهر",
// ReturningData = "Dummy data", // Returning data when tapped on notification.
Sound = Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android ? "Adhan" : "Adhan.mp3",
NotifyTime = dhuhur_time // Used for Scheduling local notification, if not specified notification will show immediately.
};
NotificationCenter.Current.Show(dhuhur_notification);
Android.Util.Log.Debug("AthanWorkRequest", "dhuhur_notification Scehduled at " + dhuhur_time.ToString());
// Maghreb Push Notification
var maghreb_notification = new NotificationRequest
{
NotificationId = 300,
Title = "صلاة المغرب",
Description = "حان الآن موعد صلاة المغرب",
// ReturningData = "Dummy data", // Returning data when tapped on notification.
Sound = Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android ? "Adhan" : "Adhan.mp3",
NotifyTime = maghreb_time // Used for Scheduling local notification, if not specified notification will show immediately.
};
NotificationCenter.Current.Show(maghreb_notification);
Android.Util.Log.Debug("AthanWorkRequest", "maghreb_notification Scehduled at " + maghreb_time.ToString());
Analytics.TrackEvent("PeriodicWorkRequest", new Dictionary<string, string>
{
{ "Done Scheduling all Notifications", DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") }
});
Analytics.TrackEvent("PeriodicWorkRequest", new Dictionary<string, string>
{
{ "fajer_time", fajer_time.ToString() },
{ "dhuhur_time", dhuhur_time.ToString() },
{ "maghreb_time", maghreb_time.ToString() }
});
return Result.InvokeSuccess();
}
}
catch (PermissionException ex)
{
Crashes.TrackError(ex, new Dictionary<string, string>
{
{ "Page", "AthanTime" }
});
}
catch (Exception ex)
{
Crashes.TrackError(ex, new Dictionary<string, string>
{
{ "Page", "AthanTime" }
});
}
Analytics.TrackEvent("PeriodicWorkRequest", new Dictionary<string, string>
{
{ "Status", "InvokeFailure" }
});
return Result.InvokeFailure();
}
/*
public double CalculateTaxes()
{
return 2000;
}
*/
}
}
diagnostic
@PureWeen above BuildOutput is already set to diagnostic
This might be caused by https://www.nuget.org/packages/Plugin.LocalNotification/ which brings in the Xamarin.Android.Arch.Work.Runtime package. I believe it will need to update to the AndroidX variant.
@jrahma alright try this :-)
I was able to reproduce your issue and then get it compile
1) make sure you aren't referencing Xamarin.Android.Arch.Work.Runtime at all
2) Then setup your Plugin.LocalNotifications packref to be
<PackageReference Include="Plugin.LocalNotification" ExcludeAssets="compile">
<Version>4.1.3</Version>
</PackageReference>
Once/if you get that working then I would recommend pruning down your csproj file a bit. Just remove all the support/androidx/playservices that you can to get it down to compiling
It's ideal when anything transitive is just brought in transitively
Yes, Updating the PluginLocalNotification fixes the Worker's errors
Thank you
@jrahma great to hear!!
Thank you for getting back to us on this so quickly
@jrahma so looking a bit deeper into this it looks like my suggestion to you was incorrect and now it's just not including the LocalNotifications package at all
I've logged an issue with Plugin.LocalNotifications to look at updating to androidx and we're also looking to see if we can workaround this issue ourselves https://github.com/xamarin/AndroidX/issues/55
For now I would recommend targeting Android 9.0 instead of 10.0 and once any of the above resolutions are finished then you can switch back to 10.0