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).
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.
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!
compileSdkVersion is the version of the compiler used in building the app, while targetSdkVersion is the "API level that the application targets".
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:
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With