Kotlin + Dagger2 appears Dagger does not support injection into private fields solution

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:
insert image description here
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 Ktin 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 presenteris 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.

Guess you like

Origin blog.csdn.net/u013762572/article/details/124656241