Binding Android Kotlin Libraries for .NET MAUI

Introduction

In this article, we are going to create and understand how to create a native Android Kotlin library wrapper for the Maui project, and creating a binding for Maui involves multiple steps, it is the same as what we’ve done for Xamarin, but we need to add additional configuration to the library and project. In this solution, we are going to create two library projects and the Maui App Project. The first library is for the native library and then the MAUI Library. This library will be used in the MAUI project. Before, starting the project, download the JAR or AAR files for bindings. 

Table of Contents:

  1. Prerequisites
  2. Maui Binding with Kotlin Library
  3. Handling Binding Issues
  4. Pros and Cons of Binding Kotlin or Java Library with .NET MAUI
  5. Conclusion
  6. References

Prerequisites

  • Visual Studio for Mac
  • JAR or AAR files
  • .NET Decompiler (JetBrains dotPeek) – to view the generated binding dll API’s
  • Java Decompiler (Java Decompiler) – to view the classes in the JAR or AAR

To bind a Kotlin library with .NET Maui, you need follow the below step-by-step guide.

Binding Process Steps:

  1. Create a new Android Java Library Binding project in Visual Studio
  2. Add the Kotlin Library (JAR or AAR) to the project
  3. Resolve any binding issues
  4. Build the project to generate the DLL.
  5. Use the DLL in you .NET MAUI project.
💡 Info: Before starting binding, create a sample Kotlin or Java project and check the JAR or AAR is working fine.

Step 1: Let’s Create a Android Java Binding Library project:

To create a binding library, open Visual Studio for Mac, Click +New, on the left blade select Android Java Library Binding under the Android section, then click next, in the next window, provide the project and solution name, then click the Create button.

Configure your new Android Java Library Binding

After the project creation, you can see the solution architecture like below, then add you JAR files into the project, by right clicking on project solution, followed by add >> files, it will open finder/explorer window, select all JAR files.

Now, add Xamarin.Kotlin.StdLib.Jdk8 NuGet package to your project. After that, open the Project .csproj file package reference is added properly like below.

Now, Build the project, It must be built successfully.

💡 Note: sometimes, if the JAR has any external JAR dependency, then you should add those JAR as well. In my case, it is dependent on two JAR files, so I added two JAR file in the project.

Create a .NET Maui Library Project.

Create an MAUI library project in the same solution by right-clicking the solution and adding the .NET MAUI Class Library project.

Configure your new .NET MAUl Class Library

After the project creation, add the reference of Android Native Binding Library to this Maui Library project like below and rebuild the project.

💡 Info: if you have seen any errors while binding the project with multiple targets, please add the target condition configuration to the project reference.

In this library project, we’ve a platform class for Android and iOS. So, call the native API from those platform classes.

For example, I have to create a binding for the music recommendation engine JAR and need to call the native API to get recommended music for the user. As I guided before, we have created the binding of the library and Maui library for Music recommendation. In this library, we have two APIs to get the music recommended, one for trending commendation and another for the current user recommendation.  

Now, Create a new class under the platform class in Maui Library and name it MusicRecommendationWrapper.cs. Initialise the native class in the constructor, and write the wrapper methods for native APIs. 

Create Maui Application

Now, Create a new Maui App by selecting, new Project >> Cross Platform >> Maui Application, and add to the same solution. Give the application name as MusicApp.

Now, Open Main.xaml.cs file and initialise the wrapper class in the constructor. Add a button & click the event for the button, and add the wrapper method call in this button to get the result.

Finally, we created a binding and used in Maui application.

Handling Binding Issues

See, I faced binding issues while creating a Kotlin binding for Maui, and added a few common ways to fix the binding issues. Before starting, create a Metadata.xml file in the Java Library Binding project

  • Removing a class or method: If a class or method causing issues in Kotlin and is not needed, you can remove it from binding,
  • Renaming a class, method, or parameter: If a name has conflict in Kotlin and C#, you can rename the class, method or parameter.
  • Changing the return type or parameter type of a method: If a method’s return type of parameter type causes an issue, you can change it.
  • Changing the type of field: If the fields type is causes an issue, you can change it.

Pros and Cons of Binding Kotlin or Java Library with .NET MAUI:

In the context of programming, Binding refers to the process of creating the bridge between .NET MAUI and Kotlin or Java Code. Like, Connecting two pieces of information or data.

Pros

  1. Code Reusability: Binding allows you to use the existing Kotlin or Java Code in your .NET MAUI Code. It can save you a lot of time and effort as you don’t need to rewrite the same functionality in C#.
  2. Access To Native Features: Some Android features might only be available through the library.
  3. Interoperability: Binding allows you to use both C# and Kotlin or Java in the same project. So you can use the best language for each task.

Cons

  1. Complexity: Creating Binding can be complex, especially for large libraries and libraries that use features not supported by .NET
  2. Maintenance: If your Java library is updated, you might need to update the binding as well. This can be a time-consuming, and error-prone.
  3. Performance: There might be slight performance overhead, when calling Kotlin or Java code from C# through bindings.
  4. Debugging: Debugging issues in the Kotlin library can be difficult from MAUI, You might need to use Java or Kotlin IDE for debugging.
  5. Name conflict: Conflicts between native library names and reserved names in c# can cause issues. These need to be manually fixed in the Metadata.xml file.
  6. Limited support for Java library: Some Java features are not supported in Maui. such as anonymous inner classes, if you have those features, you might not be able to bind them.
  7. Limited support for Kotlin library: same as Java, for Kotlin, Maui provides limited support.
  8. Platform-specific: Need to maintain two library for Android and iOS.

Conclusion

We have completed the wrapper for the Maui application with Android Kotlin code. This wrapper allows us to utilize the native Android Kotlin Library within our Maui project. We achieved this by creating two library projects and a Maui App project, integrating the necessary JAR files, and establishing the necessary references and configurations. We then created a .NET Maui Library project and added the reference for the Android Native Binding Library. Finally, we created a Maui application and integrated the wrapper class for our native APIs. This process allows us to seamlessly utilize native Kotlin features within our Maui application.


For more information, refer the below documents

Microsoft Docs – Binding Library

John Douglus gist – Link

Troubleshooting – Link

If you encounter any issues related to binding libraries, please share them here. And if this blog has been helpful, feel free to post your feedback.

 83 total views,  3 views today

Leave a Comment

Your email address will not be published. Required fields are marked *