简体   繁体   English

确定支持库的最低版本

[英]Determining minimum version for support library

When I want to create an Android app that can use the recent supported features of Android, I use libraries like: 当我想创建一个可以使用最新支持的Android功能的Android应用时,我使用如下库:

appcompat_v7
support_v4

What I haven't been able to determine is what is the earliest version of Android that an appcompat_xxx or support library can actually support. 我还无法确定appcompat_xxx或支持库可以实际支持的最早的Android版本是什么。 Where do I find this information? 我在哪里可以找到此信息?

It's all in the name - appcompat_v7 supports API 7 and later, support_v4 supports API 4 and later. 全部都是名称appcompat_v7支持API 7和更高版本, support_v4支持API 4和更高版本。

It's also clearly stated in the docs for each library, for example: 每个库的文档中也明确说明了这些内容,例如:

This library is designed to be used with Android 1.6 (API level 4) and higher. 该库旨在与Android 1.6(API级别4)及更高版本一起使用。

http://developer.android.com/tools/support-library/features.html#v4 http://developer.android.com/tools/support-library/features.html#v4

Also, although this isn't always required, you should try to match up your targetSdkVersion with the respective version of the library. 此外,尽管并非总是需targetSdkVersion ,但您应尝试将targetSdkVersion与库的相应版本匹配。 For example, if your targetSdkVersion is 19, you should use com.android.support:appcompat-v7:19.+ 例如,如果您的targetSdkVersion为19,则应使用com.android.support:appcompat-v7:19.+

To be clear: the support libraries, as dictated by their name, are designed to support a minimum API level. 需要明确的是:支持库(由其名称决定)旨在支持最低API级别。 Meaning - they can be safely used on devices running that API level, as well as devices on any later API level. 含义-它们可以安全地在运行该API级别的设备以及任何更高版本的API级别的设备上使用。 If you attempt to use appcompat_v7 on a donut device (API level 4), or the leanback-v17 library on a Jellybean 4.1 device (API level 16), your app will likely crash with something like a ClassNotFoundException . 如果您尝试在甜甜圈设备(API级别4)上使用appcompat_v7或在Jellybean 4.1设备(API级别16)上使用leanback-v17库,则您的应用程序可能会崩溃,并出现类似ClassNotFoundException

The libraries were originally supposed to provide the functionality of newer API levels to the older API levels, such that you could write nearly the same code and leverage the latest platform paradigms in a single APK that would support old and new devices. 最初,这些库应该提供较新的API级别到较旧的API级别的功能,这样您就可以编写几乎相同的代码,并在支持旧设备和新设备的单个APK中使用最新的平台范例。

Fragments are a perfect example of this. 片段就是一个很好的例子。 The Fragment class was introduced with Honeycomb (API level 11). Fragment类是通过Honeycomb(API级别11)引入的。 Google then released the support_v4 library which contained a back-ported version of the Fragment class and it's respective APIs. Google随后发布了support_v4库,该库包含Fragment类的反向版本及其相应的API。 Again, as dictated by it's name , this library could be safely used in an app that supports all the way back to API 4. If you install an app using the v4 library on a API 3 device, it will crash. 再次, 如其名称所示 ,该库可以安全地用于支持API 4的应用程序中。如果在API 3设备上使用v4库安装应用程序,它将崩溃。 If you install it on, say, an API 8 device, it will work as intended. 如果将其安装在例如API 8设备上,它将按预期运行。

Perhaps you're getting caught up on the seemingly delicate nature of relying on a file name to specify the min sdk version - now this is a bit of a guess, but when the v4 library was first introduced, it was just a .jar file. 也许您已经开始依赖于文件名来指定min sdk版本了,这似乎有些微妙-现在有些猜测,但是当v4库首次引入时,它只是一个.jar文件。 Meaning, you could add it to your app's classpath, no matter what minSdkVersion you use, and it would compile without complaining. 这意味着,无论您使用哪种minSdkVersion ,都可以将其添加到应用程序的类路径中,并且可以编译而不会抱怨。 MY guess is that Google wanted to explicitly name these libraries using their minimum SDK version to avoid developers trying to use them in apps that are meant to support early API versions. 我的猜测是,Google希望使用其最低SDK版本来明确命名这些库,以避免开发人员尝试在旨在支持早期API版本的应用中使用它们。 Aside from their docs, which again very explicitly answer your question, the filename was perhaps was a risk mitigating approach to help developers who don't read the docs. 除了他们的文档(它们再次非常明确地回答了您的问题)之外,文件名也许是一种缓解风险的方法,可以帮助不阅读文档的开发人员。

Fast forward to 2014, many of these libraries either come in the form of .aar files (via Gradle), or you need to import them as projects. 快进到2014年,其中许多库要么以.aar文件的形式(通过Gradle)出现,要么需要将它们作为项目导入。 This is because these libraries now include resources such as images and themes, which cannot be packaged into a .jar file. 这是因为这些库现在包含无法打包到.jar文件中的资源,例如图像和主题。 An added benefit here is that this allows Google to include an AndroidManifest.xml with the library which specifies a minSdkVersion . 这里的一个好处是,这使得谷歌对包括AndroidManifest.xml与指定库中minSdkVersion At compile time, the build tools' manifest merger will complain if your minSdkVersion is lower than the minSdkVersion specified in any included library projects. 在编译时,如果您的minSdkVersion低于任何包含的库项目中指定的minSdkVersion ,则构建工具的清单合并将抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM