Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Linking with 'Sdk and User Assemblies' with Xamarin.Forms fails

trampstertrampster Member ✭✭✭
edited July 28 in Xamarin.Android

When setting Linking to 'Sdk and User Assemblies' with Xamarin.Forms the build fails.

The docs don't mention this or describe out to work arround it: https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker

Mono.Linker.OutputException: Failed to write 'C:\Work\Ropu\RopuForms.Android\obj\Release\90\android\assets\Client.dll ---> System.ArgumentException: Member 'System.Void Ropu.Client.Program::Main(System.String[])' is declared in another module and needs to be imported
at Mono.Cecil.MetadataBuilder.LookupToken(IMetadataTokenProvider provider) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 2249
at Mono.Cecil.MetadataBuilder.BuildModule() in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 1047
at Mono.Cecil.MetadataBuilder.BuildMetadata() in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 1006
at Mono.Cecil.ModuleWriter.<>c.b__2_0(MetadataBuilder builder, MetadataReader _) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 144
at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func3 read) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/ModuleDefinition.cs:line 971 at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 143 at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable1 stream, WriterParameters parameters) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 119
at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/AssemblyWriter.cs:line 78
at Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/cecil/Mono.Cecil/ModuleDefinition.cs:line 1160
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.OutputStep.ProcessAssembly(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 MonoDroid.Tuner.Linker.Process(LinkerOptions options, ILogger logger, LinkContext& context)
at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res)
at Xamarin.Android.Tasks.LinkAssemblies.RunTask()
at Xamarin.Android.Tasks.AndroidTask.Execute() RopuForms.Android

What do I need to do to get the linking to work with User Assemblies

Tagged:

Best Answer

  • trampstertrampster Member ✭✭✭
    Accepted Answer

    For now I've worked around the problem by changing the project I'm referencing to library instead of Exe. So that it doesn't have a Main method/entry point to confuse the linker.

Answers

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @trampster I understand that the Linker is set to Link All Assemblies (Sdk and User Assemblies).
    This means that, the Linker would have stripped off even the Code added by you or the 3rd party NuGet packages where there aren't static references to them.

    As per the docs, The Link All Assemblies option links all assemblies, which means your code may also be removed if there are no static references. In order to preserve your code from Linker, you need to follow this:
    https://docs.microsoft.com/en-us/xamarin/android/deploy-test/linker#preserving-code

    I believe from the StackTrace, you have shared, you need to look at this method:

    System.ArgumentException: Member 'System.Void **Ropu.Client**.Program::Main(System.String[])' is declared in another module and needs to be imported

    A quick search for Ropu in NuGet got me this:
    https://www.nuget.org/packages/Ropu.AesGcm/ having it's repository in here:
    https://github.com/trampster/Ropu.AesGcm

    Although, it seems like it's your packages but I am not sure if you are using the same package or not.
    All I can point to is the Linker has removed the references to Ropu.Client and hence your App is failing to compile.

    You can even try to first make use of the Skip Linking Assemblies for skipping the Ropu from being Stripped off.

    Hope it helps!

  • trampstertrampster Member ✭✭✭
    edited July 29

    Thanks for your reply, Ropu.AesGcm is my package but it is not the thing in the stack trace.

    You can see my code at https://github.com/trampster/Ropu

    The problem is with the method:
    System.Void Ropu.Client.Program::Main(System.String[])'

    This is contained in a project I am referencing locally via a project reference (not nuget) and as you can see it's the Main (an unused entry point) method that is the problem which is not actually used by my Xamarin.Forms app at all.

    So I really don't care if the linker gets rid of it, in fact I want it to remove it.

    So why is the Xamarin linker complaining about code which is actually unused and should be stripped. And how can I make it work.

  • trampstertrampster Member ✭✭✭
    Accepted Answer

    For now I've worked around the problem by changing the project I'm referencing to library instead of Exe. So that it doesn't have a Main method/entry point to confuse the linker.

Sign In or Register to comment.