marcojak / MauiMTAdmob

MIT License
104 stars 17 forks source link

Compilation error Type androidx.work.CoroutineWorker$startWork$1 is defined multiple times (Solved!) #75

Closed kablammyman closed 2 weeks ago

kablammyman commented 2 months ago

Hello, when I install the MAUI MtAdmob nuget package, I started to get compile time errors for my android build. (i have not tested the iOS version yet)

These errors happen even when I dont have any code to utilize the nuget package.

Im wondering if there is some sort of conflict with the other packages I use, since Im using onesignal for push notifications...and that uses firebase\google code.

Anyway, here is the error I get:

Error   JAVA0000    Error in obj\Release\net8.0-android34.0\lp\194\jl\classes.jar:androidx/work/CoroutineWorker$startWork$1.class:
Type androidx.work.CoroutineWorker$startWork$1 is defined multiple times: obj\Release\net8.0-android34.0\lp\194\jl\classes.jar:androidx/work/CoroutineWorker$startWork$1.class, obj\Release\net8.0-android34.0\lp\213\jl\classes.jar:androidx/work/CoroutineWorker$startWork$1.class
Compilation failed
java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: obj\Release\net8.0-android34.0\lp\194\jl\classes.jar
androidx/work/CoroutineWorker$startWork$1.class
    at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:135)
    at com.android.tools.r8.D8.main(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:5)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: obj\Release\net8.0-android34.0\lp\194\jl\classes.jar:androidx/work/CoroutineWorker$startWork$1.class
    at Version.fakeStackEntry(Version_8.2.33.java:0)
    at com.android.tools.r8.T.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:5)
    at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:82)
    at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:32)
    at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:31)
    at com.android.tools.r8.utils.S0.b(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:2)
    at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:42)
    at com.android.tools.r8.D8.b(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:13)
    at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:40)
    at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:122)
    ... 1 more
Caused by: com.android.tools.r8.utils.b: Type androidx.work.CoroutineWorker$startWork$1 is defined multiple times: obj\Release\net8.0-android34.0\lp\194\jl\classes.jar:androidx/work/CoroutineWorker$startWork$1.class, obj\Release\net8.0-android34.0\lp\213\jl\classes.jar:androidx/work/CoroutineWorker$startWork$1.class
    at com.android.tools.r8.utils.Q2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:21)
    at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:54)
    at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:10)
    at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2056)
    at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6)
    at com.android.tools.r8.graph.m4$a.d(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6)
    at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:61)
    at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:12)
    at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:9)
    at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:45)
    at com.android.tools.r8.D8.d(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:17)
    at com.android.tools.r8.D8.c(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:69)
    at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:28)
    ... 6 more
Directory 'obj\Release\net8.0-android34.0\lp\194' is from 'androidx.work.work-runtime.aar'. LandInvestingApp        1   

Here are the packages in my project:

[net8.0-android34.0]: 
Top-level Package                               Requested   Resolved
> CommunityToolkit.Maui                         8.0.1       8.0.1   
> CommunityToolkit.Maui.Maps                    2.0.1       2.0.1   
> CommunityToolkit.Maui.MediaElement            3.1.0       3.1.0   
> CommunityToolkit.Mvvm                         8.2.2       8.2.2   
> CSCore                                        1.2.1.2     1.2.1.2 
> Geocoding.Microsoft                           4.0.1       4.0.1   
> Microsoft.Extensions.Logging.Console          8.0.0       8.0.0   
> Microsoft.Extensions.Logging.Debug            8.0.0       8.0.0   
> Microsoft.Maui.Controls                       8.0.21      8.0.21  
> Microsoft.Maui.Controls.Compatibility         8.0.21      8.0.21  
> Microsoft.Maui.Controls.Maps                  8.0.21      8.0.21  
> Microsoft.Maui.Essentials                     8.0.21      8.0.21  
> Microsoft.Maui.Extensions                     6.0.553     6.0.553 
> Microsoft.NET.ILLink.Tasks              (A)   [8.0.4, )   8.0.4   
> NAudio                                        2.2.1       2.2.1   
> NAudio.Lame                                   2.1.0       2.1.0   
> Newtonsoft.Json                               13.0.3      13.0.3  
> OneSignalSDK.DotNet                           5.1.2       5.1.2   
> Plugin.LocalNotification                      11.1.2      11.1.2  
> Plugin.Maui.Audio                             2.1.0       2.1.0   
> Plugin.MauiMTAdmob                            1.4.2       1.4.2   
marcojak commented 2 months ago

If it's the same problem, there is an issue in other packages. Try to install these packages to solve your issue:

<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.3.0.1" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.3.0.1" />
kablammyman commented 2 months ago

I was only able to install Xamarin.AndroidX.Collection and Xamarin.AndroidX.Collection.Ktx with their latest version when installing your nuget package. Using 1.3.0.1 versions lead to this warning as error:

Error   NU1605  Warning As Error: Detected package downgrade: Xamarin.AndroidX.Collection from 1.3.0.2 to 1.3.0.1. Reference the package directly from the project to select a different version. 
 LandInvestingApp -> Plugin.MauiMTAdmob 1.4.2 -> Xamarin.AndroidX.Collection (>= 1.3.0.2) 
 LandInvestingApp -> Xamarin.AndroidX.Collection (>= 1.3.0.1)   LandInvestingApp    D:\source\LandInvestingApp\LandInvestingApp.csproj  1       

Anyway, when I had both Xamarin.AndroidX.Collection packages and your MTAdmob package installed, I got the same error when building the code. This is just installing the package, I didnt even need to add any code to my project to get the compiler errors

marcojak commented 2 months ago

My bad...use this:

<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.3.0.2" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.3.0.2" />

The version is important.

In the .net7 I also needed to install the following:

        <PackageReference Include="Xamarin.AndroidX.Activity" Version="1.8.2" />
        <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2" />
        <PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.6.2.3" />

You can see all of them in the Sample projects.

kablammyman commented 2 months ago

Unfortunately using version 1.3.0.2 with your nuget package did not help...same error. here are my packages:

   [net8.0-android34.0]: 
   Top-level Package                               Requested   Resolved
   > CommunityToolkit.Maui                         8.0.1       8.0.1   
   > CommunityToolkit.Maui.Maps                    2.0.1       2.0.1   
   > CommunityToolkit.Maui.MediaElement            3.1.0       3.1.0   
   > CommunityToolkit.Mvvm                         8.2.2       8.2.2   
   > CSCore                                        1.2.1.2     1.2.1.2 
   > Geocoding.Microsoft                           4.0.1       4.0.1   
   > Microsoft.Extensions.Logging.Console          8.0.0       8.0.0   
   > Microsoft.Extensions.Logging.Debug            8.0.0       8.0.0   
   > Microsoft.Maui.Controls                       8.0.21      8.0.21  
   > Microsoft.Maui.Controls.Compatibility         8.0.21      8.0.21  
   > Microsoft.Maui.Controls.Maps                  8.0.21      8.0.21  
   > Microsoft.Maui.Essentials                     8.0.21      8.0.21  
   > Microsoft.Maui.Extensions                     6.0.553     6.0.553 
   > Microsoft.NET.ILLink.Tasks              (A)   [8.0.4, )   8.0.4   
   > NAudio                                        2.2.1       2.2.1   
   > NAudio.Lame                                   2.1.0       2.1.0   
   > Newtonsoft.Json                               13.0.3      13.0.3  
   > OneSignalSDK.DotNet                           5.1.2       5.1.2   
   > Plugin.LocalNotification                      11.1.2      11.1.2  
   > Plugin.Maui.Audio                             2.1.0       2.1.0   
   > Plugin.MauiMTAdmob                            1.4.2       1.4.2   
   > Xamarin.AndroidX.Collection                   1.3.0.2     1.3.0.2 
   > Xamarin.AndroidX.Collection.Ktx               1.3.0.2     1.3.0.2 
marcojak commented 2 months ago

Could you please copy the code to include the packages from the csproj file? So I can quickly include them in a sample project and check the error.

kablammyman commented 2 months ago

Im not sure what you mean, do you need a copy of my csproj file, or are you looking to get my whole project? The whole project cant be shared.

marcojak commented 2 months ago

Just the part of the csproj file with your packages:

like this one:

    <ItemGroup>
...
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.21" />
...
    </ItemGroup>
kablammyman commented 2 months ago

ah okay, here ya go

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.21" />
        <PackageReference Include="CommunityToolkit.Maui" Version="8.0.1" />
        <PackageReference Include="CommunityToolkit.Maui.Maps" Version="2.0.1" />
        <PackageReference Include="CommunityToolkit.Maui.MediaElement" Version="3.1.0" />
        <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
        <PackageReference Include="CSCore" Version="1.2.1.2" />
        <PackageReference Include="Geocoding.Microsoft" Version="4.0.1" />
        <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
        <PackageReference Include="Microsoft.Maui.Controls.Maps" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.553" />
        <PackageReference Include="NAudio" Version="2.2.1" />
        <PackageReference Include="NAudio.Lame" Version="2.1.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="OneSignalSDK.DotNet" Version="5.1.2" />
        <PackageReference Include="Plugin.LocalNotification" Version="11.1.2" />
        <PackageReference Include="Plugin.Maui.Audio" Version="2.1.0" />
        <PackageReference Include="Plugin.MauiMTAdmob" Version="1.4.2" />
    </ItemGroup>
marcojak commented 2 months ago

The incompatibility is between OneSignalSDK.DotNet and Plugin.MauiMTAdmob.

I'm trying to investigate the issue and find a solution

marcojak commented 2 months ago

Solved!

This is a known issues with AndroidX, you need to manually import the following packages (Maybe not all are needed):

    <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
      <PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.9.0.1" />       
      <PackageReference Include="Xamarin.AndroidX.Work.Work.Runtime.Ktx" Version="2.9.0.1" />
        <PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.2" />
        <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
        <PackageReference Include="Xamarin.AndroidX.Activity" Version="1.8.2.1" />
        <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2.1" />
        <PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.7.0.1" />
        <PackageReference Include="Xamarin.AndroidX.Lifecycle.Common" Version="2.7.0.1" />
    </ItemGroup>

Add then to your csproj and the project will compile again.

kablammyman commented 2 months ago

I have this now in my csproj file:

<ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.21" />
        <PackageReference Include="CommunityToolkit.Maui" Version="8.0.1" />
        <PackageReference Include="CommunityToolkit.Maui.Maps" Version="2.0.1" />
        <PackageReference Include="CommunityToolkit.Maui.MediaElement" Version="3.1.0" />
        <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
        <PackageReference Include="CSCore" Version="1.2.1.2" />
        <PackageReference Include="Geocoding.Microsoft" Version="4.0.1" />
        <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
        <PackageReference Include="Microsoft.Maui.Controls.Maps" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.21" />
        <PackageReference Include="Microsoft.Maui.Extensions" Version="6.0.553" />
        <PackageReference Include="NAudio" Version="2.2.1" />
        <PackageReference Include="NAudio.Lame" Version="2.1.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="OneSignalSDK.DotNet" Version="5.1.2" />
        <PackageReference Include="Plugin.LocalNotification" Version="11.1.2" />
        <PackageReference Include="Plugin.Maui.Audio" Version="2.1.0" />
        <PackageReference Include="Plugin.MauiMTAdmob" Version="1.4.2" />
    </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
    <PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.9.0.1" />
    <PackageReference Include="Xamarin.AndroidX.Work.Work.Runtime.Ktx" Version="2.9.0.1" />
    <PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.2" />
    <PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
    <PackageReference Include="Xamarin.AndroidX.Activity" Version="1.8.2.1" />
    <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.8.2.1" />
    <PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.7.0.1" />
    <PackageReference Include="Xamarin.AndroidX.Lifecycle.Common" Version="2.7.0.1" />
  </ItemGroup>

Did I make the proper changes? What I have in my csproj file did not fix the errors.

Am I supposed to uninstall the AndroidX packages in nuget before making these changes to the proj file? I only made the change and didnt uninstall anything.

marcojak commented 2 months ago

Clean your project, remove the bin and obj folders, and make sure that in your csproj you have <TargetFrameworks>...net8.0-android...</TargetFrameworks>

Otherwise adjust the condition of your ItemGroup and that should work

kablammyman commented 2 months ago

Okay, it worked! thank you for looking into this!