xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.42k stars 507 forks source link

Error Binding IOS Static Library MAUI #20617

Closed Lucasvor closed 1 month ago

Lucasvor commented 1 month ago

Description

Hi, I am trying to create a wrapper for a private iOS library. I have the file with the '.a' extension and its headers. This library was working normally, but when I bind this library to .NET MAUI, the build succeeds. However, when using it in a .NET MAUI project, it returns the following error: 'Could not create a native instance of the type 'LibTest.EmbeddedSdk': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.' I have already tried with the ObjCRuntime.Class.ThrowOnInitFailure = false option, but when executing the methods, it always returns with a default value. Could you help me with this?

Steps to Reproduce

  1. Download this project: https://github.com/Lucasvor/TestLibBinding
  2. Build project "LibTestBindingIOS"
  3. Build Project "BATeste"
  4. Run project maui in simulator iphone and click on button

Expected Behavior

When executing the method: 'value.Init(data)' it should return an int value different from 0 in the MainPage.xaml.cs

Actual Behavior

Return an int value different from 0 and not the erro the type 'LibTest.EmbeddedSdk'

Environment

Environment Visual Studio Community 2022 for Mac Version 17.6.12 (build 410) Installation UUID: 76a6c7e6-4d90-439b-9dc6-d81f89ad18fc Runtime .NET 7.0.3 (64-bit) Architecture: Arm64 Microsoft.macOS.Sdk 13.1.1007; git-rev-head:8afca776a0a96613dfb7200e0917bb57f9ed5583; git-branch:release/7.0.1xx-xcode14.2 Roslyn (Language Service) 4.6.0-3.23180.6+99e956e42697a6dd886d1e12478ea2b27cceacfa NuGet Version: 6.4.0.117 .NET SDK (Arm64) SDK: /usr/local/share/dotnet/sdk/8.0.300/Sdks SDK Versions: 8.0.300 7.0.316 6.0.422 MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks .NET Runtime (Arm64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 8.0.5 7.0.19 6.0.30 Xamarin.Profiler Version: 1.8.0.49 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler Updater Version: 11 Xamarin.Android Version: 13.2.2.0 (Visual Studio Community) Commit: xamarin-android/d17-5/45b0e14 Android SDK: /Users/lucasvor/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 13.0 (API level 33) SDK Command-line Tools Version: 7.0 SDK Platform Tools Version: 34.0.3 SDK Build Tools Version: 32.0.0 Build Information: Mono: d9a6e87 Java.Interop: xamarin/java.interop/d17-5@149d70fe SQLite: xamarin/sqlite/3.40.1@68c69d8 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@ca1552d Microsoft Build of OpenJDK Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk 11.0.16.1 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Eclipse Temurin JDK Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk 1.8.0.302 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Android SDK Manager Version: 17.6.0.50 Hash: a715dca Branch: HEAD Build date: 2024-05-09 04:36:12 UTC Android Device Manager Version: 0.0.0.1309 Hash: 06e3e77 Branch: HEAD Build date: 2024-05-09 04:36:12 UTC Apple Developer Tools Xcode: 15.2 22503 Build: 15C500b Xamarin.Mac Not Installed Xamarin.iOS Version: 16.4.0.23 Visual Studio Community Hash: 9defd91b3 Branch: xcode14.3 Build date: 2023-10-23 16:15:00-0400 Xamarin Designer Version: 17.6.3.9 Hash: 2648399ae8 Branch: remotes/origin/d17-6 Build date: 2024-05-09 04:36:07 UTC Build Information Release ID: 1706120410 Git revision: 2f8e0518dd80a933901821bac53f7398d4b61c0f Build date: 2024-05-09 04:34:23+00 Build branch: release-17.6 Build lane: release-17.6 Operating System Mac OS X 13.6.3 Darwin 22.6.0 Darwin Kernel Version 22.6.0 Tue Nov 7 21:42:27 PST 2023 root:xnu-8796.141.3.702.9~2/RELEASE_ARM64_T8103 arm64
Visual Studio for MAC ``` ```

Build Logs

Build Logs System.Exception: Could not create an native instance of the type 'LibTest.EmbeddedSdk': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false. at Foundation.NSObject.InitializeObject(Boolean alloced) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSObject2.cs:line 363 at Foundation.NSObject..ctor(NSObjectFlag x) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSObject2.cs:line 214 at LibTest.EmbeddedSdk..ctor() in /Users/lucasvor/Projects/BAteste/LibTestBindingIOS/obj/Debug/net8.0-ios/iOS/LibTest/EmbeddedSdk.g.cs:line 60 at BAteste.MainPage.OnCounterClicked(Object sender, EventArgs e) in /Users/lucasvor/Projects/BAteste/BAteste/MainPage.xaml.cs:line 29 at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked() at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager) at Microsoft.Maui.Controls.Button.SendClicked() at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked() at Microsoft.Maui.Handlers.ButtonHandler.ButtonEventProxy.OnButtonTouchUpInside(Object sender, EventArgs e) at UIKit.UIControlEventProxy.Activated() in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIControl.cs:line 38 --- End of stack trace from previous location --- at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2594 at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 60 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94 at BAteste.Program.Main(String[] args) in /Users/lucasvor/Projects/BAteste/BAteste/Platforms/iOS/Program.cs:line 13 2024-05-20 15:58:28.215107-0300 BAteste[63225:2013284] Unhandled managed exception: Could not create an native instance of the type 'LibTest.EmbeddedSdk': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false. (System.Exception) at Foundation.NSObject.InitializeObject(Boolean alloced) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSObject2.cs:line 363 at Foundation.NSObject..ctor(NSObjectFlag x) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSObject2.cs:line 214 at LibTest.EmbeddedSdk..ctor() in /Users/lucasvor/Projects/BAteste/LibTestBindingIOS/obj/Debug/net8.0-ios/iOS/LibTest/EmbeddedSdk.g.cs:line 60 at BAteste.MainPage.OnCounterClicked(Object sender, EventArgs e) in /Users/lucasvor/Projects/BAteste/BAteste/MainPage.xaml.cs:line 29 at Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked() at Microsoft.Maui.Controls.ButtonElement.ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager) at Microsoft.Maui.Controls.Button.SendClicked() at Microsoft.Maui.Controls.Button.Microsoft.Maui.IButton.Clicked() at Microsoft.Maui.Handlers.ButtonHandler.ButtonEventProxy.OnButtonTouchUpInside(Object sender, EventArgs e) at UIKit.UIControlEventProxy.Activated() in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIControl.cs:line 38 --- End of stack trace from previous location --- at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2594 at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 60 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94 at BAteste.Program.Main(String[] args) in /Users/lucasvor/Projects/BAteste/BAteste/Platforms/iOS/Program.cs:line 13 ================================================================= Native Crash Reporting ================================================================= Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. ================================================================= ================================================================= Native stacktrace: ================================================================= 0x10d0746d5 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_dump_native_crash_info 0x10d0130de - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_handle_native_crash 0x10d20db08 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : sigabrt_signal_handler.cold.1 0x10d074010 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_runtime_setup_stat_profiler 0x1219385ed - /usr/lib/system/libsystem_platform.dylib : _sigtramp 0x0 - Unknown 0x121792d30 - /Library/Developer/CoreSimulator/Volumes/iOS_21C62/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 17.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_c.dylib : abort 0x10c37d0a7 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libxamarin-dotnet-debug.dylib : xamarin_unhandled_exception_handler 0x10d111ce4 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_invoke_unhandled_exception_hook 0x10d012cdf - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_handle_exception_internal 0x10d010d25 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_handle_exception 0x10d088b09 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : interp_throw 0x10d086c5c - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : interp_throw_ex_general 0x10d080e85 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_interp_exec_method 0x10d075f63 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : interp_runtime_invoke 0x10d15c2d8 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_runtime_invoke_checked 0x10d163c1b - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_runtime_exec_main_checked 0x10cfc8742 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libmonosgen-2.0.dylib : mono_jit_exec 0x10c390bea - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/libxamarin-dotnet-debug.dylib : xamarin_main 0x103067a14 - /Users/lucasvor/Library/Developer/CoreSimulator/Devices/655EF239-87F1-4167-A4F6-C55E6F76D235/data/Containers/Bundle/Application/20A0AF08-5286-4660-9D3A-5228B15ACFB0/BAteste.app/BAteste : main 0x10bb573e0 - Unknown 0x20361b41f - Unknown ================================================================= Basic Fault Address Reporting ================================================================= Memory around native instruction pointer (0x1218c41e2):0x1218c41d2 ff ff c3 90 90 90 b8 48 01 00 02 49 89 ca 0f 05 .......H...I.... 0x1218c41e2 73 08 48 89 c7 e9 df 9a ff ff c3 90 90 90 b8 53 s.H............S 0x1218c41f2 00 00 02 49 89 ca 0f 05 73 08 48 89 c7 e9 c7 9a ...I....s.H..... 0x1218c4202 ff ff c3 90 90 90 b8 83 01 00 02 49 89 ca 0f 05 ...........I.... ================================================================= Managed Stacktrace: ================================================================= =================================================================

