Xamarin and .NET Standard 2 Library issue
Asked Answered
A

3

14

I've created a cross platform library which has 3 platform specific implementations:

  • Android
  • iOS
  • .NET Standard 2.0

I've packed the library with Nuget in a similar vein as the cross platform library project does it.

project layout

Now when I create a new Xamarin Forms project and select .NET Standard as the means to share the code, I reference my nuget and try running it in the android simulator, I get:

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(2,2): Error: Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'SDK.NetStandard, Version=1.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile?
File name: 'SDK.NetStandard.dll'
  at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference reference, Mono.Cecil.ReaderParameters parameters) [0x0009a] in /Users/builder/data/lanes/5945/342b2ce9/source/monodroid/external/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:229 
  at Java.Interop.Tools.Cecil.DirectoryAssemblyResolver.Resolve (Mono.Cecil.AssemblyNameReference reference) [0x00000] in /Users/builder/data/lanes/5945/342b2ce9/source/monodroid/external/xamarin-android/external/Java.Interop/src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/DirectoryAssemblyResolver.cs:179 
  at Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver, System.Collections.Generic.ICollection`1[T] assemblies, Mono.Cecil.AssemblyDefinition assembly, System.Boolean topLevel) [0x0014a] in <fdfe8f54615a4e2ab24c72dc90da5c64>:0 
  at Xamarin.Android.Tasks.ResolveAssemblies.Execute (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver) [0x00237] in <fdfe8f54615a4e2ab24c72dc90da5c64>:0  (blank.Android)

I've double checked and the SDK.NetStandard.dll exists in the nuget package.

I've tested it also with creating a simple console app with .NET Core 2 and the library works property there.

[EDIT]

I've renamed my package from SDK to Matchmore.SDK to lessen the confusion The error looks like this in runtime iOS.

System.IO.FileNotFoundException: Could not load file or assembly 'Matchmore.SDK.NetStandard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
  at blankstandard.App..ctor () [0x0001b] in /Users/lmlynik/Projects/blankstandard/blankstandard/App.xaml.cs:15
  at blankstandard.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x00007] in /Users/lmlynik/Projects/blankstandard/blankstandard.iOS/AppDelegate.cs:26
  at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.178/src/Xamarin.iOS/UIKit/UIApplication.cs:79
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.10.1.178/src/Xamarin.iOS/UIKit/UIApplication.cs:63
  at blankstandard.iOS.Application.Main (System.String[] args) [0x00001] in /Users/lmlynik/Projects/blankstandard/blankstandard.iOS/Main.cs:17

Also after unpacking the nupkg you can see the DLL is in there. enter image description here

Arezzini answered 1/6, 2018 at 14:6 Comment(6)
It is a build error, not a runtime error. The other info is murky as well, but you must not select .NETStandard for your Forms project. It is only a correct choice for library projects.Canea
Depends on the platform, I get this a as a build error in an Android Xamarin project and I get a similar error but runtime in iOS. Could not load file or assembly 'Matchmore.SDK.NetStandard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.Younglove
can you show your .csproj content? Using netstandard needs not only nuget but also some parameters in the .csprojMiddlings
@Middlings you need the csproj for the solution which uses the library or the library it self?Younglove
@ŁukaszMłynik the csproj of the csproj which uses the libraryMiddlings
@ŁukaszMłynik have you checked for dependencies between the packages? Have you removed the reference and later added the reference to the package? Have you tested to downgrade to xamarin forms 2.5? If so, do you get the same error?Kenji
P
2

https://bugzilla.xamarin.com/show_bug.cgi?id=43713 should help you. This basically suggests that you use msbuild instead of xbuild.

If that is not possible use this work-around instead

  1. Instead of adding a .NET Standard library, add a PCL library.
  2. Get the project to run successfully
  3. Remove the Nuget packages from the PCL library
  4. Convert the PCL into a .NET Standard library
  5. Add the Nuget packages back in to the .NET Standard library
  6. Run the project again.
  7. The project should run successfully
Plano answered 8/6, 2018 at 7:36 Comment(0)
K
1

You are missing the SDK.NetStandard.dll file. Go to the Nuget and and install / update the NETStandard. Library, Solution -> Manage Nuget Packages for solution.enter image description here

Kenji answered 6/6, 2018 at 19:41 Comment(4)
I've added this in my library project for .NET Standard and got: /usr/local/share/dotnet/sdk/2.1.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.DefaultItems.targets(5,5): Warning: A PackageReference for 'NETStandard.Library' was included in your project. This package is implicitly referenced by the .NET SDK and you do not typically need to reference it from your project. For more information, see aka.ms/sdkimplicitrefs (Matchmore.SDK.NetStandard)Younglove
You mean Xamarin? It is 3+Younglove
Yes, have you checked for dependencies between the packages? Have you removed the reference and later added the reference to the package? Have you tested to downgrade to xamarin forms 2.5? If so, do you get the same error?Kenji
Adding this to my Android project made it possible to reference my Standard libraries.Closelipped
L
0

Visual Studio and .NET Core Tooling with .NET CLI below 2 doesn't fully support referencing assemblies. You need to package the library and use a project reference to the soltuion. The reason is that the required assmeblies with their versions are resolved during compilation and then write to the JSON file. While loading zour library assemblies, this might fail as they can#t find the right assembly or the dependencies.

Lobelia answered 13/6, 2018 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.