29

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
  • 2,029
  • 4
  • 26
  • 52

2 Answers2

24

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.

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • 2
    I updated my question. see my main tag... how `tools:targetApi` can detect which attribute must affected in api m? – Mahdi Moqadasi Apr 14 '19 at 11:06
  • 3
    The `tools:targetApi` only affects `Lint` – Luca Nicoletti Apr 14 '19 at 11:10
  • 4
    So it basically just removes the annoying red wavy underline. That's it. – Kirill Karmazin Oct 18 '19 at 10:28
  • 2
    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" ? – Pankaj May 12 '20 at 16:02
  • 1
    @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. – Luca Nicoletti May 13 '20 at 17:57
  • 3
    @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 – Pankaj May 15 '20 at 08:27
16

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.

Valeriy Katkov
  • 33,616
  • 20
  • 100
  • 123
  • 2
    So should we add `tools:targetApi="m"` when specifying clear text in Manifest of a project that has min SDK of 21 or not? – Bitwise DEVS Jun 13 '22 at 13:01