What's the usage of tools:targetApi="m"?

14,382

Solution 1

From the docs you can read:

Indicates that Lint should treat this type as targeting a given API level, no matter what the project target is

This means it will affect only the annotated one.

Other attributes with tools won't be affected. tools is a namespace, from which you can get attributes, an attribute won't affect the entire namespace.

Solution 2

By adding tools:targetApi="m" to an element you tell the lint that the element won't be used on API level below 23 (M). See the attribute documentation.

This tells the tools that you believe this element (and any children) will be 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 this particular case <application> uses android:usesCleartextTraffic attribute which is available starting from API 23 but the app minSdkVersion is less then 23 so lint warns you. Despite specifying tools:targetApi removes the warning in this case it isn't a right solution because the <application> can be used on older API levels if minSdkVersion allows it. But such a trick won't harm because android:usesCleartextTraffic will be ignored if it isn't supported, see this answer for more details.

What about tools namespace in general, it contains attributes which used by build tools and won't affect runtime behavior. See the docs for more details.

Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.

Share:
14,382

Related videos on Youtube

Mahdi Moqadasi
Author by

Mahdi Moqadasi

Improving everyday

Updated on June 20, 2022

Comments

  • Mahdi Moqadasi
    Mahdi Moqadasi about 2 years

    I have an app uses clearText between Android-client and server using Retrofit, and in Android 9+ it's not allowed to use clearText.

    To ignore that I added android:usesCleartextTraffic="true" in Manifest but it warns: tools:ignore="GoogleAppIndexingWarning" and suggests to add tools:targetApi="m".

    It's a bit confusing:

    • Is the tools:targetApi="m" means that any attributes with tools: is for Marshmallow and higher?

    • Is it for using this version of Manifest or something else? Is this making unwanted mistake in my app?

    My Manifest:

    ...
    <application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:fullBackupContent="false"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="false"
        android:theme="@style/AppTheme.NoActionBar"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning"
        tools:targetApi="m">
        ...
    
  • Mahdi Moqadasi
    Mahdi Moqadasi about 5 years
    I updated my question. see my main tag... how tools:targetApi can detect which attribute must affected in api m?
  • Luca Nicoletti
    Luca Nicoletti about 5 years
    The tools:targetApi only affects Lint
  • Kirill Karmazin
    Kirill Karmazin over 4 years
    So it basically just removes the annoying red wavy underline. That's it.
  • Pankaj
    Pankaj about 4 years
    If I use it in Styles.xml, tools:targetApi="p" and use that style in OS below "P" will it crash the app ? or it will ignore the line attribute for below "P" ?
  • Luca Nicoletti
    Luca Nicoletti about 4 years
    @Pankaj just declare a new styles.xml file which targets that API and use what you need there. Don't try to mess up with the framework.
  • Pankaj
    Pankaj about 4 years
    @LucaNicoletti Yes, but according to documentation, it is completely safe to use it in default styles.xml, because system will ignore it on lower versions
  • Bitwise DEVS
    Bitwise DEVS about 2 years
    So should we add tools:targetApi="m" when specifying clear text in Manifest of a project that has min SDK of 21 or not?