绑定 Android Kotlin 库Bind Android Kotlin libraries
02/11/2020
本文内容
重要
我们当前正在调查 Xamarin 平台上的自定义绑定使用情况。We're currently investigating custom binding usage on the Xamarin platform. 请参与此调查,告诉我们将来应该进行哪些开发工作。Please take this survey to inform future development efforts.
Android 平台及其本地语言和工具正在不断发展,并且已经有许多使用最新产品/服务开发的第三方库。The Android platform, along with its native languages and tooling, is constantly evolving and there are plenty of third-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. 随着 Xamarin 开发人员在开发人员队伍中的受欢迎程度不断提高,重用 Kotlin 生成的组件的能力已变得越来越重要。The ability to reuse components built with Kotlin has become increasingly important to Xamarin developers as their popularity amongst developers continues to grow. 你可能已熟悉绑定常规 Java 库的过程。You may already be familiar with the process of binding regular Java libraries. 现在可以使用其他文档介绍绑定 Kotlin 库的过程,以便 Xamarin 应用程序可以采用相同的方式使用它们。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. 本文档旨在介绍为 Xamarin 创建 Kotlin 绑定的高级方法。The purpose of this document is to describe a high-level approach to create a Kotlin Binding for Xamarin.
高级方法High-level approach
借助 Xamarin,可以绑定任何第三方本机库,使其可供 Xamarin 应用程序使用。With Xamarin, you can bind any third-party native library to be consumable by a Xamarin application. 由于 Kotlin 是新语言,因此,为使用该语言生成的库创建绑定需要一些其他步骤和工具。Kotlin is the new language and creating binding for libraries built with this language requires some additional steps and tooling. 此方法涉及以下四个步骤:This approach involves the following four steps:
生成本机库Build the native library
准备 Xamarin 元数据,使 Xamarin 工具能够生成 C# 类Prepare the Xamarin metadata, which enables Xamarin tooling to generate C# classes
使用本机库和元数据生成 Xamarin 绑定库Build a Xamarin Binding Library using the native library and the metadata
在 Xamarin 应用程序中使用 Xamarin 绑定库Consume the Xamarin Binding Library in a Xamarin application
以下各节详细介绍了这些步骤。The following sections outline these steps with additional details.
生成本机库Build the native library
第一步是获取本机 Kotlin 库(即 AAR 包,它是一个 Android 存档)。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.
准备 Xamarin 元数据Prepare the Xamarin metadata
第二步是准备元数据转换文件,Xamarin 工具将使用该文件来生成相应的 C# 类。The second step is to prepare the metadata transform file, which will be used by the Xamarin tools to generate the respective C# classes. 在最理想的情况下,此文件可能是空的,其中所有类均可通过 Xamarin 工具发现和生成。In the best case scenario, this file could be empty where all classes are discovered and generated by the Xamarin tools. 在某些情况下,必须应用元数据转换才能生成正确的和/或所需的 C# 代码。In some cases, metadata transformation must be applied to generate correct and/or desired C# code. 在许多情况下,必须使用 AAR 反汇编程序(如 Java 反编译工具 (JD))来识别隐藏的依赖项,以及需要从要生成的 C# 类的最终列表中排除的不需要的类。In many cases, an AAR disassembler, such as Java Decompiler (JD), must be used to identify hidden dependencies and unwanted classes that you wish to exclude from the final list of C# classes to be generated. 最终的元数据应表示引用的 Xamarin.Android 应用程序将与之交互的公共接口。The final metadata should represent the public interface in which the referencing Xamarin.Android application will interact with.
生成 Xamarin.Android 绑定库Build a Xamarin.Android binding library
第三步是创建一个特殊的项目,即一个 Xamarin.Android 绑定库。The third step is to create a special project - a Xamarin.Android Binding Library. 它包括 Kotlin 库作为本机引用,以及上一步中定义的元数据转换。It includes the Kotlin libraries as native references and the metadata transformation defined in the previous step. 在撰写本文时,每个被引用的 AAR 包都需要一个单独的 Android 绑定库项目。At time of writing, a separate Android Binding Library project is required for each AAR package being referenced. 绑定库必须添加 Xamarin.Kotlin.StdLib 包才能支持 Kotlin 标准库。The Binding Library must add the Xamarin.Kotlin.StdLib package in order to support the Kotlin Standard Library.
使用 Xamarin 绑定库Consume the Xamarin binding library
第四步(也是最后一步)是在 Xamarin.Android 应用程序中引用绑定库。The fourth and the final step is to reference the binding library in a Xamarin.Android application. 通过添加对 Xamarin.Android 绑定库的引用,你的 Xamarin 应用程序可以使用该包中公开的 Kotlin 类。Adding a reference to the Xamarin.Android Binding Library enables your Xamarin application to use the exposed Kotlin classes from within that package.
演练Walkthrough
上述方法概括了为 Xamarin 创建 Kotlin 绑定所需的高级步骤。The approach above outlines the high-level steps required to create a Kotlin Binding for Xamarin. 在实践中准备这些绑定时,涉及许多较低级别的步骤,并需要考虑更多详细信息,包括适应本机工具和语言的更改。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.
相关链接Related links