匕首和科特林。 Dagger 不生成组件 类
Dagger and Kotlin. Dagger doesn't generate component classes
我是 kotlin 和 Dagger 的新手。我有一个小问题,我不知道如何解决,也没有找到解决方案。
这就是我所拥有的:
@Module
class AppModule (app: Application) {
private var application: Application;
init {
this.application = app;
}
@Provides fun provideApplication(): Application? {
return application;
}
@Provides fun provideResources(): Resources? {
return application.resources;
}
}
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent: AppComponentBase {
public class Initializer {
private constructor(){}
companion object {
fun Init(app: Application): AppComponent? {
return DaggerAppComponent.builder().appModule(AppModule(app)).build()
}
}
}
}
AppComponentBase
: 这个接口包含了这个组件需要的所有方法。
现在,问题是如果我在 companion object
中执行此 DaggerAppComponent.builder().appModule(AppModule(app)).build()
调用,则 Dagger 不会生成此 DaggerAppComponent
class。
如果调用相同的行,任何由 companion object
匕首生成 de class 没有任何问题。
我寻找解决方案的另一件事是创建另一个具有相同结构的不同 class,并将 DaggerAppComponent
作为内部对象导入,我得到了相同的结果。
我不知道外面有什么组件的初始化。那么,还有其他替代解决方案吗,或者我做错了什么?
您需要 build.gradle 中的 kapt processor:
kapt {
generateStubs = true
}
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
此扩展程序将为匕首生成代码。
此外,对于较新的 gradle 版本,您还可以在 build.gradle:
中应用该插件
apply plugin: 'kotlin-kapt'
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
您可以查看this project以供参考
我不知道这个变化是什么时候发生的,但是在 Kotlin gradle 插件的 1.1.2 上,你替换了这个(在你的模块的 build.gradle
中):
kapt {
generateStubs = true
}
有了这个:
apply plugin: 'kotlin-kapt'
然后确保将使用 annotationProcessor
的依赖项替换为 kapt
。
例如,旧方法是使用:
annotationProcessor (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
现在您使用:
kapt (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
如果您对 DaggerComponent
有疑问,您应该添加
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
到build.gradle
它将为dagger 2
生成kotlin
代码,否则项目将仅在Rebuild
上构建
KOTLIN 1.1.4 更新
generateStubs
不再有效。随意使用最新的 Kotlin 进行构建,它会在 Android Studio 的 Messages 部分告诉您不再需要它。这是 Dagger2 for Android 和 Kotlin
的最新依赖项列表
apply plugin: 'kotlin-kapt'
//...
// Other Gradle stuff
//...
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.4-3"
compile 'com.google.dagger:dagger-android:2.12'
kapt 'com.google.dagger:dagger-android-processor:2.12'
compileOnly 'com.google.dagger:dagger:2.12'
kapt 'com.google.dagger:dagger-compiler:2.12'
}
这个问题可以通过以下不同于原始答案的更改来解决
以下方法也能很好地解决这个问题
有插件
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
和依赖关系
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
参考这个 Gist
我的情况一定是Dagger实现中的一些排除规则
com.mycompany.native
-> Dagger 不生成代码
com.mycompany.other
-> Dagger 生成代码
我在这上面浪费了很多时间:-(我希望它能帮助别人!
我是 kotlin 和 Dagger 的新手。我有一个小问题,我不知道如何解决,也没有找到解决方案。
这就是我所拥有的:
@Module
class AppModule (app: Application) {
private var application: Application;
init {
this.application = app;
}
@Provides fun provideApplication(): Application? {
return application;
}
@Provides fun provideResources(): Resources? {
return application.resources;
}
}
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent: AppComponentBase {
public class Initializer {
private constructor(){}
companion object {
fun Init(app: Application): AppComponent? {
return DaggerAppComponent.builder().appModule(AppModule(app)).build()
}
}
}
}
AppComponentBase
: 这个接口包含了这个组件需要的所有方法。
现在,问题是如果我在 companion object
中执行此 DaggerAppComponent.builder().appModule(AppModule(app)).build()
调用,则 Dagger 不会生成此 DaggerAppComponent
class。
如果调用相同的行,任何由 companion object
匕首生成 de class 没有任何问题。
我寻找解决方案的另一件事是创建另一个具有相同结构的不同 class,并将 DaggerAppComponent
作为内部对象导入,我得到了相同的结果。
我不知道外面有什么组件的初始化。那么,还有其他替代解决方案吗,或者我做错了什么?
您需要 build.gradle 中的 kapt processor:
kapt {
generateStubs = true
}
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
此扩展程序将为匕首生成代码。
此外,对于较新的 gradle 版本,您还可以在 build.gradle:
中应用该插件apply plugin: 'kotlin-kapt'
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
您可以查看this project以供参考
我不知道这个变化是什么时候发生的,但是在 Kotlin gradle 插件的 1.1.2 上,你替换了这个(在你的模块的 build.gradle
中):
kapt {
generateStubs = true
}
有了这个:
apply plugin: 'kotlin-kapt'
然后确保将使用 annotationProcessor
的依赖项替换为 kapt
。
例如,旧方法是使用:
annotationProcessor (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
现在您使用:
kapt (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
如果您对 DaggerComponent
有疑问,您应该添加
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
到build.gradle
它将为dagger 2
生成kotlin
代码,否则项目将仅在Rebuild
KOTLIN 1.1.4 更新
generateStubs
不再有效。随意使用最新的 Kotlin 进行构建,它会在 Android Studio 的 Messages 部分告诉您不再需要它。这是 Dagger2 for Android 和 Kotlin
apply plugin: 'kotlin-kapt'
//...
// Other Gradle stuff
//...
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.4-3"
compile 'com.google.dagger:dagger-android:2.12'
kapt 'com.google.dagger:dagger-android-processor:2.12'
compileOnly 'com.google.dagger:dagger:2.12'
kapt 'com.google.dagger:dagger-compiler:2.12'
}
这个问题可以通过以下不同于原始答案的更改来解决
以下方法也能很好地解决这个问题
有插件
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
和依赖关系
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
参考这个 Gist
我的情况一定是Dagger实现中的一些排除规则
com.mycompany.native
-> Dagger 不生成代码
com.mycompany.other
-> Dagger 生成代码
我在这上面浪费了很多时间:-(我希望它能帮助别人!