最近看到了一片国外的资料,google开发工程师贡献的一片如何设置compileSdkVersion, minSdkVersion, and targetSdkVersion,讲的很官方很正解,收藏一下,结尾给出了本篇的结论,大家可以看一下,顺便练一下英文。

Dependingon the time of the year, it might only be a few months after you release an appthat a new version of Android is announced. What does that mean for your appthough — is everything going tobreak?

You’llbe happy to know that forward compatibility is a strongfocus of Android — existing apps built against prior SDKs should not break when the userupdates to a new version of Android. This is where compileSdkVersionminSdkVersion,and targetSdkVersion come in: they control what APIs areavailable, what the required API level is, and what compatiblity modes areapplied, respectively.

compileSdkVersion

compileSdkVersion isyour way to tell Gradle what version of the Android SDK to compile your appwith. Using the new Android SDK is a requirement to use any of the new APIsadded in that level.

Itshould be emphasized that changing your compileSdkVersion does not changeruntime behavior. While new compiler warnings/errors may bepresent when changing your compileSdkVersion, your compileSdkVersion is notincluded in your APK: it is purely used at compile time. (You should really fixthose warnings though — they were added for a reason!)

Thereforeit is strongly recommended that you always compile with the latest SDK.You’ll get all the benefits of new compilation checks on existing code, avoidnewly deprecated APIs, and be ready to use new APIs.

Notethat if you use the Support Library,compiling with the latest SDK is a requirement for using the latestSupport Library releases. For example, to use the 23.1.1 Support Library, youmust have a compileSdkVersion of at least 23 (those first numbers need tomatch!). In general, a new version of the Support Library is released alongsidea new platform version, providing compatibility shims to newly added APIs aswell as new features.

minSdkVersion

IfcompileSdkVersion sets the newest APIs available to you, minSdkVersion isthe lower bound for your app. The minSdkVersion is one of thesignals the Google Play Store uses to determine which of a user’s devices anapp can be installed on.

Italso plays an important role during development: by default lint runs againstyour project, warning you when you use any APIs above your minSdkVersion,helping you avoid the runtime issue of attempting to call an API that doesn’texist. Checking the system version at runtime is a common technique when using APIs only on newer platformversions.

Keepin mind that libraries you use, such as any of the Support Libraries or Google Play services,may have their own minSdkVersion — your app’s minSdkVersion must be at least as high as your dependencies’ minSdkVersion — if you have libraries thatrequire 4, 7, and 9, your minSdkVersion must be at least 9. In rare cases whereyou want to continue to use a library with a higher minSdkVersion than your app(and deal with all edge cases/ensure the library is only used on newer platformversions), you can use the tools:overrideLibrary marker, but make sure to test thoroughly!

Whendeciding on a minSdkVersion, you should consider the stats on theDashboards,which give you a global look on all devices that visited the Google Play Storein the prior 7 days — that’syour potential audience when putting an app on Google Play. It is ultimately abusiness decision on whether supporting an additional 3% of devices is worththe development and testing time required to ensure the best experience.

Ofcourse, if a new API is key to your entire app, then that makes theminSdkVersion discussion quite a bit easier. Just remember that even 0.7% of1.4 billion devices is a lot of devices.

targetSdkVersion

Themost interesting of the three, however, is targetSdkVersion. targetSdkVersionis the main way Android provides forward compatibility bynot applying behavior changes unless the targetSdkVersion is updated. Thisallows you to use new APIs (as you did update your compileSdkVersion right?)prior to working through the behavior changes.

Muchof the behavior changes that targetSdkVersion implies are documented directlyin the VERSION_CODES,but all of the gory details are also listed on the each releases’ platformhighlights, nicely linked in the API Levels table.

Forexample, the Android 6.0 changes talkthrough how targeting API 23 transitions your app to the runtime permissions model and the Android 4.4 behavior changes detail how targeting API 19 or higher changes how alarms setwith set() and setRepeating() work.

Withsome of the behavior changes being very visible to users (the deprecation of the menu button, runtime permissions, etc), updating to target the latest SDKshould be a high priority for every app. That doesn’t meanyou have to use every new feature introduced nor should you blindly update yourtargetSdkVersion without testing — please, please test before updating your targetSdkVersion! Your users will thank you.

Gradle and SDK versions

Sosetting the correct compileSdkVersion, minSdkVersion, and targetSdkVersion isimportant. As you might imagine in a world with Gradleand Android Studio,these values are integrated into the tools system through inclusion in yourmodule’s build.gradle file (also available through the Project Structure optionin Android Studio):

android {compileSdkVersion 23buildToolsVersion “23.0.1”defaultConfig {applicationId “com.example.checkyourtargetsdk"minSdkVersion 7
    targetSdkVersion 23versionCode 1versionName “1.0”}
}

ThecompileSdkVersion, being a compile time thing (who would have guessed!), is oneof the android settings alongside with your build tools version. The other twoare slightly differently in that they are declared at the build variant level — the defaultConfig isthe base for all build variants and where’d you put default values for these,but you could imagine a more complicated system where specific versions of yourapp have a different minSdkVersion for example.

minSdkVersionand targetSdkVersion also differ from compileSdkVersion in that they areincluded in your final APK — if you were to look at the generated AndroidManifest.xml,you’d see a tag such as:

<uses-sdk android:targetSdkVersion=”23" android:minSdkVersion=”7" />

You’llfind if you manually put this in your manifest, it’ll be ignored when you buildwith Gradle (although other build systems might certainly rely on it beingthere).

Putting it all together

Ifyou made it through the bolded notes, you’ll notice arelationship between the three values:

minSdkVersion <= targetSdkVersion <= compileSdkVersion

Thisintuitively makes sense — if compileSdkVersion is your ‘maximum’ and minSdkVersion is your ‘minimum’ then your maximum must be at least as high as your minimum and thetarget must be somewhere in between.

Ideally,the relationship would look more like this in the steady state:

minSdkVersion (lowest possible) <= targetSdkVersion == compileSdkVersion (latest SDK)

You’llhit the biggest audience with a low minSdkVersion and look and act the best bytargeting and compiling with the latest SDK — a great way to#BuildBetterApps.

如何设置compileSdkVersion, minSdkVersion, and targetSdkVersion相关推荐

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

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

  2. Android 面试 - compileSdkVersion、minSdkVersion、targetSdkVersion、buildToolsVersion

    在Android的module级build.gradle里,有着为数不少的Version,其中最重要的有以下几个:compileSdkVersion.buildToolsVersion.minSdkV ...

  3. Android compileSdkVersion、minSdkVersion、targetSdkVersion的区别

      Android手机都有一个运行的系统版本.系统和应用程序类似,系统版本和打包系统的targeSdkVersion版本一致,App在Android手机上可以调用所对应SDK中的Api. 一.comp ...

  4. 如何设置minSdkVersion和targetSdkVersion

    转http://www.07net01.com/2015/07/878098.html minSdkversion和targetSdkVersion相信很多人都不太理解,我在网上也看了许多关于这两者区 ...

  5. compileSdkVersion、minSdkVersion、targetSdkVersion 三者的含义和区别

    个人总结: 一.含义:compileSdkVersion: 顾名思义 Android SDK的编译版本(代码中所用的API不能超过这个版本)minSdkVersion: 此软件在Android系统中可 ...

  6. Android中minSdkVersion、targetSdkVersion、maxSdkVersion的作用

    转载自:Android中minSdkVersion.targetSdkVersion.maxSdkVersion的作用 - 谁与争锋的专栏 - 博客频道 - CSDN.NET  http://blog ...

  7. Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分

    Android中build target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分 本文参考了谷歌开发者文档:http://developer. ...

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

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

  9. compileSdkVersion、buildToolsVersion、minSdkVersion、targetSdkVersion

    compileSdkVersion:代码使用到的SDK的版本,推荐使用最新的 有时候需要降低compileSdkVersion版本,来支持旧版本SDK中的类(尽量不要这么做),但直接改这个值可能会报错 ...

最新文章

  1. %fplot('Untitled1',[-1,2])画图
  2. LeetCode Convert Sorted List to Binary Search Tree(有序单链表转为平衡二叉树)
  3. UA OPTI570 量子力学2 物质波与物质粒子
  4. [Kafka与Spark集成系列四] Spark运行结构
  5. Java ClassLoader findSystemClass()方法与示例
  6. LeetCode 333. 最大 BST 子树(递归)*
  7. 网络运维超融合服务器,15. 通过 Openfiler 软件:配置 ISCSI 和
  8. iOS 腾讯云IM UIKit 升级XCode11后, 调用语音(取消) 崩溃问题
  9. Want To Say Something
  10. 中孚计算机保密玩游戏,中孚计算机保密检查
  11. 奥维互动地图自定义地图
  12. 2014年FME校园培训考核通过名单
  13. 中风后下肢麻木瘀阻案
  14. 魔兽RPG仿魔兽世界:基尔加丹的末日V1.0
  15. iOS 调整图片尺寸和大小的两个方法
  16. 数据中心机房光纤综合布线
  17. 利用正则表达式做验证登录
  18. 华为双前置摄像头_华为nova7 Pro的前置是两个摄像头吗?自拍功能咋样
  19. 【微信小程序】小程序的生命周期
  20. 如愿斩获阿里巴巴定级P7的offer,不知四面其艰难,为此筹备半年

热门文章

  1. web站点服务器日志管理及分析
  2. 修改tomcat编码格式
  3. eps高程点和高程注记不符_利用AutoCAD二次开发实现地形图高程点与等高线错误自动查找...
  4. 研究一下列车时刻表的后台表结构和常用的查询SQL
  5. java控制台中文乱码_java用控制台输出时出现乱码怎么办
  6. 使用yolov5训练自动驾驶目标检测数据集BDD100K
  7. linux 软件工具,Ubuntu 下常用的软件工具
  8. [手机分享]黑莓手机7系列分享之——7100T
  9. iPhone5分辨率兼容(iPhone3,4,4S)
  10. linux php curl 升级,Linux下为php添加curl扩展 | 严佳冬