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

[maui] Update Native Library Interop docs for net9.0 #487

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
26 changes: 16 additions & 10 deletions docs/maui/native-library-interop/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Install prerequisites:
- [.NET MAUI workload](https://github.com/dotnet/core/blob/main/release-notes/6.0/install-maui.md#cli-installation) (via Visual Studio or CLI ```dotnet workload install maui```)
- [Android SDK](https://developer.android.com/tools)
- [Android Studio](https://developer.android.com/studio)
- [Objective-Sharpie](https://aka.ms/objective-sharpie) (required for the C# APIs to be auto-generated)
- [Objective-Sharpie](https://aka.ms/objective-sharpie) (used to manually generate the C# APIs)
- [Xamarin.iOS](https://download.visualstudio.microsoft.com/download/pr/ceb0ea3f-4db8-46b4-8dc3-8049d27c0107/3960868aa9b1946a6c77668c3f3334ee/xamarin.ios-16.4.0.23.pkg) (required for Objective-Sharpie to work)
- [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Visual Studio Code](https://code.visualstudio.com/download)
- [Xcode](https://developer.apple.com/xcode/)
Expand Down Expand Up @@ -92,9 +92,16 @@ On the native side, make updates in _template/macios/native/NewBinding/NewBindin

Back on the .NET side, we are now ready to interop with the native library:

1. Run `dotnet build` from _template/macios/NewBinding.MaciOS.Binding_ to test everything is plugged in correctly and good to go. If successful, you should see the generated C# bindings at _template/macios/native/NewBinding/bin/Release/net8.0-ios/sharpie/NewBinding/ApiDefinitions.cs_.
1. Update the contents of _template/macios/NewBinding.MaciOS.Binding/ApiDefinition.cs_ by replacing it with the contents of _template/macios/native/NewBinding/bin/Release/net8.0-ios/sharpie/NewBinding/ApiDefinitions.cs_.
1. Run `dotnet build` from _template/macios/NewBinding.MaciOS.Binding_ again.
1. Run `dotnet build` from _template/macios/NewBinding.MaciOS.Binding_ to test everything is plugged in correctly and good to go.
1. Optionally use objective sharpie to generate an updated ApiDefinitions.cs file that reflects any changes in your swift code.
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
1. Run `sharpie xcode -sdks` to get a list of valid target SDK values for the bind command. Select the value that aligns with the platform and version you are targeting to use with the next command, for example `iphoneos18.0`.
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
1. Run `sharpie bind` against the header files in the xcframework created by the binding project:
```
cd _template/macios/native/NewBinding/bin/Debug/net9.0-ios/NewBinding.MaciOS.Binding.resources/NewBindingiOS.xcframework/ios-arm64/NewBinding.framework &&
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
sharpie bind --output=sharpie-out --namespace=NewBindingMaciOS --sdk=iphoneos18.0 --scope=Headers Headers/NewBinding-Swift.h
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
```
1. Update the contents of _template/macios/NewBinding.MaciOS.Binding/ApiDefinition.cs_ by replacing it with the contents of _sharpie-out/ApiDefinitions.cs_.
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
1. Run `dotnet build` from _template/macios/NewBinding.MaciOS.Binding_ again.

#### API Definition: Android

Expand All @@ -106,17 +113,16 @@ On the native side, make updates in _template/android/native/app/src/main/java/c

Back on the .NET side, we are now ready to interop with the native library:
1. Run `dotnet build` from _template/android/NewBinding.Android.Binding_ to test everything is plugged in correctly and good to go. (Note: This step will require that you have JDK 17 installed)
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
1. Reference any Android binding dependencies by adding the following code to _template/sample/MauiSample.csproj_ for each dependency. Replace _{yourDependencyLibrary.aar}_ with the required .aar file for the dependency you are binding. (Note: The gradle/maven dependencies often need to be explicitly referenced, as they are not automatically bundled into your library. The _build.gradle.kts_ file is configured to copy dependencies into a bin/outputs/deps folder for you to reference in your application)
1. Reference any Android binding dependencies by adding `@(AndroidMavenLibrary)` or `@(PackageReference)` elements to satisfy the java dependency chain for the native library you are binding. (Note: The gradle/maven dependencies often need to be explicitly referenced, as they are not automatically bundled into your library.)
pjcollins marked this conversation as resolved.
Show resolved Hide resolved

```xml
<ItemGroup Condition="$(TargetFramework.Contains('android'))">
<AndroidLibrary Include="..\android\native\newbinding\bin\Release\net8.0-android\outputs\deps\{yourDependencyLibrary.aar}">
<Bind>false</Bind>
<Visible>false</Visible>
</AndroidLibrary>
<AndroidMavenLibrary Include="my.library:dependency-library" Version="1.0.0" Bind="false" />
pjcollins marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
```

For more information about this process, see the [AndroidMavenLibrary reference](/dotnet/android/binding-libs/advanced-concepts/android-maven-library) documentation.

> [!NOTE]
> You can rename the placeholder ```DotnetNewBinding``` class to better reflect the native library being wrapped. For more examples and tips for writing the API definitions, read more in the section below: [Modify an existing binding](#modify-an-existing-binding).

Expand Down Expand Up @@ -202,7 +208,7 @@ public static func unregister(completion: @escaping (NSError?) -> Void) {
The other half will be to update the _ApiDefinitions.cs_ file in the binding project to expose this new method. There are two ways you can go about this:

1. You can manually add the required code
2. When the binding project builds, objective sharpie is run and an _ApiDefinitions.cs_ file is generated inside of the _native/macios/bin/sharpie_ folder (this path will vary based on the project you are working on of course). You can try to find the relevant changes from this file and copy them over manually, or try copying over the whole file and looking at the diff to find the part you need.
2. After building the binding project, you can run the objective sharpie tool to generate an _ApiDefinitions.cs_ file. You can try to find the relevant changes from this file and copy them over manually, or try copying over the whole file and looking at the diff to find the part you need.

In this case, the changes to _ApiDefinitions.cs_ would be:

Expand Down
16 changes: 7 additions & 9 deletions docs/maui/native-library-interop/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,39 @@ The binding build process is extended to obtain and build native SDK dependencie
</ItemGroup>
```

Android binding projects will add a `@(NLIGradleProjectReference)` item that points to the root folder that contains the native wrapper gradle project:
Android binding projects will add a `@(AndroidGradleProject)` item that points to a build.gradle file that will be used to build the gradle project:

```xml
<ItemGroup>
<NLIGradleProjectReference Include="../native" >
<AndroidGradleProject Include="../native/build.gradle.kts" >
<ModuleName>newbinding</ModuleName>
<!-- Metadata applicable to @(AndroidLibrary) will be used if set, otherwise the following defaults will be used:
<Bind>true</Bind>
<Pack>true</Pack>
-->
</NLIGradleProjectReference>
</AndroidGradleProject>
</ItemGroup>
```

iOS binding projects will add an `@(NLIXcodeProjectReference)` item that points to the native wrapper Xcode project:
iOS binding projects will add an `@(XcodeProject)` item that points to the native wrapper Xcode project:

```xml
<ItemGroup>
<NLIXcodeProjectReference Include="../native/NewBinding/NewBinding.xcodeproj">
<XcodeProject Include="../native/NewBinding/NewBinding.xcodeproj">
<SchemeName>NewBinding</SchemeName>
<SharpieNamespace>NewBinding</SharpieNamespace>
<SharpieBind>true</SharpieBind>
<!-- Metadata applicable to @(NativeReference) will be used if set, otherwise the following defaults will be used:
<Kind>Framework</Kind>
<SmartLink>true</SmartLink>
-->
</NLIXcodeProjectReference>
</XcodeProject>
</ItemGroup>
```

Android binding projects generate the API definition automatically taking into account any optional manual modifications like those implemented via the [Metadata.xml](/previous-versions/xamarin/android/platform/binding-java-library/customizing-bindings/java-bindings-metadata#metadataxml-transform-file) transform file.

![Conceptual overview: NativeLibraryInterop for Android](../images/native-library-interop/nativelibraryinterop-conceptual-overview-android.png "Conceptual overview of NativeLibraryInterop for Android")

An iOS binding library project must include an explicitly defined API. To help with this, [Objective-Sharpie](/xamarin/cross-platform/macios/binding/objective-sharpie/#overview) can be run automatically on the resulting native framework to produce an [API definition file](/xamarin/cross-platform/macios/binding/objective-c-libraries?tabs=macos#The_API_definition_file) (ApiDefinition.cs) alongside it. This can serve as a helpful reference when creating and maintaining the ApiDefintion.cs file used by the iOS binding project.
An iOS binding library project must include an explicitly defined API. To help with this, [Objective-Sharpie](/xamarin/cross-platform/macios/binding/objective-sharpie/#overview) can be manually run on the resulting native framework to produce an [API definition file](/xamarin/cross-platform/macios/binding/objective-c-libraries?tabs=macos#The_API_definition_file) (ApiDefinition.cs) alongside it. This can serve as a helpful reference when creating and maintaining the ApiDefintion.cs file used by the iOS binding project.
pjcollins marked this conversation as resolved.
Show resolved Hide resolved

![Conceptual overview: NativeLibraryInterop for iOS](../images/native-library-interop/nativelibraryinterop-conceptual-overview-ios.png "Conceptual overview of NativeLibraryInterop for iOS")

Expand Down