java Kotlin+Dagger 2:Dagger*文件不会生成
我第一次开始同时使用Kotlin和Dagger 2。我假设一切都和Java中的一样,但显然不是这样。Dagger不会为我生成Dagger*文件。这是我的密码:
组件:
@PerActivity
@Subcomponent(modules = arrayOf(ApplicationModule::class))
interface ActivityComponent {
fun inject(app: OneAccountApplication)
}
@Singleton
@Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent {
fun inject(syncService: SyncService)
@ApplicationContext fun context(): Context
fun application(): Application
fun ribotsService(): OneAccountService
fun preferencesHelper(): PreferencesHelper
fun databaseHelper(): DatabaseHelper
fun dataManager(): DataManager
}
@ConfigPersistent
@Component(dependencies = arrayOf(ApplicationComponent::class))
interface ConfigPersistentComponent {
fun activityComponent(activityModule: ActivityModule): ActivityComponent
}
模块:
@Module
class ActivityModule(private val mActivity: Activity) {
@Provides
internal fun provideActivity() = mActivity
@Provides
@ActivityContext
internal fun providesContext() = mActivity
}
@Module
class ApplicationModule(val mApplication: Application) {
@Provides @Singleton
internal fun provideApplication() = mApplication
@Provides
@ApplicationContext
internal fun provideContext() = mApplication
@Provides
@Singleton
internal fun provideOneAccountService() = OneAccountService.Creator.newOneAccountService()
}
范围注释:
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityContext
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApplicationContext
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ConfigPersistent
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class PerActivity
这基本上就是我在java代码中成功使用的整个DI系统。但对于科特林来说,由于某种原因,它不起作用。我还添加了:apply plugin: 'kotlin-kapt'
到gradle.build
类似:
apply plugin: 'com.安卓.application'
apply plugin: 'kotlin-安卓'
apply plugin: 'kotlin-kapt'
在依赖性方面,我有:
dependencies {
final DAGGER_VERSION = '2.8'
def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
annotationProcessor daggerCompiler
testAnnotationProcessor daggerCompiler
安卓TestAnnotationProcessor daggerCompiler
compile "com.google.dagger:dagger:$DAGGER_VERSION"
}
kapt {
generateStubs = true
}
基本上,这个ithttps://github.com/ribot/安卓-boilerplate转化为Kotlin
# 1 楼答案
Kotlin使用
kapt
而不是Android插件中的annotationProcessor
您需要在
build.gradle
文件中包括并使用以下内容:更详细的说明也可以在这里找到:Dagger and Kotlin
# 2 楼答案
我用这个渐变,效果很好