绑定 Eclipse 库项目Binding an Eclipse Library Project
03/01/2018
本文内容
重要
我们当前正在调查 Xamarin 平台上的自定义绑定使用情况。We're currently investigating custom binding usage on the Xamarin platform. 请参与此调查,告诉我们将来应该进行哪些开发工作。Please take this survey to inform future development efforts.
本演练介绍了如何使用 Xamarin.Android 项目模板来绑定 Eclipse Android 库项目。This walkthrough explains how to use Xamarin.Android project templates to bind an Eclipse Android library project.
概述Overview
尽管 .AAR 文件正日益成为 Android 库分发的规范,但在某些情况下,仍有必要为 Android 库项目创建绑定。Although .AAR files are increasingly becoming the norm for Android library distribution, in some cases it is necessary to create a binding for an Android library project. Android 库项目是特殊的 Android 项目,其中包含可由 Android 应用程序项目引用的可共享代码和资源。Android library projects are special Android projects that contain shareable code and resources that can be referenced by Android application projects. 通常,在 Eclipse IDE 中创建库时,需要绑定到 Android 库项目。Typically, you bind to an Android library project when the library is created in the Eclipse IDE.
本演练举例说明了如何从 Eclipse 项目的目录结构创建 Android 库项目 .ZIP。This walkthrough provides examples of how to create an Android library project .ZIP from the directory structure of an Eclipse project.
Android 库项目与常规 Android 项目的不同之处在于,前者没有编译到 APK 中,也不能自行部署到设备上。Android library projects are different from regular Android projects in that they are not compiled into an APK and are not, on their own, deployable to a device. 相反,Android 库项目应由 Android 应用程序项目引用。Instead, an Android library project is meant to be referenced by an Android application project. 生成 Android 应用程序项目时,首先编译的是 Android 库项目。When an Android application project is built, the Android library project is compiled first. 然后,Android 应用程序项目会被并入已编译的 Android 库项目中,并将代码和资源添加到 APK 中以供分发。The Android application project will then be absorbed into the compiled Android library project and include the code and resources into the APK for distribution. 正是由于有这种差异,为 Android 库项目创建绑定与为 Java .JAR 或 .AAR 文件创建绑定略有不同。Because of this difference, creating a binding for an Android library project is slightly different than creating a binding for a Java .JAR or .AAR file.
演练Walkthrough
若要在 Xamarin.Android Java 绑定项目中使用 Android 库项目,首先需要在 Eclipse 中生成 Android 库项目。To use an Android library project in a Xamarin.Android Java Binding project it is first necessary to build the Android library project in Eclipse. 下面的屏幕截图展示了一个编译后 Android 库项目示例:The following screenshot shows an example of one Android library project after compilation:
请注意,Android 库项目中的源代码已编译到临时 .JAR 文件 android-mapviewballoons.jar 中,且资源已复制到 bin/res/crunch 文件夹中。Notice that the source code from the Android library project has been compiled to a temporary .JAR file named android-mapviewballoons.jar, and that the resources have been copied to the bin/res/crunch folder.
在 Eclipse 中编译完 Android 库项目后,便可以使用 Xamarin.Androidn Java 绑定项目来绑定它。Once the Android library project has been compiled in Eclipse, it can then be bound using a Xamarin.Android Java Binding project. 首先,必须创建 .ZIP 文件,其中包含 Android 库项目的 bin 和 res 文件夹。First a .ZIP file must be created which contains the bin and res folders of the Android library project. 请务必删除干预 crunch 子目录,让资源驻留在 bin/res 中。下面的屏幕截图展示了一个此类 .ZIP 文件的内容:It is important that you remove the intervening crunch subdirectory so that the resources reside in bin/res. The following screenshot shows the contents of one such .ZIP file:
然后,将此 .ZIP 文件添加到 Xamarin.Android Java 绑定项目中,如下面的屏幕截图所示:This .ZIP file is then added to Xamarin.Android Java Binding project, as shown in the following screenshot:
请注意,.ZIP 文件的“生成操作”已自动设置为“LibraryProjectZip”。Notice that the Build Action of the .ZIP file has been automatically set to LibraryProjectZip.
若有 Android 库项目需要的任何 .JAR 文件,应将它们添加到 Java 绑定库项目的 Jar 文件夹中,并将“生成操作”设置为“ReferenceJar”。If there are any .JAR files that are required by the Android library project, they should be added to the Jars folder of the Java Binding Library project and the Build Action set to ReferenceJar. 下面的屏幕截图中举例说明了这种情况:An example of this can be seen in the screenshot below:
完成这些步骤后,可以按本文档前面所述,使用 Xamarin.Android Java 绑定项目。Once these steps are complete, the Xamarin.Android Java Binding project can be used as described earlier on in this document.
备注
暂不支持在其他 IDE 中编译 Android 库项目。Compiling the Android library projects in other IDEs is not supported at this time. 其他 IDE 可能不会在 bin 文件夹中创建与 Eclipse 相同的目录结构或文件。Other IDEs may not create the same directory structure or files in the bin folder as Eclipse.
总结Summary
本文逐步介绍了绑定 Android 库项目的过程。In this article, we walked through the process of binding an Android library project. 我们先是在 Eclipse 中生成了 Android 库项目,然后从 Android 库项目的 bin 和 res 文件夹创建了 zip 文件。We built the Android library project in Eclipse, then we created a zip file from the bin and res folders of the Android library project. 接下来,我们使用此 zip 创建了 Xamarin.Android Java 绑定项目。Next, we used this zip to create a Xamarin.Android Java Binding project.