(#) Wrong Thread !!! ERROR: Wrong Thread This is an error. Id : `WrongThread` Summary : Wrong Thread 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 See : https://developer.android.com/guide/components/processes-and-threads.html#Threads 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/ThreadDetector.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/ThreadDetectorTest.kt) Ensures that a method which expects to be called on a specific thread, is actually called from that thread. For example, calls on methods in widgets should always be made on the UI thread. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/ThreadTest.java:15:Error: Method onPreExecute must be called from the main thread, currently inferred thread is worker thread [WrongThread] onPreExecute(); // ERROR -------------- src/test/pkg/ThreadTest.java:16:Error: Method paint must be called from the UI thread, currently inferred thread is worker thread [WrongThread] view.paint(); // ERROR ------------ src/test/pkg/ThreadTest.java:22:Error: Method publishProgress must be called from the worker thread, currently inferred thread is main thread [WrongThread] publishProgress(); // ERROR ----------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/ThreadTest.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import androidx.annotation.MainThread; import androidx.annotation.UiThread; import androidx.annotation.WorkerThread; public class ThreadTest { public static AsyncTask testTask() { return new AsyncTask() { final CustomView view = new CustomView(); @Override protected void doInBackground(Object... params) { onPreExecute(); // ERROR view.paint(); // ERROR publishProgress(); // OK } @Override protected void onPreExecute() { publishProgress(); // ERROR onProgressUpdate(); // OK // Suppressed via older Android Studio inspection id: //noinspection ResourceType publishProgress(); // SUPPRESSED // Suppressed via new lint id: //noinspection WrongThread publishProgress(); // SUPPRESSED // Suppressed via Studio inspection id: //noinspection AndroidLintWrongThread publishProgress(); // SUPPRESSED } }; } @UiThread public static class View { public void paint() { } } public static class CustomView extends View { @Override public void paint() { } } public abstract static class AsyncTask { @WorkerThread protected abstract void doInBackground(Object... params); @MainThread protected void onPreExecute() { } @MainThread protected void onProgressUpdate(Object... values) { } @WorkerThread protected final void publishProgress(Object... values) { } } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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/ThreadDetectorTest.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, `ThreadDetector.testThreading`. 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("WrongThread") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("WrongThread") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection WrongThread 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="WrongThread" 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 'WrongThread' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore WrongThread ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).