compileSdkVersion, minSdkVersion and targetSdkVersion, innocently could not tell

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/gaolh89/article/details/79809034

In the app / build.gradle Android Studio project, we can see a piece of code like this:

    compileSdkVersion 27
    defaultConfig {
        applicationId "com.glh.fabdemo"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

There compileSdkVersion, minSdkVersion, targetSdkVersion special these three attributes, one by one will be explained herein:

一.compileSdkVersion#

compileSdkVersion: SDK compiled version

compileSdkVersion Gradle tell which version of Android SDK use to compile your application . Any new API will need to be added using the corresponding Level SDK of Android .

It is emphasized that: compileSdkVersion modify the behavior does not change at runtime. When you change the compileSdkVersion time, there may be a new compiler warnings, the compile error, but the new compileSdkVersion will not be included in the APK: just use it purely at compile time. (You really should fix these warnings, their presence must be a reason)

Therefore, we strongly recommend always using the latest SDK to compile. Use existing code in the new compiler checks can get a lot of benefits to avoid new deprecated API, and is ready to use the new API.

Note that if you use the Support Library, then use the latest release of the Support Library you need to use the latest SDK to compile. For example, to use the 23.1.1 version of the Support Library, compileSdkVersion it must be at least 23 (the major version number to be consistent!). In general, the new version of the Support Library With the new system version is published, it is a new system and new features added API to provide compatibility support.

Of course, it should be noted that, in different parts of Support Library Level, although the same method call, but the source may change.

For example, I recorded FloatingActionButton basic step on the pit and use a paper mentions, Level 24 was promoted by the post 25, recyclerView and floatingActionButton scroll, hide, since CoordinatorLayout source code changes, calling hide () will not be displayed appear after hiding .

In addition, there is a buildToolsVersion. It is used to specify the project build tool version, such as 27.0.2. If a newer version, Android Studio will prompt.

二.minSdkVersion#

The minimum SDK version.

If compileSdkVersion latest API set is available, the application can minSdkVersion is the minimum requirement to run . minSdkVersion major Android application store is one of the symbols used to determine whether the user equipment can install an application.

In developing minSdkVersion also play an important role: lint default will run in the project, it will warn you when you use API lower than minSdkVersion, to help you avoid calling the API running when the problem does not exist. If used only some of the API at a higher version of the system, the system checks the version commonly used way to solve runtime.

Remember, you are using the library, such as Support Library or Google Play services, may have their own minSdkVersion. minSdkVersion set your application must be greater than equal to minSdkVersion these libraries. For example there are three libraries, they are minSdkVersion 4, 7 and 9, then your minSdkVersion must be at least 9 in order to use them. In a few cases, you still want to use a higher than minSdkVersion your application library (handles all the edge cases, to ensure that it is only used on the newer platform), you can use the tools: overrideLibrary mark, but please do a thorough the test!

三.targetSdkVersion#

The version number three of the most interesting is the targetSdkVersion. Android targetSdkVersion is to provide forward compatibility is the main basis, prior to the application of targetSdkVersion not updated system will not change in the latest application behavior. This allows you to adapt can use the new API changes before the new behavior (because you have updated compileSdkVersion is not it?).

TargetSdkVersion specified value that you have done a full test on the target version, the system will enable some of the latest capabilities and features to your application. For example, Android 6.0 system privileges cited this function is operating, if you will targetSdkVersion specified as 23 or more, then the system will start running when permissions for your application. If you targetSdkVersion designated as 22, then it shows up your program only had fully tested on Android 5.1 system, Android6.0 system, new features introduced does not get started.

Said plainly: for example, you will targetSdkVersion to 22, involving a (or several) permissions, you can configure permissions directly mainfest, and then logic processing can be performed after obtaining permission in the java code.
But if you will targetSdkVersion set to 23 or higher, you configure permissions in mainfest in addition to, but also need to determine whether the user's consent rights in java code, if you agree, do what business operations; if you do not agree, do what business operations ( usually prompted denied certain rights, certain functions can not be used properly). If you only configure the permissions mainfest in, targetSdkVersion and greater than or equal 23, Java code without permission when the code is running, your application will direct the error.

After reading the above, your idea is not: If so, then I set a little lower targetSdkVersion no more insurance.

But as a man of conscience and the pursuit of the developer, it is not recommended.

When running Android6.0 get permission to introduce, in Android6.0 Previously, users must agree to all of the rights to install the app. But in fact, a lot of authority in fact unreasonable. To QQ 7.5.5.3460 version, for example, where its rights section Screenshot:

Write pictures described here

I use QQ process, absolutely does not need to create / modify call records this privilege, even thinking "QQ team to me this authority why? I'll chat, and phone records how" about it?

After references 6.0 runtime permissions, users do not need permission to request a one-time authorization of all in the installation of the software, but you can re-apply for permission to authorize an item in the software's use. Or take QQ, for example, even when I need to create a function using a / New call logs permission, I was refused ===> This is the result brings a function or several functions can not be used, but overall still can use QQ happy to chat.

So the target is to update to the latest SDK things all applications should be given priority treatment.

But that does not mean you have to use all the features of the newly introduced, does not mean you can not do any testing blindly update targetSdkVersion, be sure to do the test before updating targetSdkVersion! Especially domestic mobile phone manufacturers a variety of customization. And in the development process, there is a possibility, product manager at runtime permissions require you to have such a demand here: refusing a user permissions, you can skip directly modify the permissions interface (ie setting / management rights here), or when the user first refusal, the second operation this button user interface or prompt permission again.

As the diversity of domestic mobile phone, this one simply needs enough you choke. Of course, have a lot of open source projects on GitHub, each version was compatible with both android and well maintained.

In short: About targetSdkVersion setting value, we need to hold their own degrees - both for users to consider improve their abilities, but also to avoid the pit itself.

# IV. On the whole #

If you're careful, you will find that the relationship between these three values ​​are:

minSdkVersion<=targetSdkVersion<=compileSdkVersion

This intuition is reasonable, if you compileSdkVersion maximum, minSdkVersion is the minimum value, then the maximum and minimum values ​​must be at least as large and targetSdkVersion must be between the two.

Ideally, in a stable relationship between the three state should be more like this:

minSdkVersion (lowest possible) <= targetSdkVersion == compileSdkVersion (latest SDK)

With a lower minSdkVersion to cover the largest crowd, set targetSdkVersion and compileVersion with the latest SDK to get the best appearance and behavior.

# Five attached:. Android SDK version #

Write pictures described here

This is the common version of Android released SDK.

Further, when judged in the code, often using the following code:

Build.VERSION.SDK_INT>=Build.VERSION_CODES.M

If there is a difference, if judged by the different versions.

Guess you like

Origin blog.csdn.net/gaolh89/article/details/79809034