Java 科特林+;匕首2:匕首*文件获胜';不产生
我第一次开始同时使用Kotlin和Dagger 2。我假设一切都和Java中的一样,但显然不是这样。Dagger不会为我生成Dagger*文件。这是我的密码: 组件:Java 科特林+;匕首2:匕首*文件获胜';不产生,java,dependency-injection,kotlin,dagger-2,Java,Dependency Injection,Kotlin,Dagger 2,我第一次开始同时使用Kotlin和Dagger 2。我假设一切都和Java中的一样,但显然不是这样。Dagger不会为我生成Dagger*文件。这是我的密码: 组件: @PerActivity @Subcomponent(modules = arrayOf(ApplicationModule::class)) interface ActivityComponent { fun inject(app: OneAccountApplication) } @Module class Acti
@PerActivity
@Subcomponent(modules = arrayOf(ApplicationModule::class))
interface ActivityComponent {
fun inject(app: OneAccountApplication)
}
@Module
class ActivityModule(private val mActivity: Activity) {
@Provides
internal fun provideActivity() = mActivity
@Provides
@ActivityContext
internal fun providesContext() = mActivity
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext
模块:
@PerActivity
@Subcomponent(modules = arrayOf(ApplicationModule::class))
interface ActivityComponent {
fun inject(app: OneAccountApplication)
}
@Module
class ActivityModule(private val mActivity: Activity) {
@Provides
internal fun provideActivity() = mActivity
@Provides
@ActivityContext
internal fun providesContext() = mActivity
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext
范围注释:
@PerActivity
@Subcomponent(modules = arrayOf(ApplicationModule::class))
interface ActivityComponent {
fun inject(app: OneAccountApplication)
}
@Module
class ActivityModule(private val mActivity: Activity) {
@Provides
internal fun provideActivity() = mActivity
@Provides
@ActivityContext
internal fun providesContext() = mActivity
}
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext
这基本上就是我在java代码中成功使用的整个DI系统。但对于科特林来说,由于某种原因,它不起作用。我还添加了:
apply plugin:'kotlin kapt'
到gradle.build
类似:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
在依赖性方面,我有:
dependencies {
final DAGGER_VERSION = '2.8'
def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
annotationProcessor daggerCompiler
testAnnotationProcessor daggerCompiler
androidTestAnnotationProcessor daggerCompiler
compile "com.google.dagger:dagger:$DAGGER_VERSION"
}
kapt {
generateStubs = true
}
基本上,它转换成了Kotlin。Kotlin使用的是
kapt
,而不是Android插件中的annotationProcessor
您需要在build.gradle
文件中包括并使用以下内容:
kapt {
generateStubs = true
}
dependencies {
// ...
kapt daggerCompiler
}
更详细的说明也可以在这里找到:您能从build.gradle文件中包括相关部分吗?你能建立你的项目吗?您是否收到错误/警告?嗯,只有几件事是相关的。我现在将包括它们。没有警告,只有关于未解析引用的消息
DaggerApplicationComponent
和DaggerConfigPersistentComponent
。对于kotlin,您需要使用kapt
,正如您在这里看到的,它表明您在构建中使用的是annotationProcessor
,而不是kapt