Gradle中不同sdkVersion代表意义

Gradle中不同sdkVersion代表意义

Android Studio中,build.gardle中有不同的sdkversion,记录不同的sdk意义。

  • compileSdkVersion——编译sdk版本
  • targetSdkVersion——开发sdk版本
  • minSdkVersion——程序兼容的最小sdk版本

compileSdkVersion

compileSdkVersion是程序编译版本api,与生成apk程序没任何影响,只是参与程序代码编译,修改compileSdkVersion只是会提示编译错误,警告提示等。

minSdkVersion

minSdkVersion是app运行所需的最低sdk版本,低于minSdkVersion的手机将无法安装app。

targetSdkVersion

An integer designating the API Level that the application targets. If not set, the defaultvalue equals that given to minSdkVersion.
This attribute informs the system that you have tested against the target version and thesystem should not enable any compatibility behaviors to maintain your app’s forward-compatibilitywith 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 appcontinues to work the way you expect. You can disable such compatibilitybehaviors by specifying targetSdkVersion to match the APIlevel of the platform on which it’s running. For example, setting this value to “11” or higherallows the system to apply a new default theme (Holo) to your app when running on Android 3.0 orhigher and also disables screencompatibility mode when running on larger screens (because support for API level 11 implicitlysupports larger screens).
There are many compatibility behaviors that the system may enable based on the value you setfor this attribute. Several of these behaviors are described by the corresponding platform versionsin the Build.VERSION_CODES reference.
To maintain your application along with each Android release, you should increasethe value of this attribute to match the latest API level, then thoroughly test your application onthe corresponding platform version.

targetSdkVersion原文是这么解释的:targetSdkVersion is the main way Android provides forward compatibility。targetSdkVersion是 Android 提供向前兼容的主要方式。在targetSdkVersion 之前的系统不会应用最新的行为。

形象的说是:Android手机版本较多,不同版本的api方法名称一样,但是实际内部方法不一致,为了保证在不同的版本上调用的是相同版本的api方法,设置targetSdkVersion,当前手机的sdk大于targetSdkVersion ,都会默认使用该targetSdkVersion的api方法,这样就保证了新系统对老应用的向前兼容性。这就是API 19开发的Android项目可以在Android 7.0系统的手机上运行,同样Android 4.x系统的手机可以运行API 24开发的Android项目。

targetSdkVersion的选择

目前,Android版本较多,如果使用低版本的api,在高版本的aip上也兼容,但是有些功能不完善,高版本的api提供更多的或封装好的接口让开发者使用,但需要慎重兼容性问题。因此对于选择targetSdkVersion要根据实际应用出发。目前大多程序设置的23-25之间,更多详情请查看源码介绍。

三者关系

Android系统平台变更时,只有targetSdkVersion的属性值被设置为大于等于该系统平台的API版本时,才会生效;compileSdkVersion属性不会被打包的APK文件中。而targetSdkVersion和minSdkVersion将被打包到APK文件中。

三者的关系应该更像这样:
minSdkVersion <= targetSdkVersion <= compileSdkVersion
用 minSdkVersion 来兼容大部分Android版本,用targetSdkVersion 和 compileSdkVersion 来获得新版本最好的效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值