简体   繁体   English

最小 SDK 版本/目标 SDK 版本与编译 SDK 版本有什么区别?

[英]What is the difference between min SDK version/target SDK version vs. compile SDK version?

What are the differences between "min sdk version/target sdk version" and "compile sdk version"? “min sdk version/target sdk version”和“compile sdk version”有什么区别? I know what min and target sdk means, but what does compile sdk version mean?我知道 min 和 target sdk 是什么意思,但是 compile sdk version 是什么意思?

In Eclipse, I have min/max and target sdk, but in android studio there are these three settings.在 Eclipse 中,我有 min/max 和 target sdk,但在 android studio 中有这三个设置。

The min sdk version is the earliest release of the Android SDK that your application can run on. min sdk 版本是您的应用程序可以在其上运行的 Android SDK 的最早版本。 Usually this is because of a problem with the earlier APIs, lacking functionality, or some other behavioural issue.这通常是因为早期 API 存在问题、缺少功能或其他一些行为问题。

The target sdk version is the version your application was targeted to run on.目标sdk 版本是您的应用程序要在其上运行的版本。 Ideally, this is because of some sort of optimal run conditions.理想情况下,这是因为某种最佳运行条件。 If you were to "make your app for version 19", this is where that would be specified.如果你要“为版本 19 制作你的应用程序”,这就是要指定的地方。 It may run on earlier or later releases, but this is what you were aiming for.它可能在早期或以后的版本上运行,但这是您的目标。 This is mostly to indicate how current your application is for use in the marketplace, etc.这主要是为了表明您的应用程序在市场上的使用情况等。

The compile sdk version is the version of android your IDE (or other means of compiling I suppose) uses to make your app when you publish a .apk file.编译 sdk 版本是您的 IDE(或我想的其他编译方式)在发布.apk文件时用于制作应用程序的 android 版本。 This is useful for testing your application as it is a common need to compile your app as you develop it.这对于测试您的应用程序很有用,因为在开发应用程序时编译您的应用程序是一种常见的需求。 As this will be the version to compile to an APK, it will naturally be the version of your release.由于这将是编译为 APK 的版本,因此它自然是您发布的版本。 Likewise, it is advisable to have this match your target sdk version.同样,建议将此匹配您的目标 sdk 版本。

minSdkVersion, targetSdkVersion, compileSdkVersion minSdkVersion, targetSdkVersion, compileSdkVersion

The formula is公式是

minSdkVersion <= targetSdkVersion <= compileSdkVersion

minSdkVersion - is a marker that defines a minimum Android version on which the application will be able to install. minSdkVersion - 是一个标记,定义了应用程序能够安装的最低 Android 版本。 Also, it is used by Lint to prevent calling API that doesn't exist.此外,Lint 使用它来防止调用不存在的 API。 Also, it has an impact on Build Time.此外,它对构建时间有影响。 So you can use build flavors to override minSdkVersion to the maximum during the development.因此,您可以在开发过程中使用构建风格将 minSdkVersion 覆盖到最大值。 It will help to make the build faster using all improvements that the Android team provides for us.使用 Android 团队为我们提供的所有改进将有助于加快构建速度。 For example, some features of Java 8 are available only when you are using specific versions of minSdkVersion.例如,Java 8 的某些功能仅在您使用特定版本的 minSdkVersion 时可用。

targetSdkVersion - If AndroidOS version is >= targetSdkVersion it says Android system to turn on specific(new) behavior changes. targetSdkVersion - 如果 AndroidOS 版本>= targetSdkVersion ,它表示 Android 系统打开特定(新) behavior更改。 *Please note that some of these new behaviors will be turned on by default even if thought targetSdkVersion is < , you should read the official documentation. *请注意,即使认为targetSdkVersion< ,这些新行为也会默认开启,您应该阅读官方文档。

For example:例如:

  • Starting in Android 6.0 (API level 23) Runtime Permissions were introduced.从 Android 6.0(API 级别 23)开始,引入了Runtime Permissions If you set targetSdkVersion to 22 or lower your application does not ask a user for some permission in run time.如果您将targetSdkVersion设置为 22 或更低,您的应用程序不会在运行时向用户请求某些权限。

  • Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear.从 Android 8.0(API 级别 26)开始,所有notifications都必须分配给一个频道,否则它不会出现。 On devices running Android 7.1 (API level 25) and lower, users can manage notifications on a per-app basis only (effectively each app only has one channel on Android 7.1 and lower).在运行 Android 7.1(API 级别 25)及更低版本的设备上,用户只能基于每个应用管理通知(实际上每个应用在 Android 7.1 及更低版本上只有一个通道)。

  • Starting in Android 9 (API level 28), Web-based data directories separated by process .从 Android 9(API 级别 28)开始, Web-based data directories separated by process If targetSdkVersion is 28+ and you create several WebView in different processes you will get java.lang.RuntimeException如果targetSdkVersion为 28+,并且您在不同的进程中创建了多个WebView ,您将获得java.lang.RuntimeException

