Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Android libraries define targetSdkVersion?

I understand why it is useful for a library to define the minSdkVersion, but does a library having the defined targetSdkVersion add any value?

I'm not sure if there would be any issues with libraries having lower and higher targetSdkVersions than the main project. Would they just be ignored if the main project defined it? Or, take the lower/higher one? Android apps can work differently at different targetSdkVersion (for example, Marshmallow runtime permissions).

like image 627
Anonsage Avatar asked Feb 17 '16 19:02

Anonsage


People also ask

What is minSdkVersion and targetSdkVersion in Android?

android:minSdkVersion — Specifies the minimum API Level on which the application is able to run. The default value is "1". android:targetSdkVersion — Specifies the API Level on which the application is designed to run.

What should compileSdkVersion be?

Ideally, the compileSdkVersion and targetSdkVersion should be equal and both point to the latest SDK. But of course only after you test that every change introduced in that version works smoothly with your app!

What is the difference between compile SDK version and target SDK version?

compileSdkVersion is the version of the compiler used in building the app, while targetSdkVersion is the "API level that the application targets".


2 Answers

Documentation regarding targetSDKVersion:

An integer designating the API Level that the application targets. If not set, the default value equals that given to minSdkVersion. 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. The application is still able to run on older versions (down to minSdkVersion).

An Android Library targetSdkVersion will tell the host application how your library is supposed to behave regarding the matter. The manifest merger has specific rules about merging targetSdkVersion:

enter image description here

Attributes in the element always use the value from the higher-priority manifest, except in the following situations:

  • When the lower-priority manifest has a minSdkVersion value that's higher, an error occurs unless you apply the overrideLibrary merge rule.
  • When the lower-priority manifest has a targetSdkVersion value that's lower, the merger tool uses the value from the higher-priority manifest, but it also adds any system permissions that are necessary to ensures that the imported library continues to function properly (for cases in which the higher Android version has increased permission restrictions). For more information about this behavior, see the section about implicit system permissions.
like image 133
Marcelo Benites Avatar answered Oct 21 '22 13:10

Marcelo Benites


The Android webpage @ uses-sdk says that targetSdkVersion

Specifies the API Level on which the application is designed to run.

Regarding your question, the library specifies the targetSdkVersion to claim the Android API in which it was tested under. If the actual API on the Android device is higher, then Android provides the forward compatibility. You may search for the text "forward compatibility" on that webpage for details.

Hopefully this answers all your questions. Have fun...

like image 1
The Original Android Avatar answered Oct 21 '22 15:10

The Original Android