(#) Should pass resolved color instead of resource id !!! ERROR: Should pass resolved color instead of resource id This is an error. Id : `ResourceAsColor` Summary : Should pass resolved color instead of resource id Severity : Error Category : Correctness Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : 3.1.0 (March 2018) Affects : Kotlin and Java files 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/ResourceTypeDetector.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/ResourceTypeDetectorTest.kt) Methods that take a color in the form of an integer should be passed an RGB triple, not the actual color resource id. You must call `getResources().getColor(resource)` to resolve the actual color value first. Similarly, methods that take a dimension integer should be passed an actual dimension (call `getResources().getDimension(resource)` (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/WrongColor.java:9:Error: Should pass resolved color instead of resource id here: getResources().getColor(R.color.blue) [ResourceAsColor] paint2.setColor(R.color.blue); ------------ src/test/pkg/WrongColor.java:11:Error: Should pass resolved color instead of resource id here: getResources().getColor(R.color.red) [ResourceAsColor] textView.setTextColor(R.color.red); ----------- src/test/pkg/WrongColor.java:12:Error: Should pass resolved color instead of resource id here: getResources().getColor(android.R.color.black) [ResourceAsColor] textView.setTextColor(android.R.color.black); --------------------- src/test/pkg/WrongColor.java:13:Error: Should pass resolved color instead of resource id here: getResources().getColor(R.color.green) [ResourceAsColor] textView.setTextColor(foo > 0 ? R.color.green : R.color.blue); ------------- src/test/pkg/WrongColor.java:21:Error: Should pass resolved color instead of resource id here: getResources().getColor(R.color.blue) [ResourceAsColor] foo2(R.color.blue); ------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here are the relevant source files: `src/test/pkg/WrongColor.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import android.app.Activity; import android.graphics.Paint; import android.widget.TextView; @SuppressWarnings("ClassNameDiffersFromFileName") public class WrongColor extends Activity { public void foo(TextView textView, int foo) { Paint paint2 = new Paint(); paint2.setColor(R.color.blue); // Wrong textView.setTextColor(R.color.red); textView.setTextColor(android.R.color.black); textView.setTextColor(foo > 0 ? R.color.green : R.color.blue); // OK textView.setTextColor(getResources().getColor(R.color.red)); // OK foo1(R.color.blue); foo2(0xffff0000); // Wrong foo1(0xffff0000); foo2(R.color.blue); } private void foo1(@androidx.annotation.ColorRes int c) { } private void foo2(@androidx.annotation.ColorInt int c) { } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ `test.pkg`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text linenumbers @color/blue ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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/ResourceTypeDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `ResourceTypeDetector.testColorInt`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("ResourceAsColor") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("ResourceAsColor") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection ResourceAsColor problematicStatement() ``` * 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="ResourceAsColor" 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 'ResourceAsColor' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore ResourceAsColor ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).