Dagger 1不会将Java类现场注入Kotlin活动

时间:2018-10-25 14:26:12

标签: java android kotlin dagger

我有一个使用Dagger 1并用Java编写的大型项目。 我现在在Kotlin中添加了新的活动,但无法在Kotlin活动中注入Java类。

这是我的模块

import dagger.Module;
import dagger.Provides;

@Module(
        injects = {

                HomeActivity.class
        },
        complete = false,
        library = true
)
public class AppModule {

    @Provides
    @ForApplication
    @Singleton
    public SessionStorage provideSessionStorage(@ForApplication YakApplication app) {
        return new SessionStorage(app);
    }
}

这是SessionStorage:

public class SessionStorage {
private SharedPreferences preferences;
public SessionStorage(Context appContext){
    preferences=appContext.getSharedPreferences(appContext.getPackageName(),Context.MODE_PRIVATE);
}
}

这是HomeActivity:

class HomeActivity : AppCompatActivity(), NavigationBarItemListener {

    override fun onSelectedNavigationBarItem(tag: String) {
        message.text = tag
    }

    @Inject
    @ForApplication
    @JvmField
    var sessionStorage : SessionStorage? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        BaseApplication.get(this).inject(this)
        bottomBarNavigation.navigationBarItemListener = this
        bottomBarNavigation.navigationBaRContainer.changeBackgroundColor(Color.BLACK)
    }

    override fun onStart() {
        super.onStart()
        bottomBarNavigation.update(sessionStorage?.favoriteTeamId)
    }
}

这是错误:

 2018-10-26 10:30:47.485 29873-29873/com.myproject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myproject, PID: 29873
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.home.HomeActivity}: java.lang.IllegalStateException: Errors creating object graph:
      com.myproject.SessionStorage has no injectable members. Do you want to add an injectable constructor? required by class com.myproject.home.HomeActivity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.IllegalStateException: Errors creating object graph:
      com.myproject.SessionStorage has no injectable members. Do you want to add an injectable constructor? required by class com.myproject.home.HomeActivity
        at dagger.internal.ThrowingErrorHandler.handleErrors(ThrowingErrorHandler.java:34)
        at dagger.internal.Linker.linkRequested(Linker.java:182)
        at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:309)
        at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279)
        at com.myproject.BaseApplication.inject(BaseApplication.java:239)
        at com.myproject.home.HomeActivity.onCreate(HomeActivity.kt:34)
        at android.app.Activity.performCreate(Activity.java:6679)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        ... 9 more

我不明白为什么不应该注入SessionStorage。 到目前为止,我发现的唯一方法是将HomeActivity扩展为JavaActivity并在其中注入SessionStorage。但是我认为这不是最好的解决方案

2 个答案:

答案 0 :(得分:1)

您是否将这两行添加到了Gradle插件中?

apply plugin: 'kotlin-kapt'
....
kapt 'com.google.dagger:dagger-compiler:2.5'

我猜你忘了将Kotlin注释处理器添加到gradle中

编辑

也请检查此链接。 Dagger - class has no injectable members

答案 1 :(得分:0)

我终于设法解决了这个问题。我正在添加解决方案,也许其他人也会遇到同样的问题。

成绩文件

apply plugin: 'kotlin-kapt'
android{
  kapt {
        generateStubs = true
    }
}
dependencies {
    //Dagger
    kapt 'com.squareup.dagger:dagger-compiler:1.2.2'
}

这就是我注入java类的方式:

 @Inject
 @field:ForApplication
 lateinit var sessionStorage: SessionStorage

显然,需要指定的注释具有字段。 我发现此链接很有帮助https://github.com/google/dagger/issues/900