xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Updating Xamarin.AndroidX.AppCompat to 1.6.0 causes ReflectionLoadException #15668

Closed MagicAndre1981 closed 1 year ago

MagicAndre1981 commented 1 year ago

Description

Updating Xamarin.AndroidX.AppCompat to 1.6.0 causes following error at startup:

Could not resolve type with token 01000363 from typeref (expected class 'AndroidX.AppCompat.Graphics.Drawable.DrawableWrapper' in assembly 'Xamarin.AndroidX.AppCompat.AppCompatResources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null')

Steps to Reproduce

  1. Update Xamarin.AndroidX.AppCompat to 1.6.0
  2. debug app

Expected Behavior

app starts fine

Actual Behavior

error at startup

Basic Information

Environment

VS2019 16.11.23

Workaround

stay at 1.5.1.1

jfversluis commented 1 year ago

Usually this happens after an upgrade and you need to aggressively do some rebuilds and/or delete the bin & obj folders to clear the cache. Have you tried that?

MagicAndre1981 commented 1 year ago

yes, I did this. I just saw at AndroidX repository there is also a bug report similar to mine

MagicAndre1981 commented 1 year ago

@jfversluis I also tested the new massive nuget updates from PR and issue is the same.

Comparing 1.5.1.1 with 1.6.x shows that the classes are renamed. In 1.6.x Compat was added to class names in AndroidX.AppCompat.AppCompatResource.

_Appcompat

This is why DrawableWrapper is not found any longer as the new name is now DrawableWrapperCompat

MagicAndre1981 commented 1 year ago

The Android Platform implements ClipDrawableWrapper in ShellSearchView.cs from DrawableWrapper.

And because of the renamed class this causes runtime failure. So update this code and require new lib as nuget dependency

sk1llsh0t commented 1 year ago

I'm getting this same error as well.

MagicAndre1981 commented 1 year ago

as mentioned above a class is renamed and XF requires the old class name which triggers this error.

sk1llsh0t commented 1 year ago

It is strange the error only occurs for me when i've set the deployment configuration to release. if it is set to debug, it works.

MagicAndre1981 commented 1 year ago

ok @jfversluis those new AndroidX nugets require now Targetframework 12.0 . This is a massive change in code. Can someone from the team make retarget and nuget androidx bumps?

solomonfried commented 1 year ago

I am targeting Android 12 and still can't build in Release. I have switched back to 1.5.1.1 for the Xamarin.AndroidX.AppCompat and Xamarin.AndroidX.AppCompat.AppCompatResources. Seems to be working

MagicAndre1981 commented 1 year ago

Android 12 is required for building XF with new androidx nugets not your app itself.

solomonfried commented 1 year ago

@MagicAndre1981 Not sure I understand. I am building on VS2022 with Android SDK Platform 33 installed. My Target Android Version for the app is 12.0 API Level 31

Release build fails with Failed to resolve AndroidX.AppCompat.Graphics.Drawable.DrawableWrapper using AppCompat Nuget libraries 1.6.x. But does build with 1.5.1.1

MagicAndre1981 commented 1 year ago

yes, but trying to make XF comaptible with AndroidX.AppCompat.Graphics.Drawable.DrawableWrapper 1.6.x XF itself must target Android12. Currently they build XF against old AndroidX libs and target Android10

solomonfried commented 1 year ago

Got it! So we need to wait for an update. I'll stay on 15.1.1 until we get the XF update

Thanks for the clarification.

sasa-bobic commented 1 year ago

A workaround that so far works for me was to build a custom binding library and add missing (renamed) classes back and repacking this in custom local nuget.

Basically this: https://github.com/sasa-bobic/Xamarin.AndroidX.AppCompat.AppCompatResources/blob/master/Xamarin.AndroidX.AppCompat.AppCompatResources/Additions/DrawableContainer.cs)

And this: https://github.com/sasa-bobic/Xamarin.AndroidX.AppCompat.AppCompatResources/blob/master/Xamarin.AndroidX.AppCompat.AppCompatResources/Additions/DrawableWrapper.cs

See this repo: https://github.com/sasa-bobic/Xamarin.AndroidX.AppCompat.AppCompatResources

donfbecker commented 1 year ago

Downgrading to 1.5.1.1 does not solve the problem for me. I have tried clearing all caches after downgrade, but problem persists.

rvalitov commented 1 year ago

@donfbecker it worked for me. Perhaps you have some nuget packages that have dependency of the ver 1.6.0 of AppCompat and silently selected newer version to be installed?

donfbecker commented 1 year ago

@donfbecker it worked for me. Perhaps you have some nuget packages that have dependency of the ver 1.6.0 of AppCompat and silently selected newer version to be installed?

The NuGet package manager reported that 1.5.1.1 was installed, but I did find some warnings about version requirements of other Xamarin.AndroidX packages that were in use. I had to downgrade the other four AndroidX packages as well.

cncb-gh commented 1 year ago

Will this be addressed soon or do we all need to use the workaround @sasa-bobic posted?

MagicAndre1981 commented 1 year ago

there is a PR to fix the issue and upgrade to Monodroid13.0

AlleSchonWeg commented 1 year ago

Same problem in release mode. Debug works. Problem is that Xamarin.AndroidX.AppCompat >= 1.6.0 is needed for the new "per-app language preferences" in android 13: https://developer.android.com/guide/topics/resources/app-languages#java

takaz-ams commented 1 year ago

@sasa-bobic followed that and updated package then the error changed. Now getting this: Type androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat$1 is defined multiple times

Error in obj\Release\130\lp\132\jl\classes.jar:androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class: Type androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat$1 is defined multiple times: obj\Release\130\lp\132\jl\classes.jar:androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class, obj\Release\130\lp\26\jl\bin\classes.jar:androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class Compilation failed java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: obj\Release\130\lp\132\jl\classes.jar androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class at com.android.tools.r8.internal.Fj.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:98) at com.android.tools.r8.D8.main(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:4) Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: obj\Release\130\lp\132\jl\classes.jar:androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class at Version.fakeStackEntry(Version_3.3.75.java:0) at com.android.tools.r8.internal.Fj.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:75) at com.android.tools.r8.internal.Fj.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:28) at com.android.tools.r8.internal.Fj.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:27) at com.android.tools.r8.internal.Fj.b(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:2) at com.android.tools.r8.D8.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:22) at com.android.tools.r8.D8.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:17) at com.android.tools.r8.internal.Fj.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:85) ... 1 more Caused by: com.android.tools.r8.internal.f: Type androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat$1 is defined multiple times: obj\Release\130\lp\132\jl\classes.jar:androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class, obj\Release\130\lp\26\jl\bin\classes.jar:androidx/appcompat/graphics/drawable/AnimatedStateListDrawableCompat$1.class at com.android.tools.r8.internal.JT.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:14) at com.android.tools.r8.internal.JT.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:22) at com.android.tools.r8.internal.HN.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:26) at com.android.tools.r8.internal.HN.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:10) at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2048) at com.android.tools.r8.internal.HN.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:6) at com.android.tools.r8.graph.B2$a.e(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:4) at com.android.tools.r8.dex.b.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:110) at com.android.tools.r8.dex.b.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:28) at com.android.tools.r8.D8.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:25) at com.android.tools.r8.D8.d(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:606) at com.android.tools.r8.D8.c(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:1) at com.android.tools.r8.internal.Fj.a(R8_3.3.75_b7a6ff6b13548611571508fe72282c9167faa649161ca0013edfc92e19bd7e58:24) ... 5 more Directory 'obj\Release\130\lp\132' is from 'androidx.appcompat.appcompat-resources.aar'

KRA2008 commented 1 year ago

I’m also very interested in a fix for this, but in the meantime the workaround that I found to be easiest was simply to downgrade the Xamarin.AndroidX.AppCompat versions to 1.5.1.1 and then also (by consequence) to target Android 12 rather than 13. This workaround will continue to work for publishing apps to Google Play until the store starts requiring targeting 13 in August of 2023.

siddhant-maurya commented 1 year ago

I am also facing the same error, and our project needs to be updated to Android 13. Can anyone provide a solution please!

MagicAndre1981 commented 1 year ago

as said, Microsoft works on it and already has a PR

BillyMartin1964 commented 1 year ago

Xamarin.AndroidX.AppCompat

It's not that it works in debug. It works with Linker set to none.

ivaldiviah commented 1 year ago

Downgrading Xamarin.AndroidX.AppCompat to 1.5.1.1 does not solve the problem, clean the proyect and delete bin and obj folder does not solve the problem...

In Debug mode, works perfectly, the problem is only in release mode, i have de links options to none and persist de error.

Has anyone found any solution?

mos379 commented 1 year ago

@ivaldiviah just use pre release of xamarin.forms and latest appCompat. then it should work.

dejanbasic commented 1 year ago

It works for me, when I also downgrade Xamarin.AndroidX.AppCompat.AppCompatResources to 1.5.1.1

https://stackoverflow.com/a/76185857

pulmuone commented 1 year ago

A workaround that so far works for me was to build a custom binding library and add missing (renamed) classes back and repacking this in custom local nuget.

Basically this: https://github.com/sasa-bobic/Xamarin.AndroidX.AppCompat.AppCompatResources/blob/master/Xamarin.AndroidX.AppCompat.AppCompatResources/Additions/DrawableContainer.cs)

And this: https://github.com/sasa-bobic/Xamarin.AndroidX.AppCompat.AppCompatResources/blob/master/Xamarin.AndroidX.AppCompat.AppCompatResources/Additions/DrawableWrapper.cs

See this repo: https://github.com/sasa-bobic/Xamarin.AndroidX.AppCompat.AppCompatResources

Thank you. I solved it with this content.

MagicAndre1981 commented 1 year ago

Thank you. I solved it with this content.

why? Simply use the official XF SR15

pulmuone commented 1 year ago

Thank you. I solved it with this content.

why? Simply use the official XF SR15

Maui (.net7) Android AOT Compile Error. https://github.com/xamarin/xamarin-android/issues/8252#issuecomment-1677395907

MagicAndre1981 commented 1 year ago

ok, this topic is about Xamarin Forms, so it was not clear that you use MAUI