简体   繁体   English

为什么在启动新项目时需要选择API级别?

[英]Why do I need to choose an API level when I start a new project?

I'm just starting with Android, and I have a question about why I have to choose an API level when I create a new project. 我只是从Android开始,所以我有一个问题,为什么在创建新项目时为什么必须选择API级别。 I know that different APIs have different methods, classes and so on, but why do I have to choose one level? 我知道不同的API具有不同的方法,类等,但是为什么我必须选择一个级别? Couldn't the compiler determine the minimum SDK needed watching which methods I used in the project? 编译器无法确定观看项目中使用的哪些方法所需的最低SDK?

Compiler can determine that which devices will be supported by APIs you used. 编译器可以确定您使用的API将支持哪些设备。 but there will be no way for compiler to warn you if you want to build your app for a lower version and using APIs from higher version. 但是如果您要针对较低版本构建应用并使用较高版本的API,则编译器将无法警告您。

so when you define a minimum SDK version compiler knows what are you targetting for and warns you if you go above that. 因此,当您定义最低SDK版本时,编译器会知道您的目标是什么,如果超出该范围,则会发出警告。

and if you don't define minSdkVersion compiler will assume its 1. 如果未定义minSdkVersion,则编译器将假定其为1。

from the documentation : 文档中

 android:minSdkVersion 

An integer designating the minimum API Level required for the application to run. 一个整数,指定运行应用程序所需的最低API级别。 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级别低于此属性中指定的值,则Android系统将阻止用户安装应用程序。 You should always declare this attribute. 您应该始终声明此属性。

Caution: If you do not declare this attribute, the system assumes a default value of "1", which indicates that your application is compatible with all versions of Android. 注意:如果不声明此属性,则系统将采用默认值“ 1”,这表明您的应用程序与所有版本的Android兼容。 If your application is not compatible with all versions (for instance, it uses APIs introduced in API Level 3) and you have not declared the proper minSdkVersion, then when installed on a system with an API Level less than 3, the application will crash during runtime when attempting to access the unavailable APIs. 如果您的应用程序与所有版本都不兼容(例如,它使用API​​级别3中引入的API),并且您尚未声明正确的minSdkVersion,则当安装在API级别小于3的系统上时,应用程序将在崩溃期间崩溃尝试访问不可用的API时的运行时。 For this reason, be certain to declare the appropriate API Level in the minSdkVersion attribute. 因此,一定要在minSdkVersion属性中声明适当的API级别。

Basically 基本上

Here's a nice explanation of compileSdkVersion: source . 这是compileSdkVersion的一个很好的解释: source
So compileSdkVersion, chosen by a developer, points what files will be used to compile with your code. 因此,由开发人员选择的compileSdkVersion会指出将使用哪些文件来编译您的代码。
For example: 例如:
API 19: Android 4.4 (KitKat) => files from ...\\sdk\\sources\\android-19 API 19: Android 4.4 (KitKat) =>来自...\\sdk\\sources\\android-19
API 22: Android 5.1 (Lollipop) => files from ...\\sdk\\sources\\android-22 API 22: Android 5.1 (Lollipop) =>来自...\\sdk\\sources\\android-22

Influence 影响

It determines: 它确定:
- what functionality will be available to you while you're developing (in general, higher version => more features you can use) -开发过程中将为您提供哪些功能(通常,更高版本=>您可以使用更多功能)
- what external libraries can be exploited in your project -在您的项目中可以利用哪些外部库

Choice 选择

As a rule of thumb: compiledSdkVersion should be the latest possible 根据经验:compiledSdkVersion应该是最新的

Personal experience: sometimes it's very useful to compile your project against API version your device has, especially when you debug, because different APIs => different source files => your breakpoints might not be hit and on the whole the highlighted line in your IDE (when you suspend application) probably won't reflect the state of a program. 个人经验:有时根据设备具有的API版本编译项目非常有用,尤其是在调试时,因为不同的API =>不同的源文件=>断点可能不会被击中,并且总的来说,IDE中突出显示的行(当您暂停应用程序时)可能无法反映程序的状态。

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

相关问题 当我在 Android Studio 中创建一个新项目时,我应该根据项目类型选择哪个 API 级别作为最低 SDK? - When I create a new project in Android Studio, which API level should I choose according to project type as the minimum SDK? 在Android Studio中创建新项目时如何选择目标API级别? - How to choose target API level when creating a new project in Android Studio? 将Maps API项目移至其他开发计算机。 我需要一个新的API密钥吗? - Moving Maps API project to a different development computer. Do I need a new API key? 为什么我需要使用API​​级别16的buttonStyle而不是android:buttonStyle? - Why do I need to use buttonStyle instead of android:buttonStyle with API level 16? 我需要为每个游戏关卡创建一个新的活动吗? - Do i need to create a new activity for every game level? 为什么在启动新的Intent时会看到键盘? - why I see the keyboard when I start a new Intent? 为什么在尝试启动新的Intent时出现异常? - Why I get an exception when I try to start a new Intent? 当我开始一个新项目时,插件 Android Support 中的异常 - Exception in plugin Android Support when I start a new project 启动新项目时无法创建父目录? - Failed to create parent directory when I start a new project? 我需要SpannableStrings还是应该选择其他解决方案? - Do I need SpannableStrings or should I choose another solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM