Using KT + Dagger2 in the project, when injecting, the code is as follows:
class MainActivity : AppCompatActivity() {
@Inject
var presenter : Presenter? = null
When the code is compiled, an error is reported directly:
In fact, it has been said very clearly that Dagger does not support the injection of private attributes, so this is more embarrassing, because the decompiled code Kt
in is as follows:presenter
public final class MainActivity extends androidx.appcompat.app.AppCompatActivity {
@org.jetbrains.annotations.Nullable()
@javax.inject.Inject()
private com.xing.jetpacklearn.Presenter presenter;
public MainActivity() {
super();
}
The attribute at this time presenter
is private
, which does not meet the requirements of Dagger. How should we deal with it? Two methods:
Method 1:
The code is as follows:
class MainActivity : AppCompatActivity() {
var presenter : Presenter? = null
@Inject set
}
Just add it to the set method of the attribute @Inject
.
Method 2: Use `@JvmField` keyword modification:
class MainActivity : AppCompatActivity() {
@Inject
@JvmField
var presenter : Presenter? = null
}
can be resolved.