Android 面试 - compileSdkVersion、minSdkVersion、targetSdkVersion、buildToolsVersion
在Android的module級build.gradle里,有著為數不少的Version,其中最重要的有以下幾個:compileSdkVersion、buildToolsVersion、minSdkVersion、targetSdkVersion、maxSdkVersion。很多人在對這些版本號進行設定的時候,其實自己腦子里也是迷迷糊糊、混混沌沌的,并不明白它們到底代表了什么。也許你還沒因為它們掉坑里,但是理解這幾個字段的作用其實還是很有必要的。本文大量引用了Android Developer(即Android官網)的資料,有興趣的朋友也可以自行去官網進行學習。下面我們就來看看這么多的Version到底代表了什么,有什么區別,以及最終該如何選擇不同的版本。
compileSdkVersion
compileSdkVersion specifies the Android API level Gradle should use to
compile your app. This means your app can use the API features
included in this API level and lower.
compileSdkVersion指定了Gradle編譯你的App時使用的Android API版本,你的App可以使用該版本或者更低版本的API特性。簡單來說,如果你用了一些比較新的API特性,那么你的compileSdkVersion就不能低了。通常它應該是Android最新的穩定版本。比如Android Pie(9.0)已經正式發布,那么我們的compileSdkVersion最好直接提升到28(即9.0對應的版本號)。
buildToolsVersion
buildToolsVersion specifies the version of the SDK build tools,
command-line utilities, and compiler that Gradle should use to build
your app. You need to download the build tools using the SDK Manager.
buildToolsVersion制定了Gradle在編譯App時使用的SDK build tools、命令行、程序、編譯器等的版本,這些都是通過SDK Manager下載的。它的值實際上是一個字符串,在Android Studio里我們通過Tools->SDK Manager,然后點選“SDK Tools”并勾選“Show Package Details”就能看到本地電腦已經下載的SDK Build-Tools的版本。
一般來說,選擇跟compileSdkVersion同一個大版本下的最新穩定版本即可,如下圖里我已經下載了這么多版本的Build-Tools,在新開發App時我會選擇28.0.3版本。
minSdkVersion
An integer designating the minimum API Level required for the
application to run. 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. 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. 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. For this reason, be certain to declare the
appropriate API Level in theminSdkVersion attribute.
minSdkVersion指定了App運行所需最低的API級別,比如你設置它為23(Android 6.0),那么該App不能在低于Android6.0版本的設備上安裝。從理論上來說,如果你設置minSdkVersion為1,則可以最低兼容到最早的Android版本——然而,這就意味著你要為老設備的兼容做大量的工作,顯然并不可取,畢竟從統計上來說已經沒有人用那么老的設備了。
一般來說,我們通過一些統計結果,以及本公司App的用戶分析,來決定最低兼容版本。
根據Google Play的統計,截止到2018年10月26日,使用Android 4.4及以上版本的設備大概占了96.5%,而5.0及以上的則占了接近89%,所以最低版本兼容到4.4就基本上問題不大了。實際上一些老設備的活躍度會比較差,從這個角度考慮,直接從5.0開始做兼容也是可以的。
而用戶分析則指的是,App面向的用戶的特征。比如用戶群體主要是30歲左右的白領,那么這些用戶普遍換手機的頻率較高,基本上都能追著比較新的Android版本,那么minSdkVersion設置的比較高也問題不大;而如果用戶群體面向50甚至60歲以上的中老年人,這些用戶對換手機、升級系統不是很感冒,所以有必要把minSdkVersion設置的低一些。
記住,千萬不要忘了設置minSdkVersion,如果你不設置,那么就變成了默認值:1,然后你使用的任何在Android 1.0之后才加入的特性,在1.0上運行就崩潰了。
targetSdkVersion
An integer designating the API Level that the application targets. If
not set, the default value equals that given to minSdkVersion.
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).
There are many compatibility behaviors that the system may enable
based on the value you set for this attribute. Several of these
behaviors are described by the corresponding platform versions in the
Build.VERSION_CODES reference.
To maintain your application along with each Android release, you
should increase the value of this attribute to match the latest API
level, then thoroughly test your application on the corresponding
platform version.
targetSdkVersion完全可以按照它的字面意思去理解:目標sdkVersion。如果沒有設置,則默認值為minSdkVersion。當你設置了targetSdkVersion的時候,表示你已經充分測試過了你的App在該目標版本的運行情況,系統不應該啟用任何兼容性行為來保持你的App與目標版本的向前兼容性。
如果系統的API級別高于應用的目標版本,則系統會啟用兼容性行為來確保應用在更高版本系統上的運行。這一點相信做Android開發時間比較久的人都很理解了,只要你寫的程序比較規矩,沒有太多的官方推薦外的行為,那么一個老版本的應用放在幾年后的Android新設備上依然能順利運行,只不過相當多的開發者(尤其是國內的)并不是很遵守規范罷了。
再詳細說說:
targetSdkVersion 是 Android 系統提供前向兼容的主要手段(即:新版本SDK手機兼容舊版本SDK工程)。這是什么意思呢?
舉例:在 Android 4.4 (API 19)以后,AlarmManager 的 set()和 setRepeat()這兩個 API 的行為發生了變化。
在 Android 4.4 以前,這兩個 API 設置的都是精確的時間,系統能保證在 API 設置的時間點上喚醒 Alarm。
在Android 4.4,因為省電原因實現了 AlarmManager 的對齊喚醒,這兩個 API 設置喚醒的時間,系統都對待成不精確的時間,系統只能保證在你設置的時間點之后某個時間喚醒。
雖然 API 沒有任何變化,但是實際上 API 的行為卻發生了變化,如果老的 APK 中使用了此 API,并且在應用中的行為非常依賴 AlarmManager 在精確的時間喚醒,例如鬧鐘應用。如果 Android 系統不能保證兼容,老的 APK 安裝在新的系統上,就會出現問題。
Android 系統是怎么保證這種兼容性的呢?這時候 targetSdkVersion 就起作用了。APK 在調用系統 AlarmManager 的 set()或者 setRepeat()的時候,系統首先會查一下調用的 APK 的 targetSdkVersion 信息,如果小于 19,就還是按照老的行為,即精確設置喚醒時間,如果大于19了,就會崩潰,因為你沒配置4.4的新功能
再簡單的說:
Android 6.0新增加了動態權限申請,我們的targetSdkVersion是22(API 5.0),如果我們運行在23(API 6.0)的設備上怎么辦?
因為我們這個可以向前兼容,向后不行啊,如果你的代碼里處理了Android 6.0的動態權限處理,那么可以的,如果沒呢?那就直接崩潰,糾接著去處理
maxSdkVersion
對于這個屬性,我都懶得貼官方的英文文檔了,簡單來說,它指定了應用所能運行的系統的最高版本。比如你把你的應用的maxSdkVersion設定為26(Android 8.0),則它無法安裝在27以及更高版本的系統中。如果原來安裝在26的設備里,后來設備系統升級到27或28,則該程序不能運行。總而言之,我實在是想不到任何理由去設定maxSdkVersion,所以你不應該給你的應用設定maxSdkVersion——除非你能找到說服自己的理由,但有什么理由不兼容更高的版本和更新的設備呢?
總結:
maxSdkVersion直接放棄。 minSdkVersion根據不同版本占有率和應用面向用戶的群體特征來安排。 targetSdkVersion和compileSdkVersion一般都直接用最新的穩定版本即可。 buildToolsVersion則使用跟compileSdkVersion大版本相同的最新穩定小版本。 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Android 面试 - compileSdkVersion、minSdkVersion、targetSdkVersion、buildToolsVersion的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 面试 - 动画
- 下一篇: Java final、static fi