8

I have an .net maui application using .net 7 preview 2 that I want to publish for Android.

My publish command: dotnet publish ${{env.UI_PROJECT_PATH}} -f net7.0-android -c Release

This gives me this error:

C:\Program Files\dotnet\sdk\7.0.100-rc.2.22477.23\Sdks\Microsoft.NET.ILLink.Tasks\build\Microsoft.NET.ILLink.targets(86,5): error NETSDK1144: Optimizing assemblies for size failed. Optimization can be disabled by setting the PublishTrimmed property to false. [D:\a\MoneyFox\MoneyFox\src\MoneyFox.Ui\MoneyFox.Ui.csproj::TargetFramework=net7.0-android]
ILLink : error IL1011: Failed to write 'obj\Release\net7.0-android\android-x64\linked\SkiaSharp.Views.Android.dll'. [D:\a\MoneyFox\MoneyFox\src\MoneyFox.Ui\MoneyFox.Ui.csproj::TargetFramework=net7.0-android]
  Fatal error in IL Linker
  Unhandled exception. Mono.Linker.LinkerFatalErrorException: ILLink: error IL1011: Failed to write 'obj\Release\net7.0-android\android-x64\linked\SkiaSharp.Views.Android.dll'.
   ---> System.ArgumentException: Member 'System.Int32 SKCanvasView_ignorePixelScaling' is declared in another module and needs to be imported
     at Mono.Cecil.MetadataBuilder.LookupToken(IMetadataTokenProvider provider)
     at Mono.Cecil.Cil.CodeWriter.WriteOperand(Instruction instruction)
     at Mono.Cecil.Cil.CodeWriter.WriteInstructions()
     at Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody(MethodDefinition method)
     at Mono.Cecil.Cil.CodeWriter.WriteMethodBody(MethodDefinition method)
     at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method)
     at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type)
     at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
     at Mono.Cecil.MetadataBuilder.AddTypes()
     at Mono.Cecil.MetadataBuilder.BuildTypes()
     at Mono.Cecil.MetadataBuilder.BuildModule()
     at Mono.Cecil.MetadataBuilder.BuildMetadata()
     at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader _)
     at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
     at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
     at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
     at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
     at Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters)
     at Mono.Cecil.AssemblyDefinition.Write(String fileName, WriterParameters parameters)
     at Mono.Linker.Steps.OutputStep.WriteAssembly(AssemblyDefinition assembly, String directory, WriterParameters writerParameters)
     --- End of inner exception stack trace ---
     at Mono.Linker.Steps.OutputStep.WriteAssembly(AssemblyDefinition assembly, String directory, WriterParameters writerParameters)
     at Mono.Linker.Steps.OutputStep.OutputAssembly(AssemblyDefinition assembly)
     at Mono.Linker.Steps.BaseStep.Process(LinkContext context)
     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
     at Mono.Linker.Pipeline.Process(LinkContext context)
     at Mono.Linker.Driver.Run(ILogger customLogger)
     at Mono.Linker.Driver.Main(String[] args)

I tried to include the SkiaSharp.Views.Android Dll so that is not trimmed with this:

    <PropertyGroup>        
        <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
        <PublishTrimmed>true</PublishTrimmed>
        <TrimMode>partial</TrimMode>
    </PropertyGroup>

    <ItemGroup>
        <TrimmerRootAssembly Include="LiveChartsCore.SkiaSharpView.Maui" />
    </ItemGroup>

But it seems that didn't work. I could disable trimming, but then I would have to disable AOT as well which I want to avoid. enter image description here

Is there another way to circumvent this issue?

NPadrutt
  • 3,619
  • 5
  • 24
  • 60
  • You may set the PublishTrimmed property to false. For more steps to publish a MAUI Android app , you may refer to the following link: https://learn.microsoft.com/en-us/dotnet/maui/android/deployment/publish-cli. Also, may I know what AOT aswell means? – Hongxin Sui-MSFT Oct 26 '22 at 06:49
  • AOT is ahead of time compilling which improves startup time and performance. As mentioned before would I have to disable that aswell when I would want to disable trimming which I would like to avoid. – NPadrutt Oct 26 '22 at 13:09
  • You don't need to disable that aswell at the same time. – Hongxin Sui-MSFT Oct 28 '22 at 02:08
  • 1
    On .net 7 RC2 I get this error message when I set `PublishTrimmed` to false: `The 'RunAOTCompilation' MSBuild property is only supported when trimming is enabled. Edit the project file in a text editor to set 'PublishTrimmed' to 'true' for this build configuration` – NPadrutt Oct 28 '22 at 07:14
  • Native AOT applications come with a few fundamental limitations and compatibility issues. Some are related to trimming, you can refer to the documentation for details:https://learn.microsoft.com/zh-cn/dotnet/core/deploying/native-aot/#limitations-of-native-aot-deployment. The error before you did not disable trimming was a linker related error, for the solution, you can refer to the link: https://github.com/dotnet/linker/issues/1559. – Hongxin Sui-MSFT Oct 31 '22 at 01:45
  • Sorry, I'm not sure I can follow. I read through the GitHub Issue you linked and from what I understand they say it's a bug and that they have fixed it 2 years ago. Given that it works with .net 6 that makes sense. but why does it no longer work with .net 7? – NPadrutt Oct 31 '22 at 06:52
  • This may still be a compatibility issue in nature, so could you consider switching back to .Net6 version if necessary? – Hongxin Sui-MSFT Nov 11 '22 at 09:23
  • 1
    I'm experiencing this issue too in my app... also after updating my app to .NET 7. Any idea on why it doesn't work anymore? When will it work? As a workaround, adding this to my .csproj solved the problem for android ` False False False ` But then, app is no longer optimized with this tags in .csproj so it's just a temporary fix... – Juansero29 Nov 19 '22 at 00:44

1 Answers1

4

I'm not expert enough to explain the intricacies of the situation but here is a quick workaround (see comments to question above for more details).

Right click the project and select "Properties"

Find "Enable trimming" and uncheck it. enter image description here

Then find AOT and uncheck it too 2

To be clear, this may not be the best way to resolve it permanently since the app will be less optimized.

adinas
  • 4,150
  • 3
  • 37
  • 47
  • 2
    This should not be the solution only a temporary workaround I created a github issue with a reference sample which links to this post: https://github.com/mono/SkiaSharp/issues/2327 – eugstman Dec 02 '22 at 10:04
  • 1
    Just want to chime in to say this solved this exact error message, which is great for a first-timer who wants to just test the publication procedure. – Tornseglare Apr 06 '23 at 00:39