Forum Xamarin Xamarin.Forms

Linking assemblies nightmare - Xamarin Forms - Android error

NicolasKrierNicolasKrier FRMember ✭✭✭

Hello, it's been a while since I did not have this kind of problem : building the app in release mode with a linking mode differentt to none throws an error which I'm unable to get rid off (neither SDK only nor SDK and User assemblies options work).

Here is the error I got while compiling (notice that I can't deploy and thus open device log to see advanced/more verbal logs.

System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.String'.
at Mono.Cecil.MetadataBuilder.GetConstantSignature(ElementType type, Object value)
at Mono.Cecil.MetadataBuilder.AddConstant(IConstantProvider owner, TypeReference type)
at Mono.Cecil.MetadataBuilder.AddField(FieldDefinition field)
at Mono.Cecil.MetadataBuilder.AddFields(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.b__5_0(MetadataBuilder builder, MetadataReader _)
at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func3 read) at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata) at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable1 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)
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.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()

I don't see any clue on the project or the dll that is messing my project.

I've tried the linking.config approach where you specify a linker.description which is supposed to say to the compiler "don't touch those dll". But didn't manage to make it work.

At first, I thought it might be some dependencies not working but I found an old backup (6months ago) and it builds well in release mode with linking mode set to SDK only. I've tried to upgrade every packages from the project to match the packages' version I use in my project (the latest version). The old project seems to still compile fine in release mode with linking assemblies set to SDK...

I'm a bit lost. It's hard to find which dependencies got a problem since I need to do a ton of merge and additional work if I want to use the latest updates. I've tried to find some additional info on the web but couldn't find any tips.

I need help because my project goes from (linking SDK only) 66mo to 119mo. The gap is huge and I would like to release some updates without increasing the size by 80%... Users will think I've added massive new features whereas I did not :blush:

Any help or feedback on how you made it working will be much appreciate.
Thanks for reading so far.

If you a need any further information to help me, let me know ! Thanks again and have a good day.


  • XamtasticXamtastic Member ✭✭✭
    edited September 2019

    When you go to Android Options > Linking what do you have?
    What is in your Manifest for Android versions?

    And I'm just going to assume you did... Clean > Rebuild > Delete > Build/Run
    On both the Forms and Android projects

  • NicolasKrierNicolasKrier FRMember ✭✭✭
    edited September 2019

    Hello @Xamtastic
    Yes I did a clean / delete recursive bin obj / rebuild.

    I have this :

    (I've tried with different version and nothing changes)

    I've tried with both Code shrinker : ProGuard / r8.

    Did you face the same issue in the past and managed to fix it ?

  • LandLuLandLu Member, Xamarin Team Xamurai

    It seems your error is caused by this:

    System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.String'.

    Have you tried to cast the byte somewhere else?

  • NicolasKrierNicolasKrier FRMember ✭✭✭

    Yes I thought the same but without any clue on where the cast is made (I use a framework shared by a big WPF project and it could came from a lot of classes). It's hard to debug. Will try to investigate further.

  • XamtasticXamtastic Member ✭✭✭

    Trust, me. You're not alone in this linker issue. Sorry I can't remember, my brain is fried after iOS into iTunes... but I believe there is no code error -- on your end. I do remember it was something to do with versions that I had to muck about with. The best i can do other than get this message back to the top of the board

    Also, didn't use a shrinker

    Looping back on Android bugs this week, if it comes to mind I'll drop a line. But you're on the right track.

Sign In or Register to comment.