[英]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.