compileSdkVersion - actually it is the SDK Platform version and tells Gradle which Android SDK uses to compile. compileSdkVersion - 实际上它是 SDK 平台版本,并告诉 Gradle 使用哪个 Android SDK 进行编译。 When you want to use new features or debug .java files from Android SDK you should take care of compileSdkVersion.当您想使用新功能或调试 Android SDK 中的.java文件时,您应该注意 compileSdkVersion。 One more example is using AndroidX that forces to use compileSdkVersion - level 28. compileSdkVersion is not included in your APK : it is purely used at compile time .另一个例子是使用强制使用compileSdkVersion级别 28 compileSdkVersion compileSdkVersion包含在您的 APK 中:它纯粹在compile time使用。 Changing your compileSdkVersion does not change runtime behavior.更改 compileSdkVersion 不会更改运行时行为。 It can generate for example new compiler warnings/errors.例如,它可以生成新的编译器警告/错误。 Therefore it is strongly recommended that you always compile with the latest SDK.因此强烈建议您始终使用最新的 SDK 进行编译。 You'll get all the benefits of new compilation checks on existing code, avoid newly deprecated APIs, and be ready to use new APIs.您将获得对现有代码进行新编译检查的所有好处,避免新弃用的 API,并准备好使用新的 API。 One more fact is compileSdkVersion >= Support Library version另一个事实是compileSdkVersion >= Support Library version

You can read more about it here .你可以在这里阅读更多关于它的信息。 Also, I would recommend you to take a look at the example of migration to Android 8.0.另外,我建议您看一下迁移到 Android 8.0 的示例。

[buildToolsVersion] [构建工具版本]

The min sdk version is the minimum version of the Android operating system required to run your application. min sdk 版本是运行应用程序所需的最低 Android 操作系统版本。

The target sdk version is the version of Android that your app was created to run on.目标 sdk 版本是创建您的应用程序以在其上运行的 Android 版本。

The compile sdk version is the the version of Android that the build tools uses to compile and build the application in order to release, run, or debug. compile sdk 版本构建工具用于编译和构建应用程序以便发布、运行或调试的 Android 版本。

Usually the compile sdk version and the target sdk version are the same.通常编译 sdk 版本和目标 sdk 版本是相同的。

compileSdkVersion : The compileSdkVersion is the version of the API the app is compiled against. compileSdkVersion :compileSdkVersion 是应用程序编译所针对的 API 版本。 This means you can use Android API features included in that version of the API (as well as all previous versions, obviously).这意味着您可以使用该 API 版本中包含的 Android API 功能(显然,也可以使用所有以前的版本)。 If you try and use API 16 features but set compileSdkVersion to 15, you will get a compilation error.如果您尝试使用 API 16 功能但将 compileSdkVersion 设置为 15,您将收到编译错误。 If you set compileSdkVersion to 16 you can still run the app on a API 15 device.如果您将 compileSdkVersion 设置为 16,您仍然可以在 API 15 设备上运行该应用程序。

minSdkVersion : The min sdk version is the minimum version of the Android operating system required to run your application. minSdkVersion :min sdk 版本是运行应用程序所需的最低 Android 操作系统版本。

targetSdkVersion : The target sdk version is the version your app is targeted to run on. targetSdkVersion :目标 sdk 版本是您的应用程序要运行的版本。

Reference- Medium Article by Paulina Sadowska 参考 - Paulina Sadowska 的 Medium 文章

  1. compileSdkVersion defines which Android SDK version will be used by Gradle to compile your app. compileSdkVersion 定义 Gradle 将使用哪个 Android SDK 版本来编译您的应用程序。

For example:例如:

In Android 12, so in SDK version 31, there was a new API introduced, that allows us to easily implement a splash screen.在 Android 12 中,因此在 SDK 版本 31 中,引入了一个新的 API,使我们能够轻松实现启动画面。 In this new API, the splash screen can be customized using those properties:在这个新的 API 中,可以使用这些属性自定义启动画面:

