“最小sdk版本/目标sdk版本”和“编译sdk版本”有什么区别?我知道什么是最小和目标sdk的意思,但什么是编译sdk版本的意思?

在Eclipse中,我有min/max和目标sdk,但在android studio中有这三个设置。


最小sdk版本是运行应用程序所需的Android操作系统的最小版本。

目标sdk版本是你的应用程序创建运行的Android版本。

编译sdk版本是构建工具用来编译和构建应用程序以发布、运行或调试的Android版本。

通常编译sdk版本和目标sdk版本是相同的。


min sdk版本是应用程序可以在其上运行的Android sdk的最早版本。这通常是由于早期api的问题,缺乏功能,或其他行为问题。

目标sdk版本是应用程序运行的目标版本。理想情况下,这是因为某种最佳运行条件。如果你要“为版本19制作你的应用”,这就是指定的地方。它可以在较早或较晚的版本上运行,但这是您的目标。这主要是为了表明您的应用程序在市场上的使用情况,等等。

当你发布一个。apk文件时,编译sdk版本是你的IDE(或其他编译方式)用来制作你的应用程序的android版本。这对于测试应用程序很有用,因为在开发应用程序时,编译应用程序是一个常见的需求。因为这将是要编译到APK的版本,所以它自然是您发布的版本。同样,最好让它与你的目标sdk版本相匹配。


minSdkVersion, targetSdkVersion, compileSdkVersion

公式是

minSdkVersion <= targetSdkVersion <= compileSdkVersion

minSdkVersion - is a marker that defines a minimum Android version on which the application will be able to install. Also, it is used by Lint to prevent calling API that doesn’t exist. Also, it has an impact on Build Time. So you can use build flavors to override minSdkVersion to the maximum during the development. It will help to make the build faster using all improvements that the Android team provides for us. For example, some features of Java 8 are available only when you are using specific versions of minSdkVersion.

targetSdkVersion -如果AndroidOS版本是>= targetSdkVersion,它说Android系统打开特定的(新的)行为变化。*请注意,即使targetSdkVersion为<,其中一些新行为也会默认开启,您应该阅读官方文档。

例如:

Starting in Android 6.0 (API level 23) Runtime Permissions were introduced. If you set targetSdkVersion to 22 or lower your application does not ask a user for some permission in run time. Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or it will not appear. 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). Starting in Android 9 (API level 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

compileSdkVersion - actually it is the SDK Platform version and tells Gradle which Android SDK uses to compile. When you want to use new features or debug .java files from Android SDK you should take care of 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. Changing your compileSdkVersion does not change runtime behavior. It can generate for example new compiler warnings/errors. Therefore it is strongly recommended that you always compile with the latest 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. One more fact is compileSdkVersion >= Support Library version

你可以在这里阅读更多信息。 另外,我建议你看一下迁移到Android 8.0的例子。

[buildToolsVersion]


compileSdkVersion: compileSdkVersion是编译应用程序的API版本。这意味着你可以使用该版本的API中包含的Android API特性(当然也包括之前的所有版本)。如果您尝试使用API 16特性,但将compileSdkVersion设置为15,则会得到编译错误。如果你将compileSdkVersion设置为16,你仍然可以在API 15的设备上运行应用程序。

minSdkVersion:最小sdk版本是运行应用程序所需的Android操作系统的最小版本。

targetSdkVersion:目标sdk版本是应用程序运行的目标版本。


参考-媒介文章由Paulina Sadowska

compileSdkVersion定义哪个Android SDK版本将被Gradle用来编译你的应用。

例如:

在Android 12中,也就是SDK版本31中,引入了一个新的API,让我们能够轻松实现启动画面。在这个新的API中,启动画面可以使用这些属性进行定制:

如果你想在你的应用程序中使用该API,你首先必须:

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

只有这样你才能看到这些新属性。只有这样,你才能在你的代码中使用这个新的启动画面API。

2.targetSdkVersion是一个属性,它告诉系统应用程序是在哪个Android版本上设计和测试的。

如果用户在android版本高于应用程序中定义的targetSdkVersion的设备上运行应用程序,对于新的android功能,系统可能会引入一些向后兼容行为,以确保你的应用程序仍然以你设计的方式运行。

例如:

In Android 12 the appearance of custom notifications was changed. 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. 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. Only after you update the target SDK version to 31 the new notification appearance will be used.


这里有一个简单明了的方法来理解——

minSdkVersion应该更低,以目标最大覆盖android设备上的应用程序将被安装。

在开发应用程序时需要compileSdkVersion,以使用最新和优化的android api。

tarketSkdVersion是最新的/版本的android操作系统上,你要运行你的应用程序,以实现android资源的充分优化。

注:如有错误,请指正。 谢谢


在之前的答案中有很多很好的解释,但没有一个链接到官方文件。如果好奇,请看 https://developer.android.com/guide/topics/manifest/uses-sdk-element:

minSdkVersion:

如果系统的API级别低于此属性中指定的值,Android系统将阻止用户安装应用程序。

另外:如果你使用NDK来运行本地代码,minSdkVersion也会影响NDK的API可用性。(https://developer.android.com/ndk/guides/sdk-versions)

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). 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 app continues to work the way you expect. You can disable such compatibility behaviors by specifying targetSdkVersion to match the API level of the platform on which it's running. 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).

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. Furthermore, your code should be able to handle platforms that don't have that new feature (down to your minSdkVersion, e.g. checking your code handles missing APIs that you use gracefully, etc). 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".

详见https://developer.android.com/studio/build:

compileSdkVersion:

compileSdkVersion指定Gradle应该使用的Android API级别 这意味着你的应用程序可以使用中包含的API特性 这个API级别或更低。

理想情况下,您可以将目标和编译版本设置为最高版本,当然您不必使用任何新特性。但是,在准备更新目标版本之前,您可能希望将目标版本保持在已经发布的旧版本上,同时使用更新的编译版本以获得更好的警告/错误。在过去,它还允许用户通过Android Gradle插件升级在代码中使用更新的Java语言特性,而不依赖于目标Android api。


最后,不要忘记谷歌最近的目标API级别要求,这基本上要求你在特定日期之前发布一个针对最新API级别的构建,如果你希望Play商店上使用比你的目标API更新的操作系统的用户仍然可用。这是为了激励应用开发社区提供更新的性能/安全增强(比如当你请求位置信息时,给用户更多的隐私选项)。

Every version of Android released since 9 lists behavior changes that will impact all apps regardless of your targetSdkVersion (e.g. here's Android 12's), and what changes when you specifically target it (e.g. 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.