简体   繁体   中英

what is the propose of targetSdkVersion in android?

I have 2 questions about targetSdkVersion:

1-what is the propose of targetSdkVersion in android?

2-what happen if i remove it from gradle settings?

android:targetSdkVersion

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.

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

Relationship between minSDK - targetSdk - compileSdk

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

What if you don't provide targetSdkVersion ?

Your minSdkVersion & compileSdkVersion is < API 23 (6.0 - MarshMallow - Have RunTime Permission) & you haven't added targetSdkVersion .

Your application will get all the permission you have provided in your manifest by default as you haven't compiled with API 23 or provided targetSdkVersion.

In those cases defaults to the minSdkVersion . If you haven't provided minSdkVersion then it defaults to API 1.

What is the propose of targetSdkVersion in Android?

The purpose is to tell the Gradle what version of the Android API you have tested against. It also tells the runtime platform to enable backwards compatibility features to be compatible with the target version. Thus, if you run on a newer platform, the runtime will try to behave like your (older) target version.

What happens if I remove it from the Gradle settings?

If you leave out the targetSdkVersion , it defaults to the minSdkVersion . (And if you leave that out, it defaults to 1!)

For more information (including some examples of the effects of setting targetSdkVersion ) read

So for example if we target an app with API 14, will Android handle runtime permissions introduced in higher APIs?

Typically, yes 1 . If a permission is introduced in a later level than your target level, the compatibility code will enable the permission for the app.

For example, see https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#DONUT


1 - Check the documentation. I don't know if there are exceptions to this. If there were, they would break compatibility... for apps that depend on the functionality covered by the new permissions.

targetSdkVersion — Specifies the API Level on which the application is designed to run. In some cases, this allows the application to use manifest elements or behaviors defined in the target API Level, rather than being restricted to using only those defined for the minimum API Level.

If you don't set targetSdkVersion , the default value equals that given to minSdkVersion .

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.

See official doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM