【移动开发】targetSdkVersion的作用

        在AndroidMenifest.xml中,常常会有下面的语句:

 <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="10" android:maxSdkVersion="15" />

        在project.properties中,会看到下面的语句: target=android-10 如果是使用Eclipse的话,还可能会看到这样的警告:

Attribute minSdkVersion (4) is lower than the project target API level (10) 

        那么,这里面的minSdkVersion、maxSdkVersion、targetSdkVersion、target四个属性到底有什么区别?

         minSdkVersion与maxSdkVersion比较容易理解,就是在安装程序的时候,如果目标设备的API版本小于minSdkVersion, 或者大于maxSdkVersion,程序将无法安装。一般来说没有必要设置maxSdkVersion。

         targetSdkVersion相对复杂一些,如果设置了此属性,那么在程序执行时,如果目标设备的API版本正好等于此数值, 他会告诉Android平台:此程序在此版本已经经过充分测,没有问题。不必为此程序开启兼容性检查判断的工作了。 也就是说,如果targetSdkVersion与目标设备的API版本相同时,运行效率可能会高一些。 但是,这个设置仅仅是一个声明、一个通知,不会有太实质的作用, 比如说,使用了targetSdkVersion这个SDK版本中的一个特性,但是这个特性在低版本中是不支持的 ,那么在低版本的API设备上运行程序时,可能会报错:java.lang.VerifyError。也就是说,此属性不会帮你解决兼容性的测试问题。 你至少需要在minSdkVersion这个版本上将程序完整的跑一遍来确定兼容性是没有问题的。(这个问题确实让人头疼)  

         project.properties中的target是指在编译的时候使用哪个版本的API进行编译。 综上,上面的四个值其实是作用于不同的时期:

target API level是在编译的时候起作用,用于指定使用哪个API版本(SDK版本)进行编译。 minSdkVersion和maxSdkVersion是在程序安装的时候起作用, 用于指定哪些版本的设备可以安装此应用。 targetSdkVersion是在程序运行的时候起作用,用于提高指定版本的设备上程序运行体验。

         这四个数值在程序编译时也没有严格的检查,比如说,你可以将minSdkVersion设置的比maxSdkVersion还大,会自动忽略掉错误的maxSdkVersion。

android:targetSdkVersion
An integer designating the API Level that the application targets. If not set, the defaultvalue equals that given to minSdkVersion.

This attribute informs the system that you have tested against the target version and thesystem should not enable any compatibility behaviors to maintain your app's forward-compatibilitywith 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 appcontinues to work the way you expect. You can disable such compatibilitybehaviors by specifying targetSdkVersion to match the APIlevel of the platform on which it's running. For example, setting this value to "11" or higherallows the system to apply a new default theme (Holo) to your app when running on Android 3.0 orhigher and also disables screencompatibility mode when running on larger screens (because support for API level 11 implicitlysupports larger screens).

There are many compatibility behaviors that the system may enable based on the value you setfor this attribute. Several of these behaviors are described by the corresponding platform versionsin the Build.VERSION_CODES reference.

To maintain your application along with each Android release, you should increasethe value of this attribute to match the latest API level, then thoroughly test your application onthe corresponding platform version.

Introduced in: API Level 4


参考资料

http://developer.android.com/guide/topics/manifest/uses-sdk-element.html


posted on 2016-11-25 17:39  毛小娃  阅读(510)  评论(0编辑  收藏  举报

导航