What is the right approach to "This AsyncTask class should be static or leaks might occur" in Kotlin Android? : <p>There are many questions here dealing with <code>This class should be static or leaks might occur</code> in java android.</p>

<p><a href="https://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler">This Handler class should be static or leaks might occur: IncomingHandler</a></p>

<p><a href="https://stackoverflow.com/questions/37188519/this-handler-class-should-be-static-or-leaks-might-occurasyncqueryhandler">This Handler class should be static or leaks might occur:AsyncQueryHandler</a></p>

<p><a href="https://stackoverflow.com/questions/44309241/this-asynctask-class-should-be-static-or-leaks-might-occur-anonymous-android-os">This AsyncTask class should be static or leaks might occur (anonymous android.os.AsyncTask)</a></p>

<p>The warning is due to the fact that inner class holds an implicit reference to the outer class, and hence preventing the outer class from GC'd.
The solution lies in the warning itself that the class should be declared static.</p>

<p>However, the solution is java specific. Given that kotlin has no <code>static</code> modifier, the closest thing is <a href="https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects" rel="noreferrer">companion object</a> and companion object does hold reference to it's "outer class". </p>

<p>Below are my [failed] attempts with remarks</p>

<pre><code>class MyActivity : AppCompatActivity(), MyListener {

    companion object {
        class Attempt3Task(val callback: MyListener) : AsyncTask&lt;Unit, Unit, Unit&gt;() {
            override fun doInBackground(vararg params: Unit?) {
                TODO("")
            }

            override fun onPostExecute(result: Unit?) {
                callback.updateUi()
            }
        }
    }

    inner class Attempt2Task : AsyncTask&lt;Unit, Unit, Unit&gt; () {
        override fun doInBackground(vararg params: Unit?) {
            TODO("
        }
    }

    // Gives warning "This AsyncTask class should be static or leaks might occur"
    val attempt_1 = object: AsyncTask&lt;Unit, Unit, Unit&gt;() {
        override fun doInBackground(vararg params: Unit?) {
            TODO("")
        }
    }

    // Does not give warning but, as far as I can tell, is conceptually same as attempt_1
    val attempt_2 = Attempt2Task()

   // Does not give warning but companion object does have reference to the activity, no?
    val attempt_3 = Attempt3Task(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}
</code></pre>

<p>Are the assertion about attempt2 and attempt3 correct that even without the linter warning, the code is still leaking?</p>

<p>What options do we have to avoid the leaking? 
Should I resolve to plain old top-level <code>class MyTask : AsyncTask&lt;Unit, Unit, Unit&gt; ()</code> with a member of <code>WeakReference</code> to callback?</p>
