(#) Querying resource properties using LocalContext.current !!! ERROR: Querying resource properties using LocalContext.current This is an error. Id : `LocalContextGetResourceValueCall` Summary : Querying resource properties using LocalContext.current Severity : Error Category : Correctness Platform : Any Vendor : Jetpack Compose Identifier : androidx.compose.ui Feedback : https://issuetracker.google.com/issues/new?component=612128 Min : Lint 8.7+ Compiled : Lint 8.7+ Artifact : [androidx.compose.ui:ui-android](androidx_compose_ui_ui-android.md.html) Since : 1.10.0 Affects : Kotlin and Java files and test sources Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/ui/ui-lint/src/main/java/androidx/compose/ui/lint/LocalContextResourcesConfigurationReadDetector.kt) Tests : [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/ui/ui-lint/src/test/java/androidx/compose/ui/lint/LocalContextResourcesConfigurationReadDetectorTest.kt) Copyright Year : 2024 Changes to the Configuration object will not cause LocalContext.current reads to be invalidated, so calls to APIs such as Context.getString() will not be updated when the Configuration changes, and so stale values might be used. Instead, you can use Compose APIs such as stringResource, colorResource, and painterResource - or LocalResources.current and query properties from Resources directly. Using these APIs will invalidate callers when the Configuration changes, to ensure that these calls reflect the latest values. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/test.kt:13:Error: Querying resource values using LocalContext.current [LocalContextGetResourceValueCall] LocalContext.current.getText(-1) -------------------------------- src/test/test.kt:14:Error: Querying resource values using LocalContext.current [LocalContextGetResourceValueCall] LocalContext.current.getString(-1) ---------------------------------- src/test/test.kt:15:Error: Querying resource values using LocalContext.current [LocalContextGetResourceValueCall] LocalContext.current.getString(-1, Any()) ----------------------------------------- src/test/test.kt:16:Error: Querying resource values using LocalContext.current [LocalContextGetResourceValueCall] LocalContext.current.getColor(-1) --------------------------------- src/test/test.kt:17:Error: Querying resource values using LocalContext.current [LocalContextGetResourceValueCall] LocalContext.current.getDrawable(-1) ------------------------------------ src/test/test.kt:18:Error: Querying resource values using LocalContext.current [LocalContextGetResourceValueCall] LocalContext.current.getColorStateList(-1) ------------------------------------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/test.kt`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers package test import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalContext @Composable fun Test() { LocalContext.current.resources LocalContext.current.getResources() LocalContext.current.resources.configuration LocalContext.current.getResources().getConfiguration() LocalContext.current.getText(-1) LocalContext.current.getString(-1) LocalContext.current.getString(-1, Any()) LocalContext.current.getColor(-1) LocalContext.current.getDrawable(-1) LocalContext.current.getColorStateList(-1) } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/ui/ui-lint/src/test/java/androidx/compose/ui/lint/LocalContextResourcesConfigurationReadDetectorTest.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, `LocalContextResourcesConfigurationReadDetector.error`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=612128. (##) Including !!! This is not a built-in check. To include it, add the below dependency to your project. ``` // build.gradle.kts implementation("androidx.compose.ui:ui-android:1.11.0-alpha06") // build.gradle implementation 'androidx.compose.ui:ui-android:1.11.0-alpha06' // build.gradle.kts with version catalogs: implementation(libs.ui.android) # libs.versions.toml [versions] ui-android = "1.11.0-alpha06" [libraries] # For clarity and text wrapping purposes the following declaration is # shown split up across lines, but in TOML it needs to be on a single # line (see https://github.com/toml-lang/toml/issues/516) so adjust # when pasting into libs.versions.toml: ui-android = { module = "androidx.compose.ui:ui-android", version.ref = "ui-android" } ``` 1.11.0-alpha06 is the version this documentation was generated from; there may be newer versions available. NOTE: These lint checks are **also** made available separate from the main library. You can also use `androidx.compose.ui:ui-lint:1.11.0-alpha06`. [Additional details about androidx.compose.ui:ui-android](androidx_compose_ui_ui-android.md.html). (##) 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("LocalContextGetResourceValueCall") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("LocalContextGetResourceValueCall") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection LocalContextGetResourceValueCall 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="LocalContextGetResourceValueCall" 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 'LocalContextGetResourceValueCall' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore LocalContextGetResourceValueCall ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).