【移動開發】targetSdkVersion的做用

        在AndroidMenifest.xml中,經常會有下面的語句:html

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

        在project.properties中,會看到下面的語句: target=android-10 若是是使用Eclipse的話,還可能會看到這樣的警告:java

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

        那麼,這裏面的minSdkVersion、maxSdkVersion、targetSdkVersion、target四個屬性到底有什麼區別?android

         minSdkVersion與maxSdkVersion比較容易理解,就是在安裝程序的時候,若是目標設備的API版本小於minSdkVersion, 或者大於maxSdkVersion,程序將沒法安裝。通常來講沒有必要設置maxSdkVersion。app

         targetSdkVersion相對複雜一些,若是設置了此屬性,那麼在程序執行時,若是目標設備的API版本正好等於此數值, 他會告訴Android平臺:此程序在此版本已經通過充分測,沒有問題。沒必要爲此程序開啓兼容性檢查判斷的工做了。 也就是說,若是targetSdkVersion與目標設備的API版本相同時,運行效率可能會高一些。 可是,這個設置僅僅是一個聲明、一個通知,不會有太實質的做用, 好比說,使用了targetSdkVersion這個SDK版本中的一個特性,可是這個特性在低版本中是不支持的 ,那麼在低版本的API設備上運行程序時,可能會報錯:java.lang.VerifyError。也就是說,此屬性不會幫你解決兼容性的測試問題。 你至少須要在minSdkVersion這個版本上將程序完整的跑一遍來肯定兼容性是沒有問題的。(這個問題確實讓人頭疼)  ide

         project.properties中的target是指在編譯的時候使用哪一個版本的API進行編譯。 綜上,上面的四個值實際上是做用於不一樣的時期:測試

target API level是在編譯的時候起做用,用於指定使用哪一個API版本(SDK版本)進行編譯。 minSdkVersion和maxSdkVersion是在程序安裝的時候起做用, 用於指定哪些版本的設備能夠安裝此應用。 targetSdkVersion是在程序運行的時候起做用,用於提升指定版本的設備上程序運行體驗。ui

         這四個數值在程序編譯時也沒有嚴格的檢查,好比說,你能夠將minSdkVersion設置的比maxSdkVersion還大,會自動忽略掉錯誤的maxSdkVersion。this

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).spa

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).code

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

相關文章
相關標籤/搜索