24

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

Anonsage
  • 8,030
  • 5
  • 48
  • 51

2 Answers2

8

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.
Community
  • 1
  • 1
Marcelo Benites
  • 833
  • 8
  • 18
  • 5
    What about lower-priority manifest having higher targetSdkVersion than higher priority manifest (main)? – Ayyappa Aug 10 '21 at 20:58
  • As of today, the whole app (main, sdk 30) was promoted to the higher targetSdkVersion (library, sdk 31), introducing new app permission we aren't handling and causing crashes where the permissions are required – Yusef Maali Apr 12 '22 at 08:22
  • @YusefMaali how have you come with this conclusion? I checked my app and see that higher library's targetSDK doesn't change the targetSDK of the final app even the app's targetSDK is lower. Checked in the merged manifest file and in runtime. – xoxol_89 Aug 08 '22 at 11:28
  • 2
    Linking to the document where this information was culled from would be rather helpful. – Shayne3000 Nov 09 '22 at 17:26
1

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

The Original Android
  • 6,147
  • 3
  • 26
  • 31