简体   繁体   English

应该编译sdk低于目标sdk

[英]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_STORAGEREAD_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 . 因此,这将是您特别不希望compileSdkVersiontargetSdkVersion匹配的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年,一个常见的(虽然有缺点)建议是使compileSdkVersionminSdkVersion匹配(或者,事实上,他们的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.

相关问题 此替代库不应使用低于目标SDK 23的版本(21) - this suport libary should not use a lower version (21) than the target SDK 23 如何更改目标SDK并将其编译为25 - How to change target sdk and compile sdk into 25 我是否应该同时更改目标SDK和编译SDK以支持更高版本? - Should I change both the target SDK and compile SDK to support higher versions? 试图用最小于目标SDK的最小SDK编译Eclipse中的Android应用 - Struggling to compile android apps in Eclipse with a min-sdk that is smaller than the target-sdk 针对较低目标SDK的Android O限制 - Android O limits for lower target SDK Android 12 目标 SDK 应用程序未安装在低于 Android 12 的设备上 - Android 12 target SDK Application does not install on devices lower than Android 12 有没有办法在 Android Studio 中仍然使用低于 26 的 API 作为目标 SDK - Is there any way to still use APIs lower than 26 as target SDK in Android Studio 如果我使用低于 Oreo 目标 sdk,那么我的应用程序将不会有 Oreo 背景限制? - if i use lower than Oreo target sdk, then my app will not have Oreo Background Limitation? 最低sdk是否过低(如8 sdk)有什么影响,目标sdk越过sdk(如23 sdk)? - There is any impact of minimum sdk is too lower level (as 8 sdk level) and target sdk is higher sdk level (as 23 sdk level) ? “min sdk,target sdk和compile with”有什么区别?在android中 - what is the difference between “min sdk , target sdk and compile with ” ? in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM