Windows環境下Android Studio系列8—SDK版本配置

來源:互聯網
上載者:User

標籤:

1. 問題的由來

在一次調試問題中,出現下面錯誤資訊:

09-07 09:15:08.000    1342-1342/? W/System.err﹕ android.os.NetworkOnMainThreadException09-07 09:15:08.000    1342-1342/? W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)09-07 09:15:08.000    1342-1342/? W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)09-07 09:15:08.000    1342-1342/? W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)09-07 09:15:08.000    1342-1342/? W/System.err﹕ at java.net.InetAddress.getByName(InetAddress.java:289)略。。。。。。

後來百度/Google"android.os.NetworkOnMainThreadException", [1]中在2013-06-24的博文中就指出:NetworkOnMainThreadException是因為應用程式在主線程上嘗試網路操作。另外,這個異常在Honeycomb SDK或更高版本的SDK上才會拋出。低版本的SDK允許應用程式在主線程或loop線程執行網路操作,但不鼓勵這樣做。應該放到子線程中,然後當子線程完成之後再發廣播也好,message也好,通知主線程做相應的UI更改,或利用AsyncTask來做網路操作。

這個問題涉及到AndroidManifest.xml檔案中的SDK版本配置,而且隨著Android Studio預設使用Gradle來編譯,build.gradle檔案中也存在相應的SDK版本配置,那麼如何來正確地配置這些SDK版本呢?

2. AndroidManifest.xml檔案中的minSdkVersion與targetSdkVersion

參考[2], AndroidManifest.xml檔案中,<uses-sdk>標籤中有minSdkVersion, targetSdkVersion, maxSdkVersion三個屬性,由於maxSdkVersion屬性已經不再推薦配置,因此下面主要討論另外兩個屬性。

首先minSdkVersion與targetSdkVersion都是整數值,通常都需要配置而不要採用預設值。

minSdkVersion是應用程式運行所需的SDK最低版本。如果不指明該屬性,其預設值為1。如果Android手機版本低於這個值,應用程式將安裝失敗。


[3]中通過查看Android源碼和兩個例子來理解targetSdkVersion:

例子1: DatePickerDialog組件

當AndroidManifest.xml檔案中 targetSDKversion屬性設定不同的值,DatePickerDialog組件表現會不同。如果設定的值為10或更低的值,DatePickerDialog為左側顯示。如果設定的值為11或更高的值,DatePickerDialog為右側顯示。

 

Android源碼如下(注意:下面代碼可能在最新的SDK版本中有變化,但判斷targetSdkVersion的邏輯沒變):

public DatePickerDialog(Context context,    OnDateSetListener callBack,    int year,    int monthOfYear,    int dayOfMonth,    boolean yearOptional) {        this(context, context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB                                ? com.android.internal.R.style.Theme_Holo_Light_Dialog_Alert                : com.android.internal.R.style.Theme_Dialog_Alert,        callBack, year, monthOfYear, dayOfMonth, yearOptional);}

在第7行根據設定的targetSDKversion來設定不同的theme:getApplicationInfo().targetSdkVersion >= SOME_VERSION。實際上在Android源碼中到處都有類似的判斷

例子2: Webview類

當設定 targetSDKversion屬性為18或更高值時,Webview類的public方法應當在主線程中調用,否則運行時系統將拋出一個RuntimeException。可參見下面原始碼:

sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >=            Build.VERSION_CODES.JELLY_BEAN_MR2;    if (sEnforceThreadChecking) {    throw new RuntimeException(throwable);}

Android官方文檔中提到, 隨著Android新版本的發展,一些行為甚至appearances 可能發生改變從前面兩個例子已經可以看出。

總結一下:當設定targetSdkVersion為更高的值時,就像DatePickerDialog例子中那樣會有appearance增強好處,但正如Webview例子那樣,以前在低版本中正常啟動並執行代碼,如果修改targetSdkVersion為更高值時可能會引發致命錯誤,但此時在開發階段就可以修訂代碼來適應更高的targetSdkVersion。因此targetSdkVersion屬性告訴系統該應用已經在target Version系統上進行了充分測試。

但可能targetSdkVersion這個SDK版本中的某個新特性在低版本中是不支援的,那麼在低版本的API裝置上運行程式時,可能會報錯:java.lang.VerifyError。也就是說,此屬性不會幫你解決相容性的測試問題。你至少需要在minSdkVersion這個版本上將程式完整的跑一遍來確定相容性是沒有問題的。

因此,需要在minSdkVersion與targetSdkVersion分別需要將程式完整的跑一遍來確保相容性沒有問題。

3. build.gradle中的SDK版本配置 

build.gradle中會有以下代碼:

android {        compileSdkVersion 19        buildToolsVersion "21.1.1"             defaultConfig {            minSdkVersion 8            targetSdkVersion 19        }

AndroidManifest.xml檔案中有以下代碼:

<uses-sdk    android:minSdkVersion="8"    android:targetSdkVersion="8"/>

Gradle檔案會覆蓋manifest檔案中的值,可以不用管mainfest檔案中的值,將minSdkVersion和targetSdkVersion都在Gradle檔案中配置即可,也可以刪除mainfest檔案中的<uses-sdk>標籤。


在build.gradle檔案中,

compileSdkVersion是編譯時間Android的API level。

buildToolsVersion是編譯器(aapt, dx, renderscript compiler等)的版本. 從版本18開始的每個API level, 會有一個匹配的.0.0編譯器版本。例如在IO 2014, 發布了API 20和build-tools 20.0.0。

minSdkVersion與targetSdkVersion在前面已經討論過。

注意:targetSdkVersion不應當超過compileSdkVersion。


在[4]中還給出了一種統一各個版本的方法:

編輯 gradle.properties 檔案,增加以下代碼:

ANDROID_BUILD_MIN_SDK_VERSION=14ANDROID_BUILD_TARGET_SDK_VERSION=21ANDROID_BUILD_TOOLS_VERSION=21.1.3ANDROID_BUILD_SDK_VERSION=21

然後,就可以在 build.gradle 檔案中這樣使用:

//...android {    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION    defaultConfig {        minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)        targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)    }}//..
4. 遺留問題 

在build.gradle檔案中,除了上述與版本相關的配置以外,在dependencies中也涉及編譯期間的版本配置,這留待後續再學習研究:

dependencies {    compile ‘com.android.support:support-v4:20.0.0‘        // your unit test dependencies as described in the article    unitTestCompile files("$project.buildDir/classes/release")    unitTestCompile ‘junit:junit:4.11‘    unitTestCompile ‘com.google.android:android:4.1.1.4‘    unitTestCompile ‘org.robolectric:robolectric:2.1.1‘    // duplicate these dependencies in the instrumentTestCompile scope     // in order to have the integration in Android Studio (autocompletion and stuff)    instrumentTestCompile ‘junit:junit:4.11‘    instrumentTestCompile ‘org.robolectric:robolectric:2.1.1‘}


5. 參考資料

[1] android.os.NetworkOnMainThreadException的解決方案, 2013-06-24, http://www.cnblogs.com/kissazi2/archive/2013/06/24/3153307.html

[2] Android中<uses-sdk>屬性和target屬性分析, 2014-06-03, http://blog.csdn.net/fuzhengchao/article/details/28121193

[3] Android Min SDK Version vs. Target SDK Version, http://stackoverflow.com/questions/4568267/android-min-sdk-version-vs-target-sdk-version

[4] How do I add a library project to the Android Studio?http://stackoverflow.com/questions/16588064/how-do-i-add-a-library-project-to-the-android-studio/16639227#16639227

Windows環境下Android Studio系列8—SDK版本配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.