Search code examples
androidandroid-studiobuild.gradleandroid-manifest

what is the difference between tools:targetApi in the Manifest and target sdk in build.gradle?


I developed an app in Android and I want to limit the Android versions allowed to use it. So, in the build.gradle file, I put:

android {
    namespace 'wamboo.example.videocompressor'
    compileSdk 33

    defaultConfig {
        applicationId "wamboo.example.videocompressor"
        minSdk 30
        targetSdk 33
        versionCode 2
        versionName "2.0"

but I also saw this in the AndroidManifest file:

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.VideoCompressor"
    tools:targetApi="31">

I was wondering if I need to change that tools:targetApi="31"> to tools:targetApi="33">?


Solution

  • tools:targetApi="x" is used by lint to specify which API level is supported by this element (in your case application)

    tools:targetApi documentation

    This tells the tools that you believe this element and any children are used only on the specified API level or higher. This stops lint from warning you if that element or its attributes are not available on the API level you specify as your minSdkVersion.

    In your case, application has dataExtractionRules attribute, which was aded in API 31. Since your minSdk is 30, without tools:targetApi you will see a lint warning.

    Answering your question, it's fine to leave it as 31, since it's the minimum version required by all attributes in the application element. Though it wouldn't harm to set it to 33 as well because it's only used by lint and won't affect the app in runtime.