dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.47k stars 4.76k forks source link

Cannot publish android project with ReadyToRun #109004

Open MaxwellDAssistek opened 1 month ago

MaxwellDAssistek commented 1 month ago

Description

When I attempt to publish an Android project (with or without MAUI) using ReadyToRun, I get an exception while building.

Reproduction Steps

  1. Create a new Android app project.
  2. Add the following to the csproj file:
    <RunAOTCompilation>false</RunAOTCompilation>
    <PublishTrimmed>false</PublishTrimmed>
    <PublishReadyToRun>true</PublishReadyToRun>
  3. Run the following command: dotnet publish -f net8.0-android -c Release -p:RuntimeIdentifier=android-arm64

Expected behavior

Publish should complete successfully.

Actual behavior

MSBuild version 17.9.8+b34f75857 for .NET
  Determining projects to restore...
  Restored <snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj (in 279 ms).
  AndroidApp1 -> <snip>\AndroidApp1\AndroidApp1\bin\Release\net8.0-android\android-arm64\  AndroidApp1.dll
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error : Error: Object reference not set to an instance of an object. [<snip>\AndroidApp1\AndroidApp 1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error : System.NullReferenceException: Object reference not set to an instance of an object. [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.ProcessLinkerXmlBase.ProcessXml(Boolean) + 0xd9 [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.Compilation..ctor(DependencyAnalyzerBase`1, NodeFactory, IEnumerable`1, ILProvider, DevirtualizationManager, IEnumerable`1, Logger, InstructionSetSupport) + 0x143 [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.ReadyToRunCodegenCompilation..ctor(DependencyAnalyzerBase`1, NodeFactory, IEnumerable`1, ILProvider, Logger, DevirtualizationManager, IEnumerable`1, String, InstructionSetSupport, Boolean, Boolean, Boolean, Boolean, Func`2, String, Boolean, String, Int32, Boolean, Int32, ProfileDataManager, ReadyToRunMethodLayoutAlgorithm, ReadyToRunFileLayoutAlgorithm, Int32, Boolean) + 0x1b5 [<snip>\AndroidApp1\AndroidApp1\AndroidApp 
1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.ReadyToRunCodegenCompilationBuilder.ToCompilation() + 0x6ac [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.Program.RunSingleCompilation(Dictionary`2, InstructionSetSupport, String, Dictionary`2, HashSet`1, ReadyToRunCompilerContext, Logger) + 0x1d23 [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.Program.Run() + 0x11c6 [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj] 
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(470,5): error :     at ILCompiler.Crossgen2RootCommand.<>c__DisplayClass205_0.<.ctor>b__0(ParseResult result) + 0x3e9 [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]
C:\Program Files\dotnet\sdk\8.0.204\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets(358,5): error NETSDK1096: Optimizing assemblies for performance failed. You can either exclude the failing assemblies from being optimized, or set the PublishReadyToRun property to false. [<snip>\AndroidApp1\AndroidApp1\AndroidApp1.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

.NET version 8.0.204 Windows 10

Other information

No response

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger See info in area-owners.md if you want to be subscribed.

filipnavara commented 1 month ago

.NET on Android uses the MonoVM runtime. R2R is only available with CoreCLR runtime and it's thus NOT available on Android.

However, MonoVM does support ahead of time compilation and profiled AOT.

MaxwellDAssistek commented 1 month ago

@filipnavara I am looking to dynamically load some plugins during runtime, which prevents using AOT, so I was attempting to use Ready To Run to optimize the first-load performance of these plugins.

filipnavara commented 1 month ago

@filipnavara I am looking to dynamically load some plugins during runtime, which prevents using AOT, so I was attempting to use Ready To Run to optimize the first-load performance of these plugins.

MonoVM AOT is technically closer to how CoreCLR ReadyToRun works. It still allows dynamic code. It's NOT the same as NativeAOT that has the restrictions you are mentioning.

MaxwellDAssistek commented 1 month ago

MonoVM AOT is technically closer to how CoreCLR ReadyToRun works. It still allows dynamic code. It's NOT the same as NativeAOT that has the restrictions you are mentioning.

I can't seem to find any clear instructions on how I can apply Mono AOT without Trimming being enforced.