Android Studio Series 8-SDK version configuration under Windows environment

Source: Internet
Author: User

1. The origin of the problem

In a debugging issue, the following error message appears:

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

later Baidu/google "Android.os.NetworkOnMainThreadException", [1] In 2013-06-24, blog post pointed out that Networkonmainthreadexception is because the application tries network operations on the main thread. Additionally, this exception is thrown on the Honeycomb SDK or later SDK. The lower version of the SDK allows the application to perform network operations on the main thread or loop thread, but does not encourage this. It should be placed in a sub-thread, and then broadcast when the child thread is finished, message, notify the main thread to make the corresponding UI change, or use Asynctask to do the network operation.

This issue involves the SDK version configuration in the Androidmanifest.xml file, and as Android Studio defaults to compiling with Gradle, Build.gradle file also has the corresponding SDK version configuration, so how to properly configure these SDK versions?

2. minsdkversion and Targetsdkversion in the Androidmanifest.xml file

reference [2], The ,<uses-sdk> tag in the Androidmanifest.xml file has minsdkversion, Targetsdkversion, maxsdkversion three properties, Because the Maxsdkversion property is no longer recommended for configuration, the following are mainly discussed in the other two properties.

First minsdkversion and targetsdkversion are integer values, which are usually configured without default values.

Minsdkversion is the minimum version of the SDK required for the application to run. If this property is not specified, its default value is 1. If the Android phone version is below this value, the application will fail to install.


[3] See the Android source code and two examples to understand targetsdkversion:

Example 1: datepickerdialog Components

when the Targetsdkversion property is set to a different value in the Androidmanifest.xml file,datepickerdialog component performance will be different. If the value is set to a value of 10 or lower, Datepickerdialog is displayed on the left. If the value is set to a value of 11 or higher, Datepickerdialog is displayed to the right.

android source is as follows (note: The following code may change in the latest SDK version, But judging Targetsdkversion's logic does not change):

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);}

Getapplicationinfo (). targetsdkversion >= some_version. actually on Android There are similar judgments everywhere in the source code

Example 2 webview class

 targetsdkversion property is 18 or higher, the public method of the WebView class should be called in the main thread, or the runtime will throw a runtimeexception

senforcethreadchecking = Context.getapplicationinfo (). targetsdkversion >= Build.version_codes.    JELLY_BEAN_MR2; if (senforcethreadchecking) {throw new RuntimeException (Throwable);}

Android官方文档中提到, 随着Android新版本的发展,一些行为甚至appearances 可能发生改变从前面两个例子已经可以看出。

To summarize: When setting targetsdkversion to a higher value, it is likeAs in the Datepickerdialog example, there will beappearance enhanced benefits, but just as webview example, code that was previously running normally in the lower version, if modify targetsdkversion for higher may cause a fatal error, but at this point in the development phase you can revise the code to accommodate a higher targetsdkversion . soTargetsdkversionProperty tells the system that the application has been fully tested on the target version system.

However, it may be targetsdkversion that one of the new features in this SDK version is not supported in the low version, then when running the program on a lower version of the API device, you may get an error: Java.lang.VerifyError. That is, this property does not help you resolve the compatibility test problem. You need at least a complete run of the program on Minsdkversion this version to determine compatibility is no problem.

Therefore, minsdkversion and targetsdkversion need to run the program completely once to ensure compatibility is not a problem.

3. SDK version configuration in Build.gradle

The following code is available in Build.gradle:

Android {compilesdkversion buildtoolsversion "21.1.1" Defaultconfig {Minsdkversi On 8 targetsdkversion 19}

AndroidManifest.xmlThe following code is in the file:

<USES-SDK android:minsdkversion= "8" android:targetsdkversion= "8"/>

The Gradle file overwrites the values in the manifest file, you can configure both Minsdkversion and targetsdkversion in the Gradle file without having to control the values in the Mainfest file, or you can delete the Mainfest file <uses-sdk> tags.


In the Build.gradle file,

Compilesdkversion is the API level for Android at compile time.

Buildtoolsversion is the version of the compiler (AAPT, DX, renderscript compiler, etc.). Each API level starting with version 18 will have a matching. 0.0 compiler version. For example, in IO 2014, API 20 and Build-tools 20.0.0 are released.

Minsdkversion and Targetsdkversion have been discussed earlier.

Note: Targetsdkversion should not exceed compilesdkversion.


In [4] a method of unifying the various versions is also given:

Edit gradle.properties file, add the following code:

Android_build_min_sdk_version=14android_build_target_sdk_version=21android_build_tools_version=21.1.3android_ Build_sdk_version=21

You can then build.gradle Use this in a file:

... 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. Legacy issues 

In the Build.gradle file, in addition to the version-related configuration described above, the version configuration during compilation is also involved in the dependencies, which is left for further study:

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 solution, 2013-06-24, http://www.cnblogs.com/kissazi2/archive/ 2013/06/24/3153307.html

[2] Android <uses-sdk> properties and Target attribute analysis, 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 does I add a library project to the Android Studio?http://stackoverflow.com/questions/16588064/how-do-i-add-a-libra ry-project-to-the-android-studio/16639227#16639227

Android Studio Series 8-SDK version configuration under Windows environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.