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.44k stars 508 forks source link

NET8 iOS Full Binding a XCFramework #21015

Closed cjrvdev closed 21 hours ago

cjrvdev commented 1 month ago

Apple platform

iOS, macOS, Mac Catalyst, tvOS

Framework version

net8.0-*

Affected platform version

.net8-iOS (8018)

Description

I am trying to do a full binding of the Lottie iOS library using the xcframework file from the official repo: https://github.com/airbnb/lottie-ios/releases/tag/4.5.0

The binding project fails to compile and generate the expected DLL file.

We need to do a full binding project and not slim bindings unfortunately as if we simply added a Nuget (which none are available at this time)

Sample REPO: https://github.com/cjrvdev/Cjrv.Lottie.iOS

Steps to Reproduce

1- Create a binding project using the iOS Binding Library Template in VS4Mac 2- Add the .xcframework file as a native reference 3- Generate files with sharpie using the following command sharpie bind --sdk=iphoneos17.5 --output="XamarinApiDef" --namespace="Crjv.Lottie.iOS" --scope="/Users/carlosrodriguez/Desktop/charpi/Lottie.xcframework/ios-arm64/Lottie.framework/Headers/" "/Users/carlosrodriguez/Desktop/charpi/Lottie.xcframework/ios-arm64/Lottie.framework/Headers/Lottie-Swift.h" 4- Add the files to the project and build

Project fails to compile: The type or namespace name 'Lottie' could not be found (are you missing a using directive or an assembly reference?)

Probably because there is a "using Lottie" in the ApiDefinitions file.

Did you find any workaround?

No

Build logs

No response

rolfbjarne commented 1 month ago

Sample REPO: cjrvdev/Cjrv.Lottie.iOS

I get a 404, can you make it public or invite me to it?

cjrvdev commented 1 month ago

Sample REPO: cjrvdev/Cjrv.Lottie.iOS

I get a 404, can you make it public or invite me to it?

My apologies, I marked it as private by mistake. It should be public now. Thanks for taking your time!

rolfbjarne commented 1 month ago

This PR makes the binding project build for me: https://github.com/cjrvdev/Cjrv.Lottie.iOS/pull/1

Can you try it and see if it works for you?

cjrvdev commented 1 month ago

Thanks for the PR. I have indeed managed to compile and create a nupkg file. Right now Im checking if I have all the necessary APIs though Im not seeing a few methods that I need. I'll look a it further and adjust if necessary and keep the issue up to date.

Again thanks a lot buddy :)

rolfbjarne commented 4 weeks ago

Thanks for the PR. I have indeed managed to compile and create a nupkg file. Right now Im checking if I have all the necessary APIs though Im not seeing a few methods that I need. I'll look a it further and adjust if necessary and keep the issue up to date.

Again thanks a lot buddy :)

That's great!

I'll close the issue then, but feel free to reopen (or as us to) if you have more problems.

cjrvdev commented 4 weeks ago

Sorry @rolfbjarne to reopen this. I've readjusted some of the code since we were using a rather old version of Lottie and it doesnt seem to compile to simulator. I am using XCode 15.4 and iOS 17.5 simulator and I'm getting the following error:

/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk.net8.0_17.5/17.5.8018/targets/Xamarin.Shared.Sdk.targets(3,3): Error: clang++ exited with code 1: Undefined symbols for architecture x86_64: "_OBJC_CLASS_$__TtC6Lottie10DebugLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie10GroupLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie10ImageLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie10ShapeLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie10SolidLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie12PreCompLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie13RepeaterLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie14ShapeItemLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie14TransformLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie16CompositionLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie16ShapeRenderLayer", referenced from: -exported_symbol[s_list] command line option "_OBJC_CLASS_$__TtC6Lottie18BaseAnimationLayer", referenced from: -exported_symbol[s_list] command line option (XXXXX.Touch)

PS: I obfuscated the name of the project, as it contains the client's name.

Saw a workaround mentioned here #20195 but it doesnt seem to work

rolfbjarne commented 3 weeks ago

The problem is that those symbols are not external in the framework (i.e. they're private):

$ nm -m Cjrv.Lottie.iOS/fws/4.2/Lottie.xcframework/ios-arm64/Lottie.framework/Lottie | grep '_OBJC_CLASS_$__TtC6Lottie10DebugLayer'
000000000017c7a0 (__DATA,__objc_data) non-external (was a private external) _OBJC_CLASS_$__TtC6Lottie10DebugLayer

So at link time they can't be found.

The fix is likely to just remove the corresponding bindings from the ApiDefinition.cs (i.e. for the symbol _OBJC_CLASS_$__TtC6Lottie10DebugLayer the fix would be to remove the DebugLayer type).

cjrvdev commented 1 day ago

Hello @rolfbjarne sorry for the late response, I've been on vacation for a few weeks. I tried removing the ApiDefinitions as you suggested and the error is gone. Now I seem to be able to compile but the application crashes when it starts. Here is the error:

dyld[66956]: Library not loaded: @rpath/Lottie.framework/Lottie Referenced from: /Users/carlosrodriguez/Library/Developer/CoreSimulator/Devices/6C9E1EC4-3F18-44CA-91FF-892B16A812CE/data/Containers/Bundle/Application/B1B46C9B-9C0B-4ECC-ACCB-0990034A92F9/MyApp.Touch.app/MyApp.Touch Reason: tried: '/Users/carlosrodriguez/Library/Developer/CoreSimulator/Devices/6C9E1EC4-3F18-44CA-91FF-892B16A812CE/data/Containers/Bundle/Application/B1B46C9B-9C0B-4ECC-ACCB-0990034A92F9/MyApp.Touch.app/Lottie.framework/Lottie' (no such file), '/Users/carlosrodriguez/Library/Developer/CoreSimulator/Devices/6C9E1EC4-3F18-44CA-91FF-892B16A812CE/data/Containers/Bundle/Application/B1B46C9B-9C0B-4ECC-ACCB-0990034A92F9/MyApp.Touch.app/Frameworks/Lottie.framework/Lottie' (no such file), '/Users/carlosrodriguez/Library/Developer/CoreSimulator/Devices/6C9E1EC4-3F18-44CA-91FF-892B16A812CE/data/Containers/Bundle/Application/B1B46C9B-9C0B-4ECC-ACCB-0990034A92F9/MyApp.Touch.app/Lottie.framework/Lottie' (no such file), '/Users/carlosrodriguez/Library/Developer/CoreSimulator/Devices/6C9E1EC4-3F18-44CA-91FF-892B16A812CE/data/Containers/Bundle/Application/B1B46C9B-9C0B-4ECC-ACCB-0990034A92F9/MyApp.Touch.app/Frameworks/Lottie.framework/Lottie' (no such file), '/Library/Developer/CoreSimulator/Volumes/iOS_21C62/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 17.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Lottie.framework/Lottie' (no such file)

Tried googling around with no luck unfortunately. Any ideas? I've tried adding the library as both .dll and nupkg and both share the same error.

Thanks a lot in advance. I updated my repo with the latest apidefinition in case you would like to try yourself. Also Im using .net8 and XCode 15.4

rolfbjarne commented 1 day ago

Looks like the Lottie framework isn't copied to the app bundle for some reason.

Can you get an MSBuild binlog for a build that causes this error?

cjrvdev commented 1 day ago

binlog removed Here it is. Please note there might be a bit of sensitive information about the client app inside it and I might need to remove my comment in the not-so-far future

Thanks

rolfbjarne commented 22 hours ago

Can you try changing Kind to Framework in your NativeReference: https://github.com/cjrvdev/Cjrv.Lottie.iOS/blob/93ee50a3638cd262da83b76b004e138ee0932ed5/Cjrv.Lottie.iOS/Cjrv.Lottie.iOS.csproj#L16?

<Kind>Framework</Kind>

(you can remove the binlog link now)

cjrvdev commented 21 hours ago

Tried it and now its working! (on simulator and debug at least, gonna try device soon)

Im going to close the issue for now and might reopen if something else appears. Consider yourself a hero, you saved the day, week and month

Thanks buddy!