Xamarin-android: Issue with .net standard 2.1 due lack of MaybeNullAttribute in xamarin android

Created on 10 Oct 2019  路  5Comments  路  Source: xamarin/xamarin-android

Steps to Reproduce

Create a xamarin android project which references a .net standard 2.1 library. At that library write following code:

    public class MyClass
    {
        public static void MyMethod()
        {
            MaybeNullAttribute maybeNullAttribute = new MaybeNullAttribute();
        }
    }

Then call MyClass.MyMethod() in your xamarin android project and you'll receive the following exception:

System.TypeLoadException: 'Could not resolve type with token 01000143 from typeref (expected class 'System.Diagnostics.CodeAnalysis.MaybeNullAttribute' in assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51')'

XamAndroidNetStandard21Issue.zip

Expected Behavior

It should work without any problem, because I'm using vs 16.4 prev1 and xamarin.android 10.0.99.100
That version of xamarin android comes with mono 6.6 and mono 6.4 declares that it supports .net standard 2.1

Actual Behavior

We can't use any .net standard 2.1 dependent library such as ef core 3 )-:

Version Information

N/A

Log File

N/A

Most helpful comment

@zaratul77, thanks for the additional information! I did a quick check just now with Visual Studio 2019 version 16.3.5 and verified that the code snippet ysmoradi provided no longer showed an error when running on device.

One possibility if you are deploying the app in the Debug configuration is that you might be seeing https://github.com/xamarin/xamarin-android/issues/3784, where the new shared runtime version is not getting deployed to devices correctly. The workaround for that issue is to remove the old shared runtime by hand, for example by navigating to the apps section of the system preferences on the device, selecting the Mono Shared Runtime app, uninstalling it, and then redeploying your project from Visual Studio.

If you are still seeing a similar error in Visual Studio 2019 version 16.3.4 or higher even after manually removing the Mono Shared Runtime, then if you could submit a new issue to describe what you're seeing so the team can take a look, that would be perfect. Thanks in advance!

All 5 comments

Thanks for the report! The fix for this issue will be included in a future Visual Studio 2019 version 16.4 Preview version.

If by chance you also have the current Release version of Visual Studio 2019 version 16.3 installed side-by-side, the latest version 16.3.3 from this week now includes the fix for this issue. (There hasn't yet been a new Preview version published since the fix was committed, but barring any unexpected complications, the fix also will be included in the next Visual Studio 2019 version 16.4 Preview version.)

I will mark this issue as a duplicate of an earlier issue on the same topic. I'll post an update on that item when the new Preview version with the fix is published.

Duplicate of #3723

I have Version 16.3.4. and the bug is still present

@zaratul77, thanks for the additional information! I did a quick check just now with Visual Studio 2019 version 16.3.5 and verified that the code snippet ysmoradi provided no longer showed an error when running on device.

One possibility if you are deploying the app in the Debug configuration is that you might be seeing https://github.com/xamarin/xamarin-android/issues/3784, where the new shared runtime version is not getting deployed to devices correctly. The workaround for that issue is to remove the old shared runtime by hand, for example by navigating to the apps section of the system preferences on the device, selecting the Mono Shared Runtime app, uninstalling it, and then redeploying your project from Visual Studio.

If you are still seeing a similar error in Visual Studio 2019 version 16.3.4 or higher even after manually removing the Mono Shared Runtime, then if you could submit a new issue to describe what you're seeing so the team can take a look, that would be perfect. Thanks in advance!

@brendanzagaeski Can confirm, that this helps.
Notice: Clean and Rebuild your solution! Otherwise the Mono Shared Runtime App might not pushed to your device and your app will crash immediately.

Was this page helpful?
0 / 5 - 0 ratings