在Android的module级build.gradle里,有着为数不少的Version,其中最重要的有以下几个:compileSdkVersion、buildToolsVersion、minSdkVersion、targetSdkVersion、maxSdkVersion。很多人在对这些版本号进行设定的时候,其实自己脑子里也是迷迷糊糊、混混沌沌的,并不明白它们到底代表了什么。也许你还没因为它们掉坑里,但是理解这几个字段的作用其实还是很有必要的。本文大量引用了Android Developer(即Android官网)的资料,有兴趣的朋友也可以自行去官网进行学习。下面我们就来看看这么多的Version到底代表了什么,有什么区别,以及最终该如何选择不同的版本。

compileSdkVersion

compileSdkVersion specifies the Android API level Gradle should use to
compile your app. This means your app can use the API features
included in this API level and lower.

compileSdkVersion指定了Gradle编译你的App时使用的Android API版本,你的App可以使用该版本或者更低版本的API特性。简单来说,如果你用了一些比较新的API特性,那么你的compileSdkVersion就不能低了。通常它应该是Android最新的稳定版本。比如Android Pie(9.0)已经正式发布,那么我们的compileSdkVersion最好直接提升到28(即9.0对应的版本号)。

buildToolsVersion

buildToolsVersion specifies the version of the SDK build tools,
command-line utilities, and compiler that Gradle should use to build
your app. You need to download the build tools using the SDK Manager.

buildToolsVersion制定了Gradle在编译App时使用的SDK build tools、命令行、程序、编译器等的版本,这些都是通过SDK Manager下载的。它的值实际上是一个字符串,在Android Studio里我们通过Tools->SDK Manager,然后点选“SDK Tools”并勾选“Show Package Details”就能看到本地电脑已经下载的SDK Build-Tools的版本。

一般来说,选择跟compileSdkVersion同一个大版本下的最新稳定版本即可,如下图里我已经下载了这么多版本的Build-Tools,在新开发App时我会选择28.0.3版本。

minSdkVersion

An integer designating the minimum API Level required for the
application to run. The Android system will prevent the user from
installing the application if the system’s API Level is lower than the
value specified in this attribute. You should always declare this
attribute.

Caution: If you do not declare this attribute, the system assumes a
default value of “1”, which indicates that your application is
compatible with all versions of Android. If your application is not
compatible with all versions (for instance, it uses APIs introduced in
API Level 3) and you have not declared the proper minSdkVersion, then
when installed on a system with an API Level less than 3, the
application will crash during runtime when attempting to access the
unavailable APIs. For this reason, be certain to declare the
appropriate API Level in theminSdkVersion attribute.
minSdkVersion指定了App运行所需最低的API级别,比如你设置它为23(Android 6.0),那么该App不能在低于Android6.0版本的设备上安装。从理论上来说,如果你设置minSdkVersion为1,则可以最低兼容到最早的Android版本——然而,这就意味着你要为老设备的兼容做大量的工作,显然并不可取,毕竟从统计上来说已经没有人用那么老的设备了。

一般来说,我们通过一些统计结果,以及本公司App的用户分析,来决定最低兼容版本。

根据Google Play的统计,截止到2018年10月26日,使用Android 4.4及以上版本的设备大概占了96.5%,而5.0及以上的则占了接近89%,所以最低版本兼容到4.4就基本上问题不大了。实际上一些老设备的活跃度会比较差,从这个角度考虑,直接从5.0开始做兼容也是可以的。

而用户分析则指的是,App面向的用户的特征。比如用户群体主要是30岁左右的白领,那么这些用户普遍换手机的频率较高,基本上都能追着比较新的Android版本,那么minSdkVersion设置的比较高也问题不大;而如果用户群体面向50甚至60岁以上的中老年人,这些用户对换手机、升级系统不是很感冒,所以有必要把minSdkVersion设置的低一些。

记住,千万不要忘了设置minSdkVersion,如果你不设置,那么就变成了默认值:1,然后你使用的任何在Android 1.0之后才加入的特性,在1.0上运行就崩溃了。

targetSdkVersion

An integer designating the API Level that the application targets. If
not set, the default value equals that given to minSdkVersion.

This attribute informs the system that you have tested against the
target version and the system should not enable any compatibility
behaviors to maintain your app’s forward-compatibility with the target
version. The application is still able to run on older versions (down
to minSdkVersion).

As Android evolves with each new version, some behaviors and even
appearances might change. However, if the API level of the platform is
higher than the version declared by your app’s targetSdkVersion, the
system may enable compatibility behaviors to ensure that your app
continues to work the way you expect. You can disable such
compatibility behaviors by specifying targetSdkVersion to match the
API level of the platform on which it’s running. For example, setting
this value to “11” or higher allows the system to apply a new default
theme (Holo) to your app when running on Android 3.0 or higher and
also disables screen compatibility mode when running on larger screens
(because support for API level 11 implicitly supports larger screens).

There are many compatibility behaviors that the system may enable
based on the value you set for this attribute. Several of these
behaviors are described by the corresponding platform versions in the
Build.VERSION_CODES reference.

To maintain your application along with each Android release, you
should increase the value of this attribute to match the latest API
level, then thoroughly test your application on the corresponding
platform version.

targetSdkVersion完全可以按照它的字面意思去理解:目标sdkVersion。如果没有设置,则默认值为minSdkVersion。当你设置了targetSdkVersion的时候,表示你已经充分测试过了你的App在该目标版本的运行情况,系统不应该启用任何兼容性行为来保持你的App与目标版本的向前兼容性。

如果系统的API级别高于应用的目标版本,则系统会启用兼容性行为来确保应用在更高版本系统上的运行。这一点相信做Android开发时间比较久的人都很理解了,只要你写的程序比较规矩,没有太多的官方推荐外的行为,那么一个老版本的应用放在几年后的Android新设备上依然能顺利运行,只不过相当多的开发者(尤其是国内的)并不是很遵守规范罢了。

再详细说说

targetSdkVersion 是 Android 系统提供前向兼容的主要手段(即:新版本SDK手机兼容旧版本SDK工程)。这是什么意思呢?

举例:在 Android 4.4 (API 19)以后,AlarmManager 的 set()和 setRepeat()这两个 API 的行为发生了变化。

在 Android 4.4 以前,这两个 API 设置的都是精确的时间,系统能保证在 API 设置的时间点上唤醒 Alarm。

在Android 4.4,因为省电原因实现了 AlarmManager 的对齐唤醒,这两个 API 设置唤醒的时间,系统都对待成不精确的时间,系统只能保证在你设置的时间点之后某个时间唤醒。

虽然 API 没有任何变化,但是实际上 API 的行为却发生了变化,如果老的 APK 中使用了此 API,并且在应用中的行为非常依赖 AlarmManager 在精确的时间唤醒,例如闹钟应用。如果 Android 系统不能保证兼容,老的 APK 安装在新的系统上,就会出现问题。

Android 系统是怎么保证这种兼容性的呢?这时候 targetSdkVersion 就起作用了。APK 在调用系统 AlarmManager 的 set()或者 setRepeat()的时候,系统首先会查一下调用的 APK 的 targetSdkVersion 信息,如果小于 19,就还是按照老的行为,即精确设置唤醒时间,如果大于19了,就会崩溃,因为你没配置4.4的新功能

再简单的说

Android 6.0新增加了动态权限申请,我们的targetSdkVersion是22(API 5.0),如果我们运行在23(API 6.0)的设备上怎么办?
因为我们这个可以向前兼容,向后不行啊,如果你的代码里处理了Android 6.0的动态权限处理,那么可以的,如果没呢?那就直接崩溃,纠接着去处理

maxSdkVersion

对于这个属性,我都懒得贴官方的英文文档了,简单来说,它指定了应用所能运行的系统的最高版本。比如你把你的应用的maxSdkVersion设定为26(Android 8.0),则它无法安装在27以及更高版本的系统中。如果原来安装在26的设备里,后来设备系统升级到27或28,则该程序不能运行。总而言之,我实在是想不到任何理由去设定maxSdkVersion,所以你不应该给你的应用设定maxSdkVersion——除非你能找到说服自己的理由,但有什么理由不兼容更高的版本和更新的设备呢?

总结:

maxSdkVersion直接放弃。
minSdkVersion根据不同版本占有率和应用面向用户的群体特征来安排。
targetSdkVersion和compileSdkVersion一般都直接用最新的稳定版本即可。
buildToolsVersion则使用跟compileSdkVersion大版本相同的最新稳定小版本。

Android 面试 - compileSdkVersion、minSdkVersion、targetSdkVersion、buildToolsVersion相关推荐

  1. build.gradle 中compileSdkVersion,minSdkVersion,targetSdkVersion,buildToolsVersion的意思

    compileSdkVersion: 编译版本:compileSdkVersion告诉gradle使用哪个版本AndroidSDK编译你的应用: minSdkVersion: 最低SDK版本:他代表的 ...

  2. compileSdkVersion,minSdkVersion,targetSdkVersion还有buildToolsVersion的区别

    compileSdkVersion指的是你当前android sdk的版本 minSdkVersion指的是应用最低兼容的android sdk版本 targetSdkVersion指的是应用向前兼容 ...

  3. minSdkVersion、compileSdkVersion和targetSdkVersion

    从Eclipse转到AS以后,一直用着最新的各种版本号,对于这三个参数还没有仔细研究过,正好最近对三年前的一个APP进行升级时才发现这里面还是有区别的 1.minSdkVersion:没啥好说的,就是 ...

  4. 如何设置compileSdkVersion, minSdkVersion, and targetSdkVersion

    最近看到了一片国外的资料,google开发工程师贡献的一片如何设置compileSdkVersion, minSdkVersion, and targetSdkVersion,讲的很官方很正解,收藏一 ...

  5. minSdkVersion = targetSdkVersion = compileSdkVersion

    minSdkVersion <= targetSdkVersion <= compileSdkVersion 理想情况 minSdkVersion (lowest possible) &l ...

  6. Android面试必备知识点总结

    本文原文(MarkDown)链接:https://github.com/DmrfCoder/interview/blob/master/Android/Android.md 文章目录 Android的 ...

  7. Android面试复习资料整理

    Activity巩固和复习 1. 什么是Activity 四大组件之一,通常一个用户交互界面对应一个activity.activity是Context的子类,同时实现了window.callback和 ...

  8. Android面试问题汇总

    GitHub持续更新:(声明:本答案为个人收集与总结并非标准答案,仅供参考,如有错误还望指出,谢谢!如有重复可能是常问问题) ArrayList的使用,ArrayList使用过程中有没有遇到过坑. 参 ...

  9. Android面试宝典2022-(停止更新,请看面试专栏)

    Android面试宝典2020-持续更新 一.Java基础 1.java基本数据类型和引用类型 2.object equals和==的区别 equals和hashcode的关系? 3.static关键 ...

最新文章

  1. 面对新型肺炎疫情,AI能做什么?
  2. 找回MySQL的root密码
  3. 在Ubuntu Server上添加NFS共享文件夹
  4. 神策数据荣获 2017 年度商业影响力大数据领域新锐企业 TOP 10
  5. asp.net中将枚举绑定到下拉列表
  6. sort list java leetcode_[LeetCode] 148. Sort List Java
  7. CentOS安装jdk和tomcat
  8. 备份恢复与同步(数据搬运专家)
  9. excel进销存管理系统_【实例分享】勤哲Excel服务器做企业进销存财务管理系统...
  10. 用jquery选取表行
  11. Android Lint 实践 —— 简介及常见问题分析
  12. xp系统更新的服务器失败是怎么回事啊,xp系统显示“服务器错误500”的两种解决方法...
  13. JavaWeb之Servlet:Cookie 和 Session
  14. 空位補零,你會選擇哪種方式?
  15. iOS UI08_TableView界面传值
  16. Spring Cloud 微服务开发:入门、进阶与源码剖析 —— 9.4 Spring Cloud Gateway 路由断言工厂
  17. Python之禅及其翻译
  18. 网站备案必须要云服务器,备案必须要云服务器吗
  19. 测试中常用的正则表达式你知道哪些?
  20. 【Python爬虫系列教程 28-100】小姐姐带你入门爬虫框架Scrapy、 使用Scrapy框架爬取糗事百科段子

热门文章

  1. MySQL8小时连接超时断开问题
  2. MATLAB中多个一维数组的合并
  3. OpenGL 各类库的解析 gl/glu/glut/freeglut/glfw/glew
  4. Mac系统Eclipse配置Maven
  5. java插入flash_怎样可以把flash添加到Java应用程序
  6. pandas 将某一列转换为字符类型_6个冷门但实用的pandas知识点
  7. stm32 DMA 配置 串口程序
  8. redis cluster集群模式总结
  9. 那些人工智能未来式,没看过你就 OUT 了
  10. Android中activity传值的两种方式