更智能的 Xamarin Android 支持 v4 / v13 NuGet 包Smarter Xamarin Android Support v4 / v13 NuGet Packages

关于 Android 支持库About the Android Support Libraries

Google 已创建支持库,使新功能可用于较早版本的 Android。Google has created support libraries to make new features available to older versions of Android. 一般情况下,支持库在其名称中提供版本号,这是与兼容的最低 Android API 级别(例如:支持-v4 只能在 API 级别4和更高版本上使用。In general, Support Libraries are given a version number in their name, which is the lowest Android API Level they are compatible with (eg: Support-v4 can only be used on API Level 4 and higher. 有关详细信息,请Stack Overflow 讨论)。More info in this Stack Overflow discussion).

两个支持库:不能在同一应用中一起使用 Support-v4Support-v13,也就是说,它们是互斥的。Two of the support libraries: Support-v4 and Support-v13 can not be used together in the same app, that is, they are mutually exclusive. 这是因为 Support-v13 实际上包含 Support-v4的所有类型和实现。This is because Support-v13 actually contains all of the types and implementation of Support-v4. 如果尝试在同一项目中同时引用这两个项目,则会遇到重复的类型错误。If you try and reference both in the same project you will encounter duplicate type errors.

引用问题Problems with Referencing

由于 Support-v4 现已变得非常普遍,因此许多第三方库现在依赖于它。Since Support-v4 has become so popular, a lot of 3rd party libraries now depend on it. 他们可以选择依赖于 v13,但更常见的做法是依赖于_v4_ ,因为使用这些第三方库的任何应用都可以选择支持 API 级别。They could have chosen to depend on Support-v13 instead, but it's more common to depend on v4 since that gives any apps using these 3rd party libraries the option of supporting API levels all the way down to 4.

如果 Xamarin 第三方库引用 Xamarin.Android.Support.v4.dll 绑定到 Support-v4,则使用此库的任何应用程序还必须引用 Xamarin.Android.Support.v4.dllIf a Xamarin 3rd party library references the Xamarin.Android.Support.v4.dll binding to Support-v4, any app that uses this library must also reference Xamarin.Android.Support.v4.dll. 当同一个应用程序还希望使用 Xamarin.Android.Support.v13.dll 绑定中的某些功能来 Support-v13时,这会成为一个问题。This becomes a problem when the same app also wants to use some of the functionality from the Xamarin.Android.Support.v13.dll binding to Support-v13. 如果同时引用这两个绑定,则会遇到重复的类型错误。If you reference both bindings, you will encounter duplicate type errors.

类型转发 v4 绑定程序集Type-Forwarded v4 Binding Assembly

若要解决此问题,我们创建了一个没有实现的特殊 Xamarin.Android.Support.v4.dll 程序集,但只是 [assembly: TypeForwardedTo (..)] 属性,这些属性将所有 Support-v4 类型转发到 Xamarin.Android.Support.v13.dll 程序集内的实现。To get around this problem, we have created a special Xamarin.Android.Support.v4.dll assembly which has no implementation, but simply [assembly: TypeForwardedTo (..)] attributes which forward all of the Support-v4 types to the implementation within the Xamarin.Android.Support.v13.dll assembly.

这意味着开发人员可以在其应用程序中引用此_类型转发的_程序集,这将满足任何第三方库对 Xamarin.Android.Support.v4.dll 的引用,同时仍允许在应用中使用 Xamarin.Android.Support.v13.dllThis means a developer can reference this type-forwarded assembly in their app which will satisfy the reference to Xamarin.Android.Support.v4.dll by any 3rd party libraries, while still allowing Xamarin.Android.Support.v13.dll to be used in the app.

NuGet 帮助NuGet Assistance

尽管开发人员可以手动添加所需的正确引用,但在安装 NuGet 包时,我们可以使用 NuGet 来帮助选择正确的程序集(常规_v4_绑定或类型转发_v4_程序集)。While a developer could manually add the correct references necessary, we are able to use NuGet to help choose the right assembly (either the normal v4 binding or the type-forwarded v4 assembly) when the NuGet package is installed.

因此,Xamarin.Android.Support.v4 NuGet 包现在包含以下逻辑:So, the Xamarin.Android.Support.v4 NuGet package now contains the following logic:

如果你的应用面向 API 级别13(Gingerbread 3.2)或更高版本:If your app is targeting API Level 13 (Gingerbread 3.2) or higher:

  • Xamarin.Android.Support.v13 NuGet 将自动添加为依赖项Xamarin.Android.Support.v13 NuGet will automatically be added as a dependency
  • 将在项目中引用_类型转发_的 Xamarin.Android.Support.v4.dllThe type-forwarded Xamarin.Android.Support.v4.dll will be referenced in the project

如果你的应用面向的是低于 API 级别13的任何内容,你将获得你的项目中引用的普通 Xamarin.Android.Support.v4.dll 绑定。If your app is targeting anything lower than API Level 13, you will get the normal Xamarin.Android.Support.v4.dll binding referenced in your project.

我是否必须使用 v13?Do I have to use Support-v13?

如果你的应用面向 API 级别13或更高版本,并且你选择使用 Xamarin Android Support-v4 NuGet 包,则 Xamarin Android Support v13 NuGet 包是必需的依赖项。If your app is targeting API Level 13 or higher and you choose to use the Xamarin Android Support-v4 NuGet package, then the Xamarin Android Support v13 NuGet package is a required dependency.

我们认为应用程序大小的变化非常小(两个 .jar 文件在17kb 上存在差异)非常值得,这就是兼容性,并且它带来的麻烦更少。We feel the very minor increase in app size (the two .jar files differ by 17kb) is well worth the compatibility and fewer headaches it results in.

如果你 adamant 在面向 API 级别13或更高版本的应用程序中使用 Support-v4,你始终可以手动下载 .nupkg、提取和引用程序集。If you are adamant about using Support-v4 in an app that targets API Level 13 or higher, you can always manually download the .nupkg, extract it, and reference the assembly.