Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Getting (The type 'Worker' exists in both...) with pre release #9546

Closed
jrahma opened this issue Feb 12, 2020 · 17 comments
Closed

Getting (The type 'Worker' exists in both...) with pre release #9546

jrahma opened this issue Feb 12, 2020 · 17 comments
Labels
Android10 i/regression p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛
Milestone

Comments

@jrahma
Copy link

jrahma commented Feb 12, 2020

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 jrahma added s/unverified New report that has yet to be verified t/bug 🐛 labels Feb 12, 2020
@pauldipietro pauldipietro added this to New in Triage Feb 12, 2020
@samhouts samhouts added this to the 4.5.0 milestone Feb 13, 2020
@PureWeen
Copy link
Contributor

@jrahma can you include some additional information ?

  • what packages do you all have installed in your android project?
  • do you have a repro?
  • possibly a build log

@samhouts samhouts added s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. labels Feb 13, 2020
@jrahma
Copy link
Author

jrahma commented Feb 13, 2020

@PureWeen I don't have a Repo because it's a business project

Here is my BuildOutput

BuildOutput.txt

and these are my References

Project.txt

@samhouts samhouts moved this from New to Needs Info in Triage Feb 13, 2020
@PureWeen
Copy link
Contributor

@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 ?

@samhouts samhouts added this to To do in v4.5.0 Feb 13, 2020
@mattleibow
Copy link
Contributor

@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.

@jrahma
Copy link
Author

jrahma commented Feb 14, 2020

@mattleibow Removing the Xamarin.Android.Arch.Work.Runtime still causing errors.

Here is my Build Output

BuildOutput.txt

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.

@jrahma
Copy link
Author

jrahma commented Feb 14, 2020

Xamarin.AndroidX.Work.Runtime

@PureWeen

Did not solve the problem

@JonDouglas
Copy link

@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:

image

Notice there are android.support dependencies in it.

Here is the new package:

image

This has all the androidx dependencies instead.

@jrahma
Copy link
Author

jrahma commented Feb 14, 2020

@JonDouglas even after removing the X I still get errors

AndroidWroker

and this is the Project file:

Project.txt

and BuildOutput:

BuildOutput.txt

@JonDouglas
Copy link

JonDouglas commented Feb 14, 2020

@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.

@PureWeen
Copy link
Contributor

@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

@jrahma
Copy link
Author

jrahma commented Feb 14, 2020

Sorry, Updated it above

This is where the error comes:

WorkerError

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;
        }
        */
    }
}

@jrahma
Copy link
Author

jrahma commented Feb 14, 2020

diagnostic

@PureWeen above BuildOutput is already set to diagnostic

@JonDouglas
Copy link

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.

@PureWeen
Copy link
Contributor

@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

@jrahma
Copy link
Author

jrahma commented Feb 14, 2020

Yes, Updating the PluginLocalNotification fixes the Worker's errors

Thank you

@jrahma jrahma closed this as completed Feb 14, 2020
Triage automation moved this from Needs Info to Closed Feb 14, 2020
v4.5.0 automation moved this from To do to Done Feb 14, 2020
@PureWeen
Copy link
Contributor

@jrahma great to hear!!

Thank you for getting back to us on this so quickly

@PureWeen
Copy link
Contributor

PureWeen commented Feb 14, 2020

@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 xamarin/AndroidX#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

@samhouts samhouts removed this from Closed in Triage Mar 15, 2020
@samhouts samhouts removed this from Done in v4.5.0 May 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android10 i/regression p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests

5 participants