Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios binding library with xcframework/framework cannot be referenced in the main .NET MAUI project #21189

Closed
wwen0 opened this issue Sep 6, 2024 · 12 comments
Labels
binding-projects Issue or PR that affects binding projects need-attention An issue requires our attention/response
Milestone

Comments

@wwen0
Copy link

wwen0 commented Sep 6, 2024

Apple platform

iOS

Framework version

net8.0-*

Affected platform version

.NET6, .NET7, .NET8

Description

I created an iOS binding project with iOS xcframework (I also tried framework). The project built without any errors. I conditionally (iOS) referenced the binding project in my main project. But the namespace and the classes that were generated in the binding project's obj/Debug/net8/0-ios/iOS. I tried different ways to reference the binding project in the main project like these:


..\MauiDemoApp.Binding.iOS\bin\debug\net8.0-ios\MauiDemoApp.Binding.iOS.dll

or:


But when I tried referencing the namespace and the classes in the main project's Platforms/iOS/AppDelegate.cs, they were not recognized.

I created a GitHub repo for this: https://github.com/wwen0/MauiDebug

Steps to Reproduce

  1. Create the binding project, add in the xcframework, modify the ApiDefinition.cs and StructsAndEnums.cs to match the xcframework content.
  2. Build the binding project.
  3. After successful build of the binding project, conditionally add the binding project reference in the main project.
  4. Build the main project.
  5. In the main project's Platforms/iOS/AppDelegate.cs, try referencing the namespace defined in ApiDefinition.cs and StructsAndEnums.cs, and the classes in the xcframework. Red lines appear under the namespace and the class names, indicating they are not recognized.

Did you find any workaround?

No response

Build logs

No response

@wwen0 wwen0 added the binding-projects Issue or PR that affects binding projects label Sep 6, 2024
@rolfbjarne
Copy link
Member

5. Red lines appear under the namespace and the class names, indicating they are not recognized.

This is a known issue (#10148).

It's only intellisense that doesn't work, if you build the project, it'll build just fine.

Referencing the binding project directly, something like this from your main project, should work though:

<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'ios' ">
	<Reference Include="../MauiDemoApp.Binding.iOS/bin/Debug/net8.0-ios/Com.SAS.CI360.dll" />
</ItemGroup>

Note that in this case you'll have to build the binding project manually first, it won't be built automatically when you're building the main project.

Additionally, in your binding project, you should reference the .xcframework (and not the .framework inside the .xcframework):

<NativeReference Include="SASCollector.xcframework">

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

@rolfbjarne rolfbjarne added need-info Waiting for more information before the bug can be investigated no-auto-reply For internal use and removed need-info Waiting for more information before the bug can be investigated labels Sep 6, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the no-auto-reply For internal use label Sep 6, 2024
@rolfbjarne rolfbjarne added the need-info Waiting for more information before the bug can be investigated label Sep 6, 2024
@rolfbjarne rolfbjarne added this to the Future milestone Sep 6, 2024
@wwen0
Copy link
Author

wwen0 commented Sep 7, 2024

  1. Red lines appear under the namespace and the class names, indicating they are not recognized.

This is a known issue (#10148).

It's only intellisense that doesn't work, if you build the project, it'll build just fine.

Referencing the binding project directly, something like this from your main project, should work though:

<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'ios' ">
	<Reference Include="../MauiDemoApp.Binding.iOS/bin/Debug/net8.0-ios/Com.SAS.CI360.dll" />
</ItemGroup>

Note that in this case you'll have to build the binding project manually first, it won't be built automatically when you're building the main project.

Additionally, in your binding project, you should reference the .xcframework (and not the .framework inside the .xcframework):

<NativeReference Include="SASCollector.xcframework">

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

I tried, and the project ran without errors. However, even though the native methods' corresponding C# methods were called, there was no effect. It seemed that the native code was not invoked. Is there still something missing in my project? Do I need to add LinkWith in ApiDefinition.cs? The ApiDefinition.cs and StructsAndEnums.cs were copied from a previous Xamarin project. The Xamarin project worked without any issue.

@microsoft-github-policy-service microsoft-github-policy-service bot added need-attention An issue requires our attention/response and removed need-info Waiting for more information before the bug can be investigated labels Sep 7, 2024
@rolfbjarne
Copy link
Member

  1. Red lines appear under the namespace and the class names, indicating they are not recognized.

This is a known issue (#10148).
It's only intellisense that doesn't work, if you build the project, it'll build just fine.
Referencing the binding project directly, something like this from your main project, should work though:

<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'ios' ">
	<Reference Include="../MauiDemoApp.Binding.iOS/bin/Debug/net8.0-ios/Com.SAS.CI360.dll" />
</ItemGroup>

Note that in this case you'll have to build the binding project manually first, it won't be built automatically when you're building the main project.
Additionally, in your binding project, you should reference the .xcframework (and not the .framework inside the .xcframework):

<NativeReference Include="SASCollector.xcframework">

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

I tried, and the project ran without errors. However, even though the native methods' corresponding C# methods were called, there was no effect. It seemed that the native code was not invoked. Is there still something missing in my project? Do I need to add LinkWith in ApiDefinition.cs? The ApiDefinition.cs and StructsAndEnums.cs were copied from a previous Xamarin project. The Xamarin project worked without any issue.

Can you update your GitHub project with exactly what you did?

@rolfbjarne rolfbjarne added need-info Waiting for more information before the bug can be investigated no-auto-reply For internal use and removed need-attention An issue requires our attention/response labels Sep 9, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the no-auto-reply For internal use label Sep 9, 2024
@wwen0
Copy link
Author

wwen0 commented Sep 9, 2024

I already updated my Github project, with the exception of a plist file that is not essential to test. This line "SASLogger.SetLevel(SASLoggerLevel.All);" in AppDelegate.cs should provide logs such as: INFO:..., DEBUG:...

I also changed xcframework to framework, because if I run with xcframework it will complain that I cannot do it on a simulator. So I get out the framework from xcframework that is for a simulator.

@microsoft-github-policy-service microsoft-github-policy-service bot added need-attention An issue requires our attention/response and removed need-info Waiting for more information before the bug can be investigated labels Sep 9, 2024
@rolfbjarne
Copy link
Member

I get this message:

WARN: SASCollector initialization requested without full setup

Note that some types of messages from native code might not show up in the IDE (ref #21180), so you might have to use the iOS Device Log to see them (https://support.apple.com/en-in/guide/console/cnsl1012/mac)

Can you do that and check if you see anything?

@rolfbjarne rolfbjarne added need-info Waiting for more information before the bug can be investigated no-auto-reply For internal use and removed need-attention An issue requires our attention/response labels Sep 9, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the no-auto-reply For internal use label Sep 9, 2024
@wwen0
Copy link
Author

wwen0 commented Sep 9, 2024

I got the same warning. That is from the native library. So that confirms there is still log. It's just that I wasn't expect to see this. The reason that I got this warning even though I have the plist file is because the native library didn't find it. This is what I added in csproj file:

<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'iOS' ">
   <BundleResource Include="./Platforms/iOS/SASCollector.plist" />
</ItemGroup>

Is it correct?

@microsoft-github-policy-service microsoft-github-policy-service bot added need-attention An issue requires our attention/response and removed need-info Waiting for more information before the bug can be investigated labels Sep 9, 2024
@rolfbjarne
Copy link
Member

Where is this plist supposed to be located in the app bundle? In the root directory?

@wwen0
Copy link
Author

wwen0 commented Sep 9, 2024

In native app, it should be in the main bundle. Anywhere in the native app is fine because all files in different folders are flattened in one place.

@rolfbjarne
Copy link
Member

Try this:

<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'iOS' ">
   <BundleResource Include="./Platforms/iOS/SASCollector.plist" Link="SASCollector.plist" />
</ItemGroup>

@wwen0
Copy link
Author

wwen0 commented Sep 9, 2024

Try this:

<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'iOS' ">
   <BundleResource Include="./Platforms/iOS/SASCollector.plist" Link="SASCollector.plist" />
</ItemGroup>

Adding "Link" attribute solved the problem. Thank you very much @rolfbjarne
One thing I noticed is that unlike in iOS, in Android as long as I put the .properties file in Platforms/Android/Assets folder, it worked without any issue. I do not even need to add BundleResource. In Android, the file has .properties extension.

@rolfbjarne
Copy link
Member

One thing I noticed is that unlike in iOS, in Android as long as I put the .properties file in Platforms/Android/Assets folder, it worked without any issue. I do not even need to add BundleResource. In Android, the file has .properties extension.

This might also have worked if you put the SASCollector.plist in the Platforms/iOS/Resources directory.

In any case, I'm glad it's working for you now.

@borrmann
Copy link

I seem to have similars issues both with BranchSDK and AppsFlyerSDK when building with an Intel (amd no problem).

<PackageReference Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'" Include="AppsFlyerXamarinBinding" Version="6.13.1" />
leads to
C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk.net8.0_18.0\18.0.8303\tools\msbuild\iOS\Xamarin.Shared.targets(154,3): error : The xcframework C:\Users\MaxBorrmann\.nuget\packages\appsflyerxamarinbinding\6.13.1\lib\net6.0-ios16.1\AppsFlyerBinding.iOS.resources.zip has an incorrect or unknown format and cannot be processed.

@rolfbjarne do you have any idea what I could do about that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
binding-projects Issue or PR that affects binding projects need-attention An issue requires our attention/response
Projects
None yet
Development

No branches or pull requests

3 participants