The "LinkAssemblies" task failed unexpectedly in Xamarin Android - xamarin.android

I am trying to POST data using a REST API. The app used to run flawlessly, but now that I've added code to perform the POST, I'm getting a new error. Would anyone be able to help explain what's going wrong and suggest an alternative that would let me complete the POST request successfully?
Here's the error I get when I try to build the app with the linker enabled in the Release configuration:
Error The "LinkAssemblies" task failed unexpectedly.
Xamarin.Android.XamarinAndroidException: error XA2006: Reference to metadata item 'System.Runtime.InteropServices.StandardOleMarshalObject' (defined in 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') from 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' could not be resolved. ---> Mono.Cecil.ResolutionException: Failed to resolve System.Runtime.InteropServices.StandardOleMarshalObject
at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference)
at MonoDroid.Tuner.MonoDroidMarkStep.MarkType(TypeReference reference)
at Mono.Linker.Steps.MarkStep.MarkType(TypeReference reference)
at MonoDroid.Tuner.MonoDroidMarkStep.MarkType(TypeReference reference)
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 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.<ExecuteInstantiatedTask>d__26.MoveNext() GimcoIOT.Andriod

It looks like you might have added a reference to a desktop .NET assembly in your Xamarin.Android app. The System.Windows.Forms namespace is not available in the Xamarin.Android profile. (Even if the app builds successfully when you disable linking, it will in theory still fail at run time any time it tries to run code that references the System.Windows.Forms namespace.)
To solve this problem, you will want to remove the reference to the desktop .NET assembly from your Xamarin.Android project and ensure that all of your other referenced assemblies are compiled either against the Xamarin.Android profile or a supported PCL profile.
As mentioned in the documentation:
Xamarin.Android is not ABI compatible with existing assemblies compiled for a different profile. You must recompile your source code to generate assemblies targeting the Xamarin.Android profile (just as you need to recompile source code to target Silverlight and .NET 3.5 separately).
(Portable Class Libraries are the special exception to this rule.)
If you are currently using a type from the System.Windows.Forms namespace to perform the POST request, then you will need to switch to a different type. For example, one of the PostAsync() methods from System.Net.Http.HttpClient might work nicely.

Related

Error compiling in release mode or ad-hoc mode, Xamarin Forms iOS,

I have an error in xamarin forms ios, and it only happens in release mode, or Ad-Hoc mode, in debug mode if the app works, why is it?
Error Failed to resolve "System.Reflection.Emit.DynamicMethod"
reference from "mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" TuFacturadorApp.iOS CompilaciĆ³n
You need to determine what code/package is pulling that code in as you can not use Reflection.Emit in a release configuration iOS build.
Note: Also make sure that your linker setting for your Release configuration is not set to "Don't link"
No Dynamic Code Generation
Since the iPhone's kernel prevents an application from generating code dynamically Mono on the iPhone does not support any form of dynamic code generation. These include:
The System.Reflection.Emit is not available.
No support for System.Runtime.Remoting.
No support for creating types dynamically (no Type.GetType ("MyType`1")), although looking up existing types (Type.GetType ("System.String") for example, works just fine).
Reverse callbacks must be registered with the runtime at compile time.
re: https://developer.xamarin.com/guides/ios/advanced_topics/limitations/#No_Dynamic_Code_Generation

Reference to type 'System.IConvertible' claims it is defined in assembly mscorlib

Anytime I try to reference an NUNIT data type in the iOS framework, I am given a compiler error saying it can't find the IConvertible type
Error CS7069: Reference to type System.IConvertible' claims it is defined assemblymscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', but it could not be found (CS7069) (logic)
I was able to reproduce this by creating a brand new Xamarin.Forms project that only targets iOS.
Immediately after creating it, I added a second Xamarin.Forms project, this time a PCL.
I then needed to add the Xamarin.iOS library so that I can reference the iOS specific APIs, specifically the Photos API. I did this by navigating to
~/Library/Frameworks/Xamarin.iOS.framework/Versions/9.0.1.29/lib/mono/Xamarin.iOS/Xamarin.iOS.dll
Lastly, I added a blank class to the new PCL project, and created a locally scoped NUNIT field. This is where the compiler error happens. This happens in my current project when I try to get the number of photos in a PHAssetCollection, as that is an NUNIT data type.
using System;
namespace logic
{
public class EmptyClass
{
public EmptyClass()
{
nuint test = 5;
}
}
}
This gives me the compiler error I've referenced above. How can I solve this? This has become a blocking issue for me, am I referencing the wrong Xamarin.iOS.dll? It's not available in the list of nuget packages when I scan for them, so I can't add it via NuGet. NuGet only has Xamarin.Forms.dll available.
Update 1
After trying a few different combinations, I think that I have a working solution. Instead of creating a Xamarin.Forms Library, I created a normal iOS PCL library instead. This solved that problem.
I don't know why the Xamarin.Forms library wouldn't work. Is it not intended to be used for platform specific code?
Xamarin Forms PCL's need to be platform agnostic. If you have a NUnit project that needs access to a Xamarin.iOS specific reference, then you need to build within the iOS project / iOS PCL.
Xamarin.iOS.dll can only be referenced from a iOS related project.

Persistent issue with Xam.Forms Lab on Android

I have a multiplatform app using Xamarin.Forms. While I can build for iOS and WindowsPhone 8.1, when I build for Android, I get the following build error
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: error : Error executing task LinkAssemblies: error XA2006: Reference to metadata item 'System.Void Android.OS.BaseBundle::PutString(System.String,System.String)' (defined in 'XLabs.Platform.Droid, Version=2.0.5679.29813, Culture=neutral, PublicKeyToken=null') from 'XLabs.Platform.Droid, Version=2.0.5679.29813, Culture=neutral, PublicKeyToken=null' could not be resolved.
I've searched around for a solution on this but cannot seem to find one or where on earth in my code this problem is coming from.
Does anyone know how to start solving this issue so I can submit a build?
Check that the Xamarin Forms NuGet package versions are the same in the XLabs version you are using and the Android Project. Normally a version mismatch is the cause for these errors.

Error with MvvmCross using System.Net on MonoDroid

I have referenced HttpStatusCode via System.Net in a Portable Class Library.
I then need to reference that status code from a MonoDroid project that includes that PCL. I understand that the System.Net namespace exists in the System DLL for MonoDroid and this has been included.
However this results in a build error:
CS0012: The type 'System.Net.HttpStatusCode' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.
Now I realise there are strong namespacing issues with referencing the PCL System.Net, and that there is a Shim DLL in MvvmCross.PortableSupport that is supposed to resolve this issue. I have included MvvmCross.PortableSupport.3.0.8.1\lib\MonoAndroid16\System.Net.dll to the MonoDroid project, but I still get the same error.
What am I missing?
Thanks!
Matthew
So I was unable to determine the underlying issue, but the workaround was to avoid referencing the System.Net symbol from the native code via the Portable Class Library (framework 104 btw)
In this instance, instead of exposing the HttpStatusCode from the PCL, I exposed it as an integer and thus worked around the problem.
Stuart is away on holiday at the moment but he suggested this as a workaround and it works fine. It's not clear why the shim System.Net.Dll that MvvmCross includes isn't redirecting the reference correctly to the native library.

Error when building project with monodroid/mvvmcross

I've got an error when I try to build a project (monodroid/mvvmcross).
This is the message error :
Error 37 The type 'Android.App.ListActivity' is defined in an assembly that is not referenced. You must add a reference to assembly 'Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c4c4237547e4b6cd'.
Someone have an idea how to solve this problem ? This is strange because there wasn't this error before.
This error is located in MvxDialogActivityView.cs (cirrious.MvvmCross.Dialog.Droid)
I think your problem is that parts of your code is compiled against Xamarin.Android while others are compiled against Mono for Android.
See: http://forums.xamarin.com/discussion/1476/changes-to-assembly-strongnames-in-xamarin-android-4-6-0
You will need to recompile all parts of your application to target the same version of Xamarin's products.

Resources