If you want to use that API in your app you first have to:如果您想在您的应用程序中使用该 API,您首先必须:

i)  download SDK version 31 in Android Studio,
ii) and then: update compileSdkVersion to 31 in your app.

Only then you can see these new properties.只有这样您才能看到这些新属性。 And only then you can use this new splash screen API in your code.只有这样,您才能在代码中使用这个新的闪屏 API。

2.targetSdkVersion is a property that tells the system for which Android version the app was designed and tested on. 2.targetSdkVersion 是一个属性,它告诉系统应用程序是在哪个 Android 版本上设计和测试的。

If the user runs your app on a device with an android version that is higher than the targetSdkVersion defined in your app, for new android features, the system may introduce some backward-compatibility behavior to ensure your app still looks and works in a way that you designed it.如果用户在 android 版本高于应用中定义的 targetSdkVersion 的设备上运行应用,对于新的 android 功能,系统可能会引入一些向后兼容的行为,以确保应用的外观和工作方式仍然符合以下要求:你设计的。

For example:例如:

In Android 12 the appearance of custom notifications was changed.在 Android 12 中,自定义通知的外观发生了变化。 Previously they could use the whole notification area, but in Android 12 system applies the standard template to all custom notifications so they look more consistent.以前他们可以使用整个通知区域,但在 Android 12 中,系统将标准模板应用于所有自定义通知,因此它们看起来更加一致。 If your targetSdkVersion is below 31 system will assume that you haven't tested that feature and will display notifications in the old way to minimize the risk that notification will not be displayed properly.如果您的 targetSdkVersion 低于 31,系统将假定您尚未测试该功能,并将以旧方式显示通知,以最大程度地降低通知无法正确显示的风险。 Only after you update the target SDK version to 31 the new notification appearance will be used.只有在您将目标 SDK 版本更新到 31 后,才会使用新的通知外观。

看到这里,最好在目标sdk版本中指定最新发布的API版本: compileSdkVersion和targetSdkVersion有什么区别?

Lots of good explanations in earlier answers, but none link to the official docs.早期答案中有很多很好的解释,但没有链接到官方文档。 If curious, see https://developer.android.com/guide/topics/manifest/uses-sdk-element for:如果好奇,请参阅https://developer.android.com/guide/topics/manifest/uses-sdk-element了解:

  • minSdkVersion : minSdkVersion

The Android system will prevent the user from installing the application if the system's API Level is lower than the value specified in this attribute.如果系统的 API Level 低于此属性中指定的值,Android 系统将阻止用户安装应用程序

Aside : if you use the NDK to run native code, minSdkVersion also impacts the NDK's API availability.另外:如果您使用 NDK 运行本机代码, minSdkVersion也会影响 NDK 的 API 可用性。 ( https://developer.android.com/ndk/guides/sdk-versions ) https://developer.android.com/ndk/guides/sdk-versions

  • targetSdkVersion : targetSdkVersion

This attribute informs the system that you have tested against the target version and the system should not enable any compatibility behaviors to maintain your app's forward-compatibility with the target version.此属性通知系统您已针对目标版本进行了测试,并且系统不应启用任何兼容性行为以保持您的应用程序与目标版本的前向兼容性。 The application is still able to run on older versions (down to minSdkVersion).该应用程序仍然能够在旧版本上运行(低至 minSdkVersion)。

As Android evolves with each new version, some behaviors and even appearances might change.随着 Android 随着每个新版本的发展,某些行为甚至外观可能会发生变化。 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 app continues to work the way you expect .但是,如果平台的 API 级别高于您应用的 targetSdkVersion 声明的版本,系统可能会启用兼容性行为,以确保您的应用继续按照您期望的方式运行 You can disable such compatibility behaviors by specifying targetSdkVersion to match the API level of the platform on which it's running.您可以通过指定 targetSdkVersion 以匹配其运行平台的 API 级别来禁用此类兼容性行为。 For example, setting this value to "11" or higher allows the system to apply a new default theme (Holo) to your app when running on Android 3.0 or higher and also disables screen compatibility mode when running on larger screens (because support for API level 11 implicitly supports larger screens).例如,将此值设置为“11”或更高允许系统在 Android 3.0 或更高版本上运行时将新的默认主题 (Holo) 应用到您的应用程序,并且还可以在大屏幕上运行时禁用屏幕兼容模式(因为支持 API 11 级隐式支持更大的屏幕)。