Example Project (If Possible)

  1. Download project Project Binding
  2. This project contains the file ".a" and Headers
  3. Also contains the project to binding with ApiDefinitions and Strucs

Error: image

Anyone can help with this?

rolfbjarne commented 1 month ago

This happens due to a bug, which we've already fixed, but unfortunately it's not released yet.

In the meantime, you can add this to your binding project file:

<PropertyGroup>
    <NoBindingEmbedding>false</NoBindingEmbedding>
</PropertyGroup>

However, I noticed that you're using a static library, and that will eventually run into other problems: it doesn't work in the iOS arm64 simulator (because a static library can only contain a single slice of code for each architecture, and the "arm64" slice in the static library is already used up by the device version of "arm64").

The fix is to create an .xcframework bundleinstead of a .a file, and reference that in the project file instead. There are plenty of tutorials online for how to create .xcframeworks, but here's Apple's documentation as a starting point: https://developer.apple.com/documentation/xcode/creating-a-multi-platform-binary-framework-bundle

Lucasvor commented 1 month ago

Thank you for the quick response @rolfbjarne , do you know if I can get it to work with an emulator in other versions? For example, .Net 7 or .Net 6, because it worked in Xamarin :(. I tried the Slim Bindings approach and it worked well, but I didn't want to have to recreate all the methods of the library that are necessary for my application's use.

rolfbjarne commented 1 month ago

Thank you for the quick response @rolfbjarne , do you know if I can get it to work with an emulator in other versions? For example, .Net 7 or .Net 6, because it worked in Xamarin :(. I tried the Slim Bindings approach and it worked well, but I didn't want to have to recreate all the methods of the library that are necessary for my application's use.

This workaround:

<PropertyGroup>
    <NoBindingEmbedding>false</NoBindingEmbedding>
</PropertyGroup>

should work in .NET 6 and 7 as well.

The problem with the arm64 ios simulator still remains though, but you can choose to use the x64 simulator instead.