Android Support Library的前世今生
<p></p><div><br></div><div><div class="image-package imagebubble" widget="ImageBubble" style="margin-right: auto; margin-bottom: 20px; margin-left: auto; text-align: center; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://upload-images.jianshu.io/upload_images/1621638-64e0312fb3d79864.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" data-original-src="http://upload-images.jianshu.io/upload_images/1621638-64e0312fb3d79864.png?imageMogr2/auto-orient/strip%7CimageView2/2" class="imagebubble-image" style="max-width: 100%; height: auto; vertical-align: middle; border: 0px; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out; opacity: 1;"><br><div class="image-caption" style="min-width: 20%; min-height: 22px; display: inline-block; padding: 10px; margin-right: auto; margin-left: auto; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(217, 217, 217); font-size: 13px; color: rgb(153, 153, 153); font-style: italic; line-height: 1.7;">封面</div></div><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">在之前的开发经历中经常需要导入一些如v4、v7、v13等Android官方的支持包,遇到这些情况时都是网上搜索一下,按照前人给的示例添加依赖。这么稀里糊涂的使用后代码正常运行了,但不免会对Android官方提供的支持包产生了一些疑问。如,Android官方为什么要提供支持包,都提供哪些支持包,这些支持包又提供了什么特性,开发者又应该如何选择使用这些支持包?</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><em>(本文出处:<a href="http://www.jianshu.com/p/f5f9a4fd22e8" target="_blank" style="color: rgb(64, 148, 199); text-decoration: none;">http://www.jianshu.com/p/f5f9a4fd22e8</a>)</em></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">为了解开这些疑问,周末在家仔细阅读了官方的<a href="https://developer.android.com/topic/libraries/support-library/index.html#overview" target="_blank" style="color: rgb(64, 148, 199); text-decoration: none;">开发者指导文档</a>的相关内容。这篇文章就是读后整理的读书笔记。</p><h4 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 20px;">Android官方为什么要提供支持包?</h4><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">为什么官方向开发者在提供了android sdk之外,还要提供一些零碎的开发支持jar包,全部放在framework中不好吗?恩,不好!因为这不是好不好的问题,这是Android平台快速发展带来的必然产物,这张图罗列了已经发布的Android版本及其对应的开发sdk的级别。</p><div class="image-package imagebubble" widget="ImageBubble" style="margin-right: auto; margin-bottom: 20px; margin-left: auto; text-align: center; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://upload-images.jianshu.io/upload_images/1621638-ab90ba556333d9a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" data-original-src="http://upload-images.jianshu.io/upload_images/1621638-ab90ba556333d9a6.png?imageMogr2/auto-orient/strip%7CimageView2/2" class="imagebubble-image" style="max-width: 100%; height: auto; vertical-align: middle; border: 0px; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;"><br><div class="image-caption" style="min-width: 20%; min-height: 22px; display: inline-block; padding: 10px; margin-right: auto; margin-left: auto; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(217, 217, 217); font-size: 13px; color: rgb(153, 153, 153); font-style: italic; line-height: 1.7;">Android版本</div></div><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">至于为什么提供支持包官方给出了大致三个原因:</p><ol style="margin-bottom: 20px; margin-left: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">向后兼容<br>如,我们开的App需要支持的minSdkVersion=9,targetSdkVersion=11,在程序里使用了android 3.0 (API level 11)提供的ActionBar类,使用compileSdkVersion=11成功编译出apk。在android 3.0的设备上完美运行,但是app在android 2.3的设备就会crash,报找不到ActionBar的错误。这很好理解,因为旧版本上没有新版本里新增的类。为了避免使用了最新功能开发的app只能在最新系统的设备上运行的尴尬,官方把新版系统framework中新增加的接口提出来放到了Android Support Library(支持包)中,开发者在遇到上面的情况时,就可以使用支持包中具有样功能的ActionBar类,这个支持包会打包进App里,这样使用了新版本系统上功能的App也可以向后兼容以前的老系统版本设备了。<br>使用支持包中的类除了让我们免于判断App运行的系统版本外,还可以使App在各个版本保持同样的用户体验。如在5.0以下系统使用material design。</p><blockquote style="padding: 10px 15px; margin-bottom: 20px; border-left-width: 4px; border-left-style: solid; border-left-color: rgb(153, 153, 153); word-break: break-word; font-size: 15px; background-color: whitesmoke;"><p style="line-height: 1.7; word-break: break-word; overflow: visible;">App编译时用的android sdk(android.jar)不会打包进我们的App中。因为App编码是使用android.jar中的接口就是android设备里系统框架层(framework)对外提供的接口。</p></blockquote></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">提供不适合打包进framework的功能<br>Android官方对App开发提供了推荐设计,希望Android应用都有相对一致的交互设计来减少用户的使用成本,希望三方App类似系统应用从而完美融入到系统中。但是这都仅仅是推荐,不要求开发者一定要这样,如果有这种需求就可以使用官方支持包提供的这些功能,避免重复造轮子。如支持包中的DrawerLayout、Snackbar等类都是这种情况。</p></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">为了支持不同形态的设备<br>通过使用支持包来在不同形态设备上提供功能,如手机、电视、可穿戴设备等。</p></li></ol><h4 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 20px;">官方提供了哪些支持包,都有哪些特性?</h4><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">现在Android官方发布了下面13类支持库,不同的支持库包含不同特征,适用的Android版本也不相同。通常情况下我们都使用到v4和v7这两个集合库,因为这两个库支持的android系统版本范围比较广,官方推荐的UI设计模式相关类也都在这两集合库中。</p><div class="image-package imagebubble" widget="ImageBubble" style="margin-right: auto; margin-bottom: 20px; margin-left: auto; text-align: center; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://upload-images.jianshu.io/upload_images/1621638-1f66aafb225df824.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" data-original-src="http://upload-images.jianshu.io/upload_images/1621638-1f66aafb225df824.png?imageMogr2/auto-orient/strip%7CimageView2/2" class="imagebubble-image" style="max-width: 100%; height: auto; vertical-align: middle; border: 0px; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;"><br><div class="image-caption" style="min-width: 20%; min-height: 22px; display: inline-block; padding: 10px; margin-right: auto; margin-left: auto; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(217, 217, 217); font-size: 13px; color: rgb(153, 153, 153); font-style: italic; line-height: 1.7;">Android Support Library</div></div><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">【※】v4 Support Libraries</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">这些库被设计来使用在Android 2.3 (API level 9)及其以上版本中。 Support Library的第1版(2011年3月发布)就只包含v4库,当时v4库只是一个库,支持Android 1.6 (API level 4)及其以上版本,这也是v4名字的由来。随着系统的迭代现在Android 1.6设备已经很少了,官方在Support Library的第24.2.0版本(2016年8月发布)的时候移除了对Android 2.2 (API level 8)及其以下版本的支持,但是名字依然是v4。v4悠久的历史长期的发展造就了它较大的体积。也是在24.2.0这个版本Goggle将原来的单个v4库拆分成了5个子库,我们在使用的时候可以直接依赖某个子库,从而减少依赖包的大小。下图可见2.2 Froyo占有率约为0.1%。</p><div class="image-package imagebubble" widget="ImageBubble" style="margin-right: auto; margin-bottom: 20px; margin-left: auto; text-align: center; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://upload-images.jianshu.io/upload_images/1621638-8f78827ae6276c1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" data-original-src="http://upload-images.jianshu.io/upload_images/1621638-8f78827ae6276c1a.png?imageMogr2/auto-orient/strip%7CimageView2/2" class="imagebubble-image" style="max-width: 100%; height: auto; vertical-align: middle; border: 0px; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;"><br><div class="image-caption" style="min-width: 20%; min-height: 22px; display: inline-block; padding: 10px; margin-right: auto; margin-left: auto; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(217, 217, 217); font-size: 13px; color: rgb(153, 153, 153); font-style: italic; line-height: 1.7;">2016年9月5日各版本市场占有率</div></div><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">Gradle编译脚本中整个v4库的依赖语句如下:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-v4:24.2.1'</span></code></pre><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">gradle中jar依赖语句格式如 <code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">compile 'jar文件组(group/命名空间):jar文件名(name):jar文件版本(version)'</code>。所以上面的语句意思是依赖Android支持库中第24.2.1版的support-v4库。由于在24.2.0版本support-v4库已经被拆分成5个子库,所以如下图所示依赖24.2.1版本的support-v4库除了导入support-v4库外还会导入它的5个子库,这个版本的support-v4库本身是一个空的包,所有具体的实现都在它依赖的子库中。下面依次看下v4库拆分的5个子库。</p><div class="image-package imagebubble" widget="ImageBubble" style="margin-right: auto; margin-bottom: 20px; margin-left: auto; text-align: center; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://upload-images.jianshu.io/upload_images/1621638-e50b38383771e40c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" data-original-src="http://upload-images.jianshu.io/upload_images/1621638-e50b38383771e40c.png?imageMogr2/auto-orient/strip%7CimageView2/2" class="imagebubble-image" style="max-width: 100%; height: auto; vertical-align: middle; border: 0px; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;"><br><div class="image-caption" style="min-width: 20%; min-height: 22px; display: inline-block; padding: 10px; margin-right: auto; margin-left: auto; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(217, 217, 217); font-size: 13px; color: rgb(153, 153, 153); font-style: italic; line-height: 1.7; opacity: 1;">support-v4</div></div><ul style="margin-bottom: 20px; margin-left: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><li style="line-height: 30px;">v4 compat library<br>为一些框架的API提供兼容性包装。如,<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">Context.obtainDrawable()</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">View.performAccessibilityAction()</code>等。<br>Gradle编译脚本中v4 compat库的依赖语句:<pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-compat:24.2.1'</span></code></pre></li><li style="line-height: 30px;">v4 core-utils library<br>提供了一些工具类。如,<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">AsyncTaskLoader</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">PermissionChecker</code>等。<br>Gradle编译脚本中v4 core-utils库的依赖语句:<pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-core-utils:24.2.1'</span></code></pre></li><li style="line-height: 30px;">v4 core-ui library<br>提供很多UI相关组件。如,<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">ViewPager</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">NestedScrollView</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">ExploreByTouchHelper</code>等。<br>Gradle编译脚本中v4 core-ui库的依赖语句:<pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-core-ui:24.2.1'</span></code></pre></li><li style="line-height: 30px;">v4 media-compat library<br>多媒体框架相关部分。如,<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">MediaBrowser</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">MediaSession</code>等。<br>Gradle编译脚本中v4 media-compat库的依赖语句:<pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-media-compat:24.2.1'</span></code></pre></li><li style="line-height: 30px;">v4 fragment library<br>跟fragment相关部分。v4这个子库依赖了其他4个子库,所以我们一旦依赖这个库就会自动导入其他4个子库,这跟直接依赖整个support-v4效果类似。关于v4拆分这篇<a href="http://www.trinea.cn/android/android-%E6%9C%80%E6%96%B0-support-v4-%E5%8C%85%E5%A4%A7%E6%8B%86%E5%88%86%E6%9C%89%E7%94%A8%E5%90%97%EF%BC%9F/" target="_blank" style="color: rgb(64, 148, 199); text-decoration: none;">文章</a>有介绍,有兴趣的可以点过去看看。<br>Gradle编译脚本中v4 fragment 库的依赖语句如下:<pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-fragment:24.2.1'</span></code></pre></li></ul><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">【※】v7 Support Libraries</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">注意这里的Library用的也是复数,说明v7库和v4一样也是很多库的集合,不同的是v7各个库不是后来拆分出来的,而是最初发布时就是以各个独立的库的形式发布的,如最早发布的v7库v7 gridlayout library。它们的共同之处是发布之初都是支持Android 2.1 (API level 7)及其以上版本,所以把他们统称为v7支持库。需要注意的24.2.0版本以后的v7支持库支持范围也是Android 2.3 (API level 9)及其以上版本了,这是因为v7依赖的v4库支持版本范围改变了,这点在v4支持库小节有介绍。v7库集合里有7个子库,使用时根据需要选择导入哪些库。</p><ul style="margin-bottom: 20px; margin-left: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 appcompat library<br>支持UI设计样式、 material design相关,如<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">ActionBar</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">AppCompatActivity</code>、Theme等。<br>Gradle编译脚本中v7 appcompat库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:appcompat-v7:24.2.1'</span></code></pre></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 cardview library<br>支持cardview控件,使用material design语言设计,卡片式的来展示信息,在电视App中有广泛的使用。<br>Gradle编译脚本中v7 cardview库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:cardview-v7:24.2.1'</span></code></pre></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 gridlayout library<br>支持gridlayout布局。<br>Gradle编译脚本中v7 gridlayout库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:gridlayout-v7:24.2.1'</span></code></pre></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 mediarouter library<br>该库提供了 <code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">MediaRouter</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">MediaRouteProvider</code>等与Google Cast相关的类。<br>Gradle编译脚本中v7 mediarouter库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:mediarouter-v7:24.2.1'</span></code></pre></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 palette library<br>该库提供了palette类,使用这个类可以很方便提取出图片中主题色。比如在音乐App中,从音乐专辑封面图片中提取出专辑封面图片的主题色,然后将播放界面的背景色设置为封面的主题色,随着播放音乐的改变,播放界面的背景色也会巧妙的跟着改变,从而提供更好的用户体验。<br>Gradle编译脚本中v7 palette库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:palette-v7:24.2.1'</span></code></pre></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 recyclerview library<br>该库提供了recyclerview类。这个库使用的频率很高,网上有很多文章介绍recyclerview。<br>Gradle编译脚本中v7 recyclerview库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:recyclerview-v7:24.2.1'</span></code></pre></li><li style="line-height: 30px;"><p style="margin-top: 16px; margin-bottom: 16px; word-break: break-word; overflow: visible;">v7 Preference Support library<br>这个库在设置界面常用到。提供了 <code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">CheckBoxPreference</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">ListPreference</code>等类。<br>Gradle编译脚本中v7 preference support库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:preference-v7:24.2.1'</span></code></pre></li></ul><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">v8 Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">v8支持库支持范围也是Android 2.3 (API level 9)及其以上版本。v8支持库集合中现在只有一个库。</p><ul style="margin-bottom: 20px; margin-left: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;"><li style="line-height: 30px;">v8 renderscript library<br>这个库支持渲染脚本计算框架。对这个库有兴趣可以看<a href="https://developer.android.com/guide/topics/renderscript/compute.html" target="_blank" style="color: rgb(64, 148, 199); text-decoration: none;">渲染脚本开发指导</a>。<br>使用v8 renderscript库Gradle编译脚本的配置方法:<pre class="hljs cpp" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="cpp" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">defaultConfig {renderscriptTargetApi <span class="hljs-number" style="color: rgb(42, 161, 152);">18</span>
renderscriptSupportModeEnabled <span class="hljs-literal">true</span>
}</code></pre></li></ul><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">【※】v13 Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">v13支持库支持范围也是Android 3.2 (API level 13)及其以上版本。这个库跟v4 fragment library功能基本一样,也是提供兼容fragment相关内容。区别是v4 fragment library需要依赖v4支持库集合里的其它4个子库,而v13 support library依赖的是Android 3.2 (API level 13)及其以上版本framework。也就是说v4支持库除了v4 fragment library以外,其它功能都在Android 3.2 (API level 13)及其以上版本framework中提供了。所以我们的App如果只需要兼容到Android 3.2,那么fragment部分使用v13 Support Library中的相关类才是明智之举。<br>Gradle编译脚本中v13 support库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-v13:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">v14 Preference Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">功能类似<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">v7 Preference Support library</code>,支持Android系统版本不一致,新增部分相关接口。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:preference-v14:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">v17 Preference Support Library for TV</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">功能类似<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">v7 Preference Support library</code>,支持Android系统版本不一致,新增部分相关接口,为电视设备App提供相应的UI。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:preference-leanback-v17:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">v17 Leanback Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">这也是在电视设备上使用的库,主要是和YouTube相关的。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:leanback-v17:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">Annotations Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">提供注解相关功能。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:support-annotations:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">【※】Design Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">这个库现在使用的也比较多,它提供了material design设计风格的控件。如,navigation drawers、floating action buttons (<em>FAB</em>)、snackbars、tabs等。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:design:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">【※】Multidex Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">Android的单个.dex文件最多能引用65536个方法,在这之后的方法就无法引用了。当我们的方法数超过这个限制后就需要分成多个dex文件,该库就是用来支持多个dex文件构建应用程序的。<br>Gradle编译脚本中v4 multidex库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:multidex:1.0.0'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">【※】Custom Tabs Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">这个库有很有意思,提供了一种新的打开网页的方式。以前的App要打开一个网页有两种选择,一个是用webview,这种方式工作量较大,第二种方式是调用浏览器应用来打开网页,这种方式要在两个应用中切换,用户的操作体验是割裂的,都不够理想。这个库提供了第三种选择,具体情况可以点击这篇<a href="http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0914/3449.html" target="_blank" style="color: rgb(64, 148, 199); text-decoration: none;">文章</a>了解。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:customtabs:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">Percent Support Library</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">百分比支持库提供了如<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">PercentFrameLayout</code>、<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">PercentRelativeLayout</code>布局,在这些布局中子view可以使用百分比来设置大小、位置等。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:percent:24.2.1'</span></code></pre><h5 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 18px;">App Recommendation Support Library for TV</h5><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">这个库是电视设备上用来提供视频内容推荐的。<br>Gradle编译脚本中v 库的依赖语句:</p><pre class="hljs bash" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="bash" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-string" style="color: rgb(42, 161, 152);">'com.android.support:recommendation:24.2.1'</span></code></pre><h4 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 20px;">如何选择使用支持包?</h4><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">其实在了解了支持包特性之后,这个问题也就迎刃而解了,这里再做下总结。在使用Android Support Library之前我们需要通过sdk manager安装Android Support Repository,然后再在gradle编译脚本中添加如下依赖语句就可以了。</p><pre class="hljs scala" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="scala" style="font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;">compile <span class="hljs-symbol" style="color: rgb(203, 75, 22);">'com</span>.android.support:support-v4:<span class="hljs-number" style="color: rgb(42, 161, 152);">24.2</span><span class="hljs-number" style="color: rgb(42, 161, 152);">.1</span>'<span class="hljs-comment" style="color: rgb(147, 161, 161);">//以v4为例</span></code></pre><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">前面文章说过gradle中jar依赖语句格式如 <code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">compile jar文件组(group/命名空间):jar文件名(name):jar文件版本(version)</code>。对于Android Support Library库的依赖语句<strong>jar文件名</strong>和<strong>jar文件版本</strong>两部分需要选择确定。</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">jar文件名:在选择之前要明确两件事:<strong>需要使用支持包的哪种特性</strong>、<strong>需要兼容的最低Android版本</strong>,然后就可以确定具体依赖哪个支持库。<br>jar文件版本:支持库的版本需要跟<code style="padding: 2px 4px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; color: rgb(101, 123, 131); border-radius: 3px; border: none; white-space: pre-wrap; background-color: rgb(253, 246, 227);">compileSdkVersion</code>保持一致。</p><blockquote style="padding: 10px 15px; margin-bottom: 20px; border-left-width: 4px; border-left-style: solid; border-left-color: rgb(153, 153, 153); word-break: break-word; font-size: 15px; line-height: 30px; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; background-color: whitesmoke;"><p style="line-height: 1.7; text-align: justify; word-break: break-word;">注意:由于依赖的支持库会打包进apk,所以官方推荐开发者在编译时使用ProGuard工具预处理release版本的apk。ProGuard工具除了混淆源代码外,还会移除那些依赖的支持库中没有使用到的类,达到apk瘦身的效果。</p></blockquote><h4 style="font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; line-height: 1.8; color: rgb(47, 47, 47); text-rendering: optimizeLegibility; font-size: 20px;">结束</h4><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">以上就是关于Android Support Library全部了,谢谢。如果文章有错误或者有疑问请务必留言告诉我。</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; color: rgb(47, 47, 47); font-family: -apple-system, 'Helvetica Neue', Arial, 'PingFang SC', 'lucida grande', 'lucida sans unicode', lucida, helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif; font-size: 16px; line-height: 27.2px;">原文地址:</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word;"><font color="#2f2f2f" face="-apple-system, Helvetica Neue, Arial, PingFang SC, lucida grande, lucida sans unicode, lucida, helvetica, Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif"><span style="font-size: 16px; line-height: 27.2px;">http://www.jianshu.com/p/f5f9a4fd22e8</span></font></p></div>
页:
[1]