Unable to compile Xamarin.Android project in VS 2022 because of "PE image does not have metadata"

Rob Peterson 11 Reputation points
2022-05-12T19:26:06.903+00:00

I'm updating a web site and associated mobile app. The web site is going from .NET 4.6 to .NET 6. The mobile app is going from Android 8 to Android 12.

In the mobile app I converted the PCL to a .NET6 (.NETStandard) library. I'm trying to update the Xamarin.Android app by moving the project files to a new project created with the "Android Application (Preview)" project template in Visual Studio 2022. I first added all of the NuGet packages from the old Android project, upgrading all of the packages to their latest stable versions and made sure I could compile. Here are the package references:

<PackageReference Include="Acr.UserDialogs" Version="7.2.0.564" />
<PackageReference Include="Chance.MvvmCross.Plugins.UserInteraction" Version="1.1.4" />
<PackageReference Include="Cheesebaron.MvxPlugins.Connectivity" Version="6.0.0" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="4.5.1" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="4.5.1" />
<PackageReference Include="MvvmCross" Version="8.0.2" />
<PackageReference Include="MvvmCross.Droid.Support.Core.UI" Version="6.4.2" />
<PackageReference Include="MvvmCross.Droid.Support.V7.RecyclerView" Version="6.4.2" />
<PackageReference Include="MvvmCross.DroidX.RecyclerView" Version="8.0.2" />
<PackageReference Include="MvvmCross.DroidX.SwipeRefreshLayout" Version="8.0.2" />
<PackageReference Include="MvvmCross.Plugin.Visibility" Version="8.0.2" />
<PackageReference Include="MvvmCross.Plugin.WebBrowser" Version="8.0.2" />
<PackageReference Include="Naxam.BrainTree.Droid" Version="3.15.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Data.SQLite" Version="1.0.115.5" />
<PackageReference Include="Xam.Plugins.Settings" Version="3.1.1" />
<PackageReference Include="Xamarin.Android.Arch.Lifecycle.ViewModel" Version="1.1.1.3" />
<PackageReference Include="Xamarin.Android.Support.Annotations" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Collections" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Compat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Core.UI" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.CursorAdapter" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Loader" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.4.1.1" />
<PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0.13" />
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.4.1.1" />
<PackageReference Include="Xamarin.CardIO.Android" Version="5.5.1" />
<PackageReference Include="Xamarin.Controls.SignaturePad" Version="3.0.0" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.3" />
<PackageReference Include="Xamarin.Firebase.Iid" Version="121.1.0.5" />
<PackageReference Include="Xamarin.Firebase.Messaging" Version="122.0.0.5" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.6.0" />
<PackageReference Include="XamiTextSharpLGPLv2" Version="1.0.0" />

The error occurs once I start adding resources. Specifically, when I add the "Resources\drawable-mdpi" folder from the old project I start getting this error. Here's the partial verbose build output. The name of the Android project is "AndroidApp1" (Let me know if posting some of the 1300+ warnings would help):

Build started...
1>------ Build started: Project: AndroidApp1, Configuration: Debug Any CPU ------
1>C:\Users\peter.nuget\packages\xamarin.build.download\0.11.0\buildTransitive\Xamarin.Build.Download.targets(88,3): warning XBD100: ItemGroup XamarinBuildDownloadRestoreAssemblyAar is no longer supported. If your build is failing, revert to an older Xamarin.Build.Download version, or migrate your packages to use the new XamarinBuildDownloadAndroidAarLibrary ItemGroup. If your build is succeeding it is safe to ignore this warning.
1>C:\Work\Riteload\MRL-Mobile-Carrier\AndroidApp1\MrlCoreProxy\IDatabaseContextProxy.cs(18,90,18,94): warning CS8625: Cannot convert null literal to non-nullable reference type.
...

1>AndroidApp1 -> C:\Work\Riteload\MRL-Mobile-Carrier\AndroidApp1\bin\Debug\net6.0-android31.0\AndroidApp1.dll
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: System.InvalidOperationException: PE image does not have metadata.
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: at System.Reflection.PortableExecutable.PEReader.GetMetadataBlock()
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: at System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(PEReader peReader, MetadataReaderOptions options, MetadataStringDecoder utf8Decoder)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: at Xamarin.Android.Tasks.MonoAndroidHelper.HasMonoAndroidReference(ITaskItem assembly)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: at Xamarin.Android.Tasks.ProcessAssemblies.SetMetadataForAssemblies(List1 output, Dictionary2 symbols)
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: **at
Xamarin.Android.Tasks.ProcessAssemblies.RunTask()
1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.200-preview.14.106\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17**
1>Done building project "AndroidApp1.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

A few odd notes in case they're relevant:

  • I have to build the project twice before getting the error. After the first build the project doesn't recognize the reference to the .NET6 library that was converted from
    the PCL; I get missing reference errors everywhere it's used in code. After the second build, the missing reference errors go away and I get the single "PE image does
    not have metadata" error.
  • The compiler produces a .dll when the error occurs but I get the error again when I try to run it in the VS Android emulator.

Is there actually a problem with the images? Is the drawable-mdpi folder invalid in Android 12? Is this caused by the update?

Thank you in advance.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,272 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,518 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 33,381 Reputation points Microsoft Vendor
    2022-05-13T02:47:06.783+00:00

    Hello,

    First, you need to make sure that your references are support Android 12.

    Is the drawable-mdpi folder invalid in Android 12?

    No, referring to the Android documentation App resources overview, we could find it has no change.

    warning XBD100: ItemGroup XamarinBuildDownloadRestoreAssemblyAar is no longer supported. If your build is failing, revert to an older Xamarin.Build.Download version, or migrate your packages to use the new XamarinBuildDownloadAndroidAarLibrary ItemGroup. If your build is succeeding it is safe to ignore this warning.

    Referring to this issue in GitHub Warning XBD100: XamarinBuildDownloadRestoreAssemblyAar is no longer supported we could find the content written by Redth.

    In almost all cases, the warning from XBD is insignificant. It's mostly caused by the fact GPS/FB basically supports both the newer way of automatically downloading and referencing .aar's directly at build time as well as the old way of embedding them back into the .dll at build time.

    At this point I think it's safe we can remove the old method which unnecessarily causes the warning. I've started a PR here: xamarin/GooglePlayServicesComponents#392

    Cannot convert null literal to non-nullable reference type.

    In the new .NET6 templates, nullable reference types are enabled by default. You can see this if you open the csproj file, the line:

    <Nullable>enable</Nullable>

    That warning is telling you that you are assigning a null to a non-nullable type.

    System.InvalidOperationException: PE image does not have metadata.

    Referring to this GitHub issue dotnet build reporting "PE image does not have metadata" with no details., you could clean and rebuild to fix it.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. ksDev 1 Reputation point
    2022-08-24T19:53:41.903+00:00

    I encountered this "PE image does not have metadata" error while compiling a .NET MAUI app with Visual Studio 17.3.2. It would fail while trying to build the net6.0-android dll .

    1>C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.448\targets\Microsoft.Android.Sdk.AssemblyResolution.targets(98,5): error XAPRAS7009: System.InvalidOperationException: PE image does not have metadata.

    The solution for me was to remove a reference to a class library that uses "System.Data.SQLite.Core and System.Data.SqlClient

    0 comments No comments