Kotlin + Dagger 2:不会生成 Dagger* 文件
Kotlin + Dagger 2: Dagger* files won't generate
我第一次开始同时使用 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 系统。但是对于 Kotlin,由于某种原因它不起作用。我还添加了: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
}
基本上,这个https://github.com/ribot/android-boilerplate转换成Kotlin。
Kotlin 使用 kapt
而不是 Android 插件中的 annotationProcessor
。
您需要在 build.gradle
文件中包含并使用以下内容:
kapt {
generateStubs = true
}
dependencies {
// ...
kapt daggerCompiler
}
更详细的说明也可以在这里找到:Dagger and Kotlin
我用这个 gradle 并且工作正常
buildscript {
val kotlinVersion = "1.4.20"
repositories {
mavenCentral()
}
dependencies {
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
}
}
plugins {
kotlin("jvm") version "1.5.30"
kotlin("kapt") version "1.5.30"
application
}
repositories {
mavenCentral()
google()
}
application {
mainClass.set("dp.strategy.MiniDuckSimulator")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation ("com.google.dagger:dagger:2.39.1")
kapt ("com.google.dagger:dagger-compiler:2.39.1")
}
kapt {
mapDiagnosticLocations = true // include the Kotlin files into error reports
}
kapt {
javacOptions {
// Increase the max count of errors from annotation processors.
// Default is 100.
option("-Xmaxerrs", 500)
}
}
我第一次开始同时使用 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 系统。但是对于 Kotlin,由于某种原因它不起作用。我还添加了: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
}
基本上,这个https://github.com/ribot/android-boilerplate转换成Kotlin。
Kotlin 使用 kapt
而不是 Android 插件中的 annotationProcessor
。
您需要在 build.gradle
文件中包含并使用以下内容:
kapt {
generateStubs = true
}
dependencies {
// ...
kapt daggerCompiler
}
更详细的说明也可以在这里找到:Dagger and Kotlin
我用这个 gradle 并且工作正常
buildscript {
val kotlinVersion = "1.4.20"
repositories {
mavenCentral()
}
dependencies {
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
}
}
plugins {
kotlin("jvm") version "1.5.30"
kotlin("kapt") version "1.5.30"
application
}
repositories {
mavenCentral()
google()
}
application {
mainClass.set("dp.strategy.MiniDuckSimulator")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation ("com.google.dagger:dagger:2.39.1")
kapt ("com.google.dagger:dagger-compiler:2.39.1")
}
kapt {
mapDiagnosticLocations = true // include the Kotlin files into error reports
}
kapt {
javacOptions {
// Increase the max count of errors from annotation processors.
// Default is 100.
option("-Xmaxerrs", 500)
}
}