So the assumption is that you developed the app with the target API in mind, and have tested that everything looks/behaves as you expected, esp if you're trying to use features introduced in this API.因此,假设您在开发应用程序时考虑了目标 API,并测试了一切看起来/行为都符合您的预期,尤其是如果您尝试使用此 API 中引入的功能。 Furthermore, your code should be able to handle platforms that don't have that new feature (down to your minSdkVersion , eg checking your code handles missing APIs that you use gracefully, etc).此外,您的代码应该能够处理没有该新功能的平台(直到您的minSdkVersion ,例如检查您的代码是否处理您正常使用的缺失 API 等)。 But even newer Android versions may do things to keep your app running, which might otherwise break or look even funkier if the OS didn't enable "compatibility behaviors".但即使是较新的 Android 版本也可能会做一些事情来保持您的应用程序运行,如果操作系统没有启用“兼容性行为”,这可能会破坏或看起来更时髦。

See https://developer.android.com/studio/build for:请参阅https://developer.android.com/studio/build了解:

  • compileSdkVersion : compileSdkVersion

compileSdkVersion specifies the Android API level Gradle should use to compile your app. compileSdkVersion 指定 Gradle 用于编译您的应用程序的 Android API 级别。 This means your app can use the API features included in this API level and lower.这意味着您的应用可以使用此 API 级别及更低级别中包含的 API 功能。

Ideally you'd set target & compile versions the same to the highest release, and of course you don't have to use any of the new features.理想情况下,您将目标和编译版本设置为与最高版本相同,当然您不必使用任何新功能。 But you may wish to keep your target at an older version you've already released, while using a newer compile version for better warnings/errors, until you're ready to update the target version.但是您可能希望将您的目标保持在您已经发布的旧版本,同时使用更新的编译版本以获得更好的警告/错误,直到您准备好更新目标版本。 In the past it also let one use newer Java language features in their code with an Android Gradle plugin upgrade, independent of the target Android APIs. 过去,它还允许人们通过 Android Gradle 插件升级在其代码中使用更新的 Java 语言功能,而与目标 Android API 无关。


Lastly, don't forget about Google's more recent target API level requirements , which basically require you to release a build targeting up-to-date API levels by a certain date if you want to still be available to users on the Play store that use an OS newer than your target API.最后,不要忘记 Google 最新的目标 API 级别要求,这基本上要求您在特定日期之前发布针对最新 API 级别的构建,如果您希望 Play 商店中使用比您的目标 API 更新的操作系统。 This is to motivate the app dev community make available newer performance/security enhancements (like giving the user more privacy options when you request location info) .这是为了激励应用开发社区提供更新的性能/安全增强功能(例如在您请求位置信息时为用户提供更多隐私选项)

Every version of Android released since 9 lists behavior changes that will impact all apps regardless of your targetSdkVersion (eg here's Android 12's ), and what changes when you specifically target it (eg Behavior changes: Apps targeting Android 12 . When the next version is in preview is a good time to start checking your app's compatibility with the upcoming release, even if it's just that any compat modes are ok without changing your compileSdkVersion , if you aren't prepping to target it yet. The Compatibility framework tools can help with that and in the migration to using new APIs.自 9 版以来发布的每个 Android 版本都列出了将影响所有应用程序的行为更改,无论您的targetSdkVersion (例如这里是 Android 12's ),以及当您专门针对它时会发生什么变化(例如, 行为更改:针对 Android 12的应用程序。当下一个版本发布时预览是开始检查您的应用程序与即将发布的版本的兼容性的好时机,即使只是任何兼容模式都可以,而无需更改您的compileSdkVersion ,如果您还没有准备好针对它。 兼容性框架工具可以帮助解决这个问题以及迁移到使用新 API 的过程中。

Here is the little clear and easy way to understand -这是一个清晰易懂的小方法-

minSdkVersion should be lower to target the max coverage of android devices on which the app will be installed. minSdkVersion应该较低,以针对将安装应用程序的 android 设备的最大覆盖范围。

compileSdkVersion is required while developing the app to use the latest and optimize APIs of android. compileSdkVersion在开发应用程序以使用最新的和优化的 android API 时是必需的。

tarketSkdVersion is the latest/version of android OS on which you want to run your app to achieve the full optimization of android resources. tarketSkdVersion是您希望在其上运行您的应用程序以实现对 android 资源的全面优化的最新/版本的 android 操作系统。

Note- please correct me if there is a mistake.注意 - 如果有错误,请纠正我。 thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM