A Handler is created without a Looper, taking the current thread's implicitly

tx0/kotlin:handler-without-looperrecommendedwarningframeworks/androiddepth fastbug, android

What it finds

Handler() uses the looper of the thread that creates it, which crashes on a thread without one and runs callbacks on an unexpected thread otherwise. Android deprecates the constructor; Handler(Looper.getMainLooper()) says which thread is meant.

From the rule's source, handler_without_looper.rs.

Example from the rule's tests
import android.os.Handlerimport android.os.Looper fun handlers() {    val a = Handler()tx0 reports: the handler takes the looper    val b = Handler(Looper.getMainLooper())}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:handler-without-looper" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:handler-without-looper] reason="..."

tx0 explain tx0/kotlin:handler-without-looper shows the same in the terminal. Found a false positive? Report it.