(#) Attribute unused on older versions
!!! WARNING: Attribute unused on older versions
This is a warning.
Id
: `UnusedAttribute`
Summary
: Attribute unused on older versions
Severity
: Warning
Category
: Correctness
Platform
: Android
Vendor
: Android Open Source Project
Feedback
: https://issuetracker.google.com/issues/new?component=192708
Since
: Initial
Affects
: Resource files and resource folders
Editing
: This check runs on the fly in the IDE editor
Implementation
: [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ApiDetector.kt)
Tests
: [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ApiDetectorTest.java)
Copyright Year
: 2012
This check finds attributes set in XML files that were introduced in a
version newer than the oldest version targeted by your application (with
the `minSdkVersion` attribute).
This is not an error; the application will simply ignore the attribute.
However, if the attribute is important to the appearance or
functionality of your application, you should consider finding an
alternative way to achieve the same result with only available
attributes, and then you can optionally create a copy of the layout in a
layout-vNN folder which will be used on API NN or higher where you can
take advantage of the newer attribute.
Note: This check does not only apply to attributes. For example, some
tags can be unused too, such as the new `` element in layouts
introduced in API 21.
!!! Tip
This lint check has an associated quickfix available in the IDE.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
res/layout/linear.xml:11:Warning: Attribute android:theme is only used
by tags in API level 23 and higher (current min is 21)
[UnusedAttribute]
android:theme="@android:style/Theme.Holo" />
-----------------------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here are the relevant source files:
`src/AndroidManifest.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="30" />
</manifest>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`res/layout/linear.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/included"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@android:style/Theme.Holo" />
</LinearLayout>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ApiDetectorTest.java)
for the unit tests for this check to see additional scenarios.
(##) Suppressing
You can suppress false positives using one of the following mechanisms:
* Adding the suppression attribute `tools:ignore="UnusedAttribute"` on
the problematic XML element (or one of its enclosing elements). You
may also need to add the following namespace declaration on the root
element in the XML file if it's not already there:
`xmlns:tools="http://schemas.android.com/tools"`.
* Using a special `lint.xml` file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="UnusedAttribute" severity="ignore" />
</lint>
```
Instead of `ignore` you can also change the severity here, for
example from `error` to `warning`. You can find additional
documentation on how to filter issues by path, regular expression and
so on
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).
* In Gradle projects, using the DSL syntax to configure lint. For
example, you can use something like
```gradle
lintOptions {
disable 'UnusedAttribute'
}
```
In Android projects this should be nested inside an `android { }`
block.
* For manual invocations of `lint`, using the `--ignore` flag:
```
$ lint --ignore UnusedAttribute ...`
```
* Last, but not least, using baselines, as discussed
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).