Android learning-uses-sdk tag details, androiduses-sdk

Source: Internet
Author: User

Android learning-uses-sdk tag details, androiduses-sdk
1 Preface

We all know that the Android version is constantly being iterated, and each version adds different new features. As the number of Android users increases, Android Developers must be familiar with the features of various Android versions and ensure that their applications can run normally in different versions.

Because there are too many Android versions, developers are always limited in energy, and it is impossible to get all versions at once. At this time, we must use one method to describe the lowest Android version and the highest version supported by the current application. At the same time, the system can automatically obtain the information and determine whether the current system version supports the application, unsupported versions are not installed. This prevents users from discovering negative effects that are not supported after installation.

Of course, the Google giant must have taken this into consideration. Therefore, the <uses-sdk> tag is provided, which contains three fields: minSdkVersion, targetSdkVersion, and maxSdkVersion, this article will go with you to learn more.

Sample Code: https://github.com/xiaoxuetu/gs-android/tree/master/gs-target-setting

Official documents: https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#provisional

Attention

Attached sample code, you can download it and use Android Studio to import learning practices

 

2. What is API Level?

Before explaining the <uses-sdk> tag, let's take a look at what API Leval is like?

According to the official statement, API Level is an integer used in Android.Framework Version. It is not the SDK version we used during development, nor the Android version, but it corresponds to the Android version we used. In the following table, the API Level used by Android 2.3 and 2.3.1 is 9, while that used by 2.3.3 is 10.

Is the correspondence between the Android system version and API Level in the official Google documentation.

 

3 <uses-sdk> label description

The <uses-sdk> tag is used in the AndroidMainfest. xml file. Its usage is:

1 <uses-sdk android:minSdkVersion="integer"2           android:targetSdkVersion="integer"3           android:maxSdkVersion="integer" />

Note the following:

In the past, when we used Eclipse for development, uses-sdk was required to be declared directly in AndroidMainfest. xml.

Now we use Android Studio and declare it in build. gradle of the app module. It will be automatically generated to AndroidMainfest. xml during compilation. You can directly open gs-target-setting/app/build/intermediates/manifests/full/debug/AndroidMainfest. xml for viewing.

If you declare it directly in AndroidMainfest. xml, it will be ignored during compilation, and the original value will be overwritten Based on build. gradle.

 

All right, we all know that the uses-sdk tag contains three attributes: minSdkVersion, maxSdkVersion, and targetSdkVersion. In addition, when we use Android Studio, the project we created is also a Gradle project, the compileSdkVersion and buildToolsVersion attributes are included. Next, let's take a look at the functions and usage of these five attributes.

 

3.1 minSdkVersion

1> role 1: specify the minimum API Level required for running an application

This attribute is mainly used to specify the minimum API Level required for the application to run. If this parameter is not specified, the default value is 1, indicating that the application is compatible with all Android versions.

If we set the minimum API Level required for running an application and the system API Level is lower than the minSdkVersion we set, the Android system rejects the installation of the application.

The following is an example:

Device information:

Brand Red mi Note3 dual Netcom
Android system version Android 5.0.2
API Level 21

 

 

 

Clone the sample code in github and use Android Studio to modify the build. the minSdkVersion value in gradle is greater than 21, for example 22. then, when you click Run, the compiled installation package cannot be installed on the device, and a similar prompt will appear.

 

2> function 2: Lint performs static code check on the project. When the API used in the project is higher than minSdkVersion, a warning is given, avoid application bugs caused by calling APIs that do not exist in earlier versions.

The following is an example.

For example, if we set minSdkVersion to 1, we call ActionBar in the project (ActionBar is a new API provided by API Level 11 ), IDE will use Lint for detection and prompt that we have called an API whose Level is 11.

 

3.2 targetSdkVersion

This attribute is used to specify the target API Level of the application and is also an integer. If this parameter is not set, the default value of targetSdkVersion is the same as that of minSdkVersion.

This attribute is used to notify the system that the application has tested the Android version of this API Level. The system no longer needs to use the compatibility mode to make the application compatible with this target version. Of course, applications can still run normally on systems lower than targetSdkVersion.

If the platform's API Level is higher than the value specified by the targetSdkVersion attribute in your application, the system will enable compatibility to ensure that your application continues to run as expected.

According to the targetSdkVersion value we set, the system will execute many compatible behaviors. The specific compatibility behavior is discussed in Build. VERSION_CODES (portal) of the corresponding platform version.

In general, we should set the value of this attribute to the latest API level value so that we can use the new features on the new version of the system.

The following is an example:

If we set the value of targetSdkVersion to 11 or higher, when your application runs on Android3.0 or a higher system, the system will use the new default theme (Holo theme) for your application, and the screen compatibility mode will be disabled when running on a large screen device ), because API level 11 is supported, the large screen is supported.

 

3.3 maxSdkVersion

This attribute is mainly used to specify the maximum API Level version that can run our application.

According to the instructions in the official Google documentation,

In Android1.5, 1.6, 2.0, and 2.0.1, the system checks this value when installing an application or system upgrade. There are two situations:

1> If the maxSdkVersion value set by the application is lower than the API Level used by the system, the system will not allow the application to be installed.

2> after the system is upgraded, the new system will re-verify this value. If the API Level of the new system is higher than this value, the new system will delete your application.

 

On a system higher than 2.0.1, The maxSdkVersion value set in the application is not verified during application installation, and the value is not verified again after the system upgrade. There are two situations:

1> in Google Play, this attribute will continue to be used for filtering, and the application will not be displayed in the list of installable applications of systems whose API Level is higher than maxSdkVersion.

2> even if maxSdkVersion is set to 9 (Android 2.3) for our app, we can still install the app on Android 4.2 devices.

Attention

According to the instructions in the official documentation, this attribute is no longer recommended.

3.4 compileSdkVersion

This is not the attribute of the <uses-sdk> tag, but the attribute that must be declared and assigned a value in the Android Gradle project created by Alibaba.

CompileSdkVersion is used to specify the Android SDK for compiling the API Level of our application. If the newly added API is used in our application, you must use the Android SDK corresponding to the newly added API.

CompileSdkVersion does not change the running behavior of the application. When we modify compileSdkVersion, Android Studio will display some compilation warnings and compilation error prompts. We should pay attention to and make corresponding corrections.

According to the instructions in the official documentation, we strongly recommend that you use the latest SDK for compilation to prepare for using new features.

Attention

In ecplise projects, compileSdkVersion corresponds to the target attribute in projects. properties.

For example, if target = android-19 is set in projects. properties, the jar package android. jar in the android-19 directory under the platforms directory in the sdk is used to compile the project.

 

3.5 buildToolsVersion

This is the same as compileSdkVersion. It is not the attribute of the <uses-sdk> label, but the attribute that must be declared and assigned a value in the Android Gradle project created by Alibaba Cloud in build. gradle.

BuildToolsVersion is used to specify the tool versions used for application building, such as aapt and dx that we are familiar. These tools are generally located in the following directory:

${ANDROID_SDK_HOME}/build-tools/${buildToolsVersion}

 

$ {ANDROID_SDK_HOME} indicates the path of our SDK, and $ {buildToolsVersion} indicates the value of buildToolsVersion we configured.

Note that buildToolsVersion has no mandatory requirements. We can use a new build tool to build our low-version applications, you can also use the old build tool to build our high-version applications.

The following is an example:

1> compileSdkVersion is 19, and buildToolsVersion is 21.1.2, which means to build an application of a lower version using a later build tool.

2> compileSdkVersion is 21, and buildToolsVersion is 19.1.0, which means to build a later version of the application using a later version build tool.

Attention

In ecplise projects, buildToolsVersion corresponds to the sdk. buildtools attribute in projects. properties.

For example, you can set sdk. buildtools = 19.1.0 in project. properties. You can also choose not to set the latest version.

 

4. Summary

After learning about the functions of each attribute, we will find that the relationships between minSdkVersion, targetSdkVersion, and compileSdkVersion are as follows:

minSdkVersion <= targetSdkVersion <= compileSdkVersion

 

Ideally, the relationships between minSdkVersion, targetSdkVersion, and compileSdkVersion are as follows:

MinSdkVersion (the lower the better) <= targetSdkVersion = compileSdkVersion (latest API Level)

The advantage is that

1> use a lower minSdkVersion to cover the largest audience

2> set the target and compile with the latest SDK to get the best appearance and behavior, and make better use of the new features.

 

Related Article

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.