[英]Should compile sdk be lower than target sdk
The overwhelming advice on SO is that compile SDK should usually match target SDK. 关于SO的压倒性建议是编译SDK通常应该与目标SDK匹配。
Likewise it is advisable to have this [compileSdk] match you target sdk version.
同样,建议将此[compileSdk]与目标sdk版本匹配。
... CompileSDK (usually equal to target SDk version) version.
... CompileSDK(通常等于目标SDk版本)版本。
But the clear advice here is: 但这里明确的建议是:
In general, you should compile your application against the lowest possible version of the platform that your application can support.
通常,您应该针对应用程序可以支持的最低版本的平台编译应用程序。
I've always made compile sdk match target sdk, but since reading that I realize that I, and many SO answers are wrong. 我总是编译sdk匹配目标sdk,但自从阅读后我意识到我,以及许多SO答案都是错误的。
My question is, what's the harm in making them match or conversely what's the advantage of having the lowest compile sdk version you can get away with? 我的问题是,使它们匹配有什么害处,或者反过来说,拥有最低编译sdk版本的优势是什么?
The overwhelming advice on SO is that compile SDK should usually match target SDK.
关于SO的压倒性建议是编译SDK通常应该与目标SDK匹配。
Really? 真? I would expect few production Android apps to be so configured, outside of newly-created projects, where the new-project wizards tend to set up equal values for those.
我认为在新创建的项目之外,很少有生产Android应用程序如此配置,新项目向导倾向于为这些项目设置相同的值。
what's the harm in making them match
使它们匹配的危害是什么
There is no particular harm in making them match. 使它们匹配没有特别的危害。 There is no particular harm in making them not match.
使它们不匹配没有特别的危害。 It all depends on what you are writing and what behavior you want.
这一切都取决于你在写什么以及你想要什么样的行为。
For example, Android 6.0 introduces a new runtime permissions model, where you have to add code to your app to request dangerous permissions from the user, for everything from WRITE_EXTERNAL_STORAGE
to READ_CONTACTS
. 例如,Android 6.0引入了一个新的运行时权限模型,您必须向应用程序添加代码以请求来自用户的危险权限,从
WRITE_EXTERNAL_STORAGE
到READ_CONTACTS
。 However, this is only used if targetSdkVersion
is 23 or higher. 但是,仅当
targetSdkVersion
为23或更高时才使用此targetSdkVersion
。 If you are not in position to deal with that code change right now, you would specifically leave your targetSdkVersion
at something lower, like 22. 如果你现在无法处理代码更改,那么你应该特别将
targetSdkVersion
在更低的位置,比如22。
However, at the same time, perhaps you want to use the v23 editions of key Android support libraries, like appcompat-v7
. 但是,与此同时,您可能希望使用v23版本的关键Android支持库,例如
appcompat-v7
。 Generally speaking, you will want your compileSdkVersion
to match the major version of the support libraries that you are using, as they may be referencing classes, methods, constants, and such that are only available in that compileSdkVersion
. 一般来说,您需要使用
compileSdkVersion
来匹配您正在使用的支持库的主要版本,因为它们可能引用了只在该compileSdkVersion
可用的类,方法,常量等。
And so, this would be a case where you specifically do not want compileSdkVersion
to match targetSdkVersion
. 因此,这将是您特别不希望
compileSdkVersion
与targetSdkVersion
匹配的targetSdkVersion
。
what's the advantage of having the lowest compile sdk version you can get away with?
拥有最低编译sdk版本的优势是什么?
Nowadays, there is little to no advantage, IMHO. 如今,恕我直言,几乎没有任何优势。
Back in 2008-2011, a common (albeit flawed) recommendation was to have compileSdkVersion
match minSdkVersion
(or, in truth, their Eclipse/Ant equivalents, since Android Studio didn't exist back then). 早在2008 - 2011年,一个常见的(虽然有缺点)建议是使
compileSdkVersion
与minSdkVersion
匹配(或者,事实上,他们的Eclipse / Ant等价物,因为Android Studio当时不存在)。 This was because we lacked tools to automatically tell us if we used something that was valid in our compileSdkVersion
(say, 11) but was not available all the way to our minSdkVersion
(say, 4). 这是因为我们缺少自动告诉我们是否使用了在我们的
compileSdkVersion
(例如11)中有效的东西但是不能一直到我们的minSdkVersion
(比如说4)的工具。 Setting those two values equal meant you got compiler errors if you tried using stuff newer than the minSdkVersion
. 将这两个值设置为相等意味着如果您尝试使用比
minSdkVersion
更新的东西,则会出现编译器错误。 The downside is that you were stuck with the feature set from your minSdkVersion
and could not progressively enhance your app to take advantage of newer features available on newer devices. 缺点是你被
minSdkVersion
的功能集困住了,无法逐步增强你的应用程序以利用新设备上的新功能。
Nowadays, the build tools (specifically Lint) will yell at you if you try using things that are valid in your compileSdkVersion
but not available all the way back to the minSdkVersion
, so you know to put in the appropriate Build.VERSION.SDK_INT
checks to ensure you only use the newer stuff on newer devices and gracefully degrade on older devices. 现在,如果你尝试使用在
compileSdkVersion
中有效但不能一直返回minSdkVersion
东西,构建工具(特别是Lint)会对你大喊大叫,所以你知道要把相应的Build.VERSION.SDK_INT
检查放到确保您只在较新的设备上使用较新的内容,并在旧设备上正常降级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.