Binding Android Kotlin Libraries

alexeystrakh

The Android platform, along with its native languages and tooling, is constantly evolving and there are plenty of 3rd party libraries that have been developed using the latest offerings. Maximizing code and component reuse is one of the key goals of cross-platform development. The ability to reuse components built with Kotlin has become increasingly important to Xamarin developers as their popularity amongst developers continues to grow. You may already be familiar with the process of binding regular Java libraries. Additional documentation is now available describing the process of Binding a Kotlin Library, so they are consumable by a Xamarin application in the same manner. The purpose of this document is to describe a high-level approach to create a Kotlin Binding for Xamarin.

High-level Approach

With Xamarin you can bind any 3d party native library to be consumable by a Xamarin application. Kotlin is the new language and creating binding for libraries built with this language requires some additional steps and tooling. This involves the following four steps:

  1. Building the native library
  2. Preparing the Xamarin metadata which enables Xamarin tooling to generate C# classes
  3. Building a Xamarin Binding Library using the native library and the metadata
  4. Consuming the Xamarin Binding Library in a Xamarin application

The following guide outlines these steps with additional details.

Binding a Kotlin Library

Build the native library

The first step is to obtain a native Kotlin library (.aar package, which is an Android archive). You can either request it directly from a vendor or build it yourself.

Prepare the Xamarin metadata

The second step is to prepare the metadata transform file which will be used by the Xamarin tools to generate the respective C# classes. In the best-case scenario, this file could be empty where Xamarin tools will discover discovered and generated all classes. In some cases, you have to apply a metadata transformation in order to generate correct and/or desired C# code. You also need an .aar disassembler, such as Java Decompiler (JD), to identify hidden dependencies and unwanted classes that you wish to exclude from the final list of C# classes to be generated. The final metadata should represent the public interface in which the referencing Xamarin.Android application will interact with.

Build a Xamarin.Android binding library

The third step is to create a special kind of project – a Xamarin.Android Binding Library. It includes the Kotlin libraries as native references and the metadata transformation defined in the previous step. At the time of writing, a separate Android Binding Library project is required for each .aar package being referenced. The Binding Library must add the Xamarin.Kotlin.StdLib package in order to support the Kotlin Standard Library.

Consume the Xamarin binding library

The fourth and final step is to reference the binding library in a Xamarin.Android application. Adding a reference to the Xamarin.Android Binding Library enables your Xamarin application to use the exposed Kotlin classes from within that package.

Bubble Picker Demo

Walkthrough

The approach above outlines the high-level steps required to create a Kotlin Binding for Xamarin. However, there are many lower-level steps involved and further details to consider when preparing these bindings in practice including adapting to changes in the native tools and languages. The intent is to help you to gain a deeper understanding of this concept and the high-level steps involved in this process. For a detailed step-by-step guide refer to the Xamarin Kotlin Binding Walkthrough documentation.

0 comments

Discussion is closed.

Feedback usabilla icon