Linking error when trying to build Xamarin.Android project - xamarin

I am trying to build my Xamarin.Android project. Initially I had set Linking to Sdk and User Assemblies. This configuration was not giving any errors, but it removed some commands from my app, so following this answer I switched the setting to Sdk assemblies only.
Unfortunately, this causes the following error:
Mono.Linker.MarkException: Error processing method: 'System.Void Plugin.FirebaseAnalytics.FirebaseAnalytics::SetMinimumSessionDuration(System.Int64)' in assembly: 'Plugin.FirebaseAnalytics.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve System.Void Firebase.Analytics.FirebaseAnalytics::SetMinimumSessionDuration(System.Int64)
at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod(MethodReference reference)
at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference)
at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
at Mono.Linker.Steps.MarkStep.ProcessQueue()
--- End of inner exception stack trace ---
at Mono.Linker.Steps.MarkStep.ProcessQueue()
at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
at Mono.Linker.Steps.MarkStep.Process()
at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
at MonoDroid.Tuner.MonoDroidMarkStep.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 Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/builder/azdo/_work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17
Since choosing Sdk and Users Assemblies probably is not an option, any idea how can I prevent the error that appears with Sdk assemblies only?

You will see this exception at runtime when code has been stripped from the executable usually by the linker or R8/Proguard. You can provide a custom link configuration file to both tools allowing you to keep code the linker or R8/Proguard you are not using. These tools are not able to properly detect code you use at runtime (e.g. via reflection) .
For this problem, you can drive the linking process with an XML file that describes what you want from the linker.
To do this, you define an XML file with the top-level element which contains assembly nodes which in turn contain type nodes, which in turn contain method and field nodes.
Once you have this linker description file, add it to your project and:
For Android : set the Build Action to LinkDescription
For iOS : set the Build Action to LinkDescription
For more, you can check document: https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/linker

Related

Error while deploying on device in Xamarin.Android after upgrade to AndroidX in Visual Studio 2019

I recently updated Xamarin Android project to support AndroidX. I migrated PreferenceManager and few mandatory APIs/methods like ExternalStorageDirectory, SetColorFilter only, to have successful compilation.
As, there are few third party libraries, which are still using old v4/v7 support libraries. Meaning, I can not start migration of support libraries.
With this, build is working fine. I can deploy to device and run the app.
But, While deploying to device in debug mode, getting below error:
Severity Code Description Project File Line Suppression State
Error MSB4018: The "GenerateJavaStubs" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load assembly 'Xamarin.Android.Support.v7.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'Xamarin.Android.Support.v7.AppCompat.dll'
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters)
at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference)
at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type)
at Mono.Cecil.TypeReference.Resolve()
at Java.Interop.Tools.Cecil.TypeDefinitionRocks.<GetTypeAndBaseTypes>d__1.MoveNext()
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at Java.Interop.Tools.Cecil.TypeDefinitionRocks.IsSubclassOf(TypeDefinition type, String typeName)
at Java.Interop.Tools.JavaCallableWrappers.JavaTypeScanner.AddJavaTypes(List`1 javaTypes, TypeDefinition type)
at Java.Interop.Tools.JavaCallableWrappers.JavaTypeScanner.GetJavaTypes(IEnumerable`1 assemblies, IAssemblyResolver resolver)
at Xamarin.Android.Tasks.GenerateJavaStubs.Run(DirectoryAssemblyResolver res)
at Xamarin.Android.Tasks.GenerateJavaStubs.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() 0
Did someone also, found this issue?
Same question already asked:
https://forums.xamarin.com/discussion/169837/vs-2019-gives-error-could-not-load-assembly-xamarin-android-support-v7-appcompat

I am getting error in building xamarin android project

The "LinkAssemblies" task failed unexpectedly.
Mono.Linker.MarkException: Error processing method:
'Android.Views.ScaleGestureDetector
Xamarin.Forms.Platform.Android.GestureManager::InitializeScaleDetector()'
in assembly: 'Xamarin.Forms.Platform.Android.dll' --->
Mono.Cecil.ResolutionException: Failed to resolve System.Void
Android.Support.V4.View.ScaleGestureDetectorCompat::SetQuickScaleEnabled(Android.Views.ScaleGestureDetector,System.Boolean)
at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod(MethodReference
reference) at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference
reference) at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction
instruction)
'Xamarin.Forms.Platform.Android.dll' ---> Mono.Cecil.ResolutionException:
Sometimes it is resulted from Xamarin.Forms.Platform.Android.dll location refer from different place in the android project references.You need to change it.Here is a similar discussion about how to solve this problem.
https://forums.xamarin.com/discussion/102599/the-linkassemblies-task-failed-unexpectedly-error-while-building-the-application
In addition, you can check the sdk version, set the target sdk version to a newer version, and try again.

The "LinkAssemblies" task failed unexpectedly | Xamarin Release Build

I am working on an app in Visual Studio using Xamarin. I want to build in release mode in order to compile an APK so that I can have a friend install the app to test what I have done so far.
Upon building in release mode I get the following error:
Severity Code Description Project File Line Suppression State
Error The "LinkAssemblies" task failed unexpectedly.
System.NullReferenceException: Object reference not set to an instance of an object.
at Mono.Linker.TypeReferenceExtensions.<GetMethods>d__4.MoveNext()
at Mono.Linker.Steps.TypeMapStep.TryMatchMethod(TypeReference type, MethodDefinition method)
at Mono.Linker.Steps.TypeMapStep.<GetBaseMethodsInInterfaceHierarchy>d__12.MoveNext()
at Mono.Linker.Steps.TypeMapStep.MapVirtualInterfaceMethod(MethodDefinition method)
at Mono.Linker.Steps.TypeMapStep.MapVirtualMethods(TypeDefinition type)
at Mono.Linker.Steps.TypeMapStep.MapType(TypeDefinition type)
at Mono.Linker.Steps.TypeMapStep.MapType(TypeDefinition type)
at Mono.Linker.Steps.TypeMapStep.ProcessAssembly(AssemblyDefinition assembly)
at Mono.Linker.Steps.BaseStep.Process(LinkContext context)
at Mono.Linker.Pipeline.Process(LinkContext context)
at MonoDroid.Tuner.Linker.Process(LinkerOptions options, 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.<ExecuteInstantiatedTask>d__26.MoveNext() PartyMe.Android
There are a lot of results for a google search of the issue. All of them recommend targeting the latest Android SDK or API 21. I am already targeting API 26 (Oreo). A few also say to update Xamarin.Forms to the latest version, but I am not even using Xamarin.Forms.
I have also tried closing / reopening and cleaning / rebuilding.
There is a bug in Visual Studio 2017 15.5.X version that causing Linker issue for Xamarin.Android project compile in Release mode. I tested on 15.5.5, the problem still exist.
Unfortunately, so far the only workaround is to revert back to VS2017 15.4.5
Same problem reported in Xamarin forum:
https://forums.xamarin.com/discussion/112783/the-linkassemblies-task-failed-unexpectedly?
How to revert back to older version of VS2017: https://learn.microsoft.com/en-us/visualstudio/productinfo/installing-an-earlier-release-of-vs2017
It looks like it's not compiling the PCL/Shared project in your solution.
If it's a project that was ongoing/working with some references to Nuget packages I would clean the solution, delete the /bin & /obj folders and delete the packages folder.
Otherwise, I would delete and re-insert manually all the packages references in your PCL/Shared project.

“Link Assemblies” task failed unexpectedly issue while building release version of app in Xamarin

I want to release my android app. When I try to build it, it gives me the following error:
Severity Code Description Project File Line Suppression State
Error The "LinkAssemblies" task failed unexpectedly.
Java.Interop.Tools.Diagnostics.XamarinAndroidException: error XA2006: Reference to metadata item 'Mono.Security.Interface.MonoTlsProviderFactoryDelegate' (defined in 'Xamarin.iOS, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065') from 'Xamarin.iOS, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' could not be resolved. ---> Mono.Cecil.ResolutionException: Failed to resolve Mono.Security.Interface.MonoTlsProviderFactoryDelegate
at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference)
at MonoDroid.Tuner.MonoDroidMarkStep.MarkType(TypeReference reference)
at Mono.Linker.Steps.MarkStep.MarkField(FieldReference reference)
at Mono.Linker.Steps.MarkStep.InitializeFields(TypeDefinition type)
at Mono.Linker.Steps.MarkStep.InitializeType(TypeDefinition type)
at Mono.Linker.Steps.MarkStep.InitializeAssembly(AssemblyDefinition assembly)
at Mono.Linker.Steps.MarkStep.Initialize()
at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
at Mono.Linker.Pipeline.Process(LinkContext context)
at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
at Xamarin.Android.Tasks.LinkAssemblies.Execute()
--- End of inner exception stack trace ---
at Java.Interop.Tools.Diagnostics.Diagnostic.Error(Int32 code, Exception innerException, String message, Object[] args)
at Xamarin.Android.Tasks.LinkAssemblies.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() TOPAPP.Droid
I have deleted the xamarin.ios reference in the app.droid but that didn't work. Any suggestions? I also tried setting the android versions to what ever I found on the internet. I also tried updating PCL version but still nothing. Am I missing something?
EDIT:
In my project I also have a portable library linked to the android project with ios code in it.
I'm not sure why you might have Xamarin.iOS referenced in your Xamarin.Android project. But the issue is that you have some type of reference there when it shouldn't be. Your PCL should not have any Native code inside it.
The Linker does not do any work when it's a Debug configuration. When you change to Release it will try to find assemblies that you do not use and strip them out. For some reason you have an iOS assembly in your Android project.
Check that your Xamarin.Android and PCL project do not reference Xamarin.iOS in anyway. The native projects should only be referencing the PCL(Which contains no Native Code inside)

The "LinkAssemblies" task failed unexpectedly while publishing apk file

I am using Xamarin Forms to develop a mobile app.I am able to deploy the app in debug mode and the app builds with no errors.I am trying to publish apk in release mode but getting below error.I have used Syncfusion SFchart and SFGauge.Kindly help and find below error description:
C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(5,5): Error MSB4018: The "LinkAssemblies" task failed unexpectedly.
Xamarin.Android.XamarinAndroidException: error XA2006: Reference to metadata item 'System.Int32 Xamarin.Forms.Platform.Android.Resource/Attribute::actionBarSize' (defined in 'Syncfusion.SfGauge.XForms.Android, Version=14.2451.0.26, Culture=neutral, PublicKeyToken=null') from 'Syncfusion.SfGauge.XForms.Android, Version=14.2451.0.26, Culture=neutral, PublicKeyToken=null' could not be resolved. ---> Mono.Cecil.ResolutionException: Failed to resolve System.Int32 Xamarin.Forms.Platform.Android.Resource/Attribute::actionBarSize
at Mono.Linker.Steps.MarkStep.MarkField(FieldReference reference)
at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
at Mono.Linker.Steps.MarkStep.ProcessQueue()
at Mono.Linker.Steps.MarkStep.Process()
at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
at Mono.Linker.Pipeline.Process(LinkContext context)
at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
at Xamarin.Android.Tasks.LinkAssemblies.Execute()
--- End of inner exception stack trace ---
at Xamarin.Android.Diagnostic.Error(Int32 code, Exception innerException, String message, Object[] args)
at Xamarin.Android.Tasks.LinkAssemblies.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext() (MSB4018) (EWSMobile.Droid)
Your main issue is the following:
error XA2006: Reference to metadata item 'System.Int32 Xamarin.Forms.Platform.Android.Resource/Attribute::actionBarSize' (defined in 'Syncfusion.SfGauge.XForms.Android, Version=14.2451.0.26, Culture=neutral, PublicKeyToken=null') from 'Syncfusion.SfGauge.XForms.Android, Version=14.2451.0.26, Culture=neutral, PublicKeyToken=null' could not be resolved. ---> Mono.Cecil.ResolutionException: Failed to resolve System.Int32 Xamarin.Forms.Platform.Android.Resource/Attribute::actionBarSize
This means that the Attribute actionBarSize cannot be found or is not included in your Resource.designer.cs file. There are a few typical issues of why this happens:
1) You are not compiling against the latest Android API (API 23 at the current time of writing)
2) The referenced library might be out of date and should be updated and compiled against the latest Android API. (API 23 currently)
3) If you are using Xamarin.Forms, you should ensure you use the version that was compiled with the third party library.
https://developer.android.com/reference/android/R.attr.html#actionBarSize
As additional debugging steps:
1) Set your Linker setting in Release configuration to Don't Link/None.
https://developer.xamarin.com/guides/android/advanced_topics/linking/#Linker_Behavior
If it works here, you know that a type is being linked out of this library.
2) Preserve the linked out type
You can use linkskip to linkskip the assembly:
https://developer.xamarin.com/guides/android/advanced_topics/linking/#linkskip
Otherwise you can readup on general Linking here:
https://developer.xamarin.com/guides/android/advanced_topics/linking
Issue resolved with latest Xamarin.Forms Nuget packages (v 2.3.0.107), the mentioned issue occurs with older version of Xamarin.Forms packages.
Subburaj Pandian V.

Resources