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 来获得新版本最好的效果。

你可能感兴趣的:(Android-studio)