Kotlin和Dagger2

我正在尝试将Kotlin加入到我的项目中,但是在启用Kotlin之后,我无法再创建Dagger2类。 我尝试了第二个项目,我有同样的问题(它实际上更糟糕,它抱怨Dagger2和DataBinding)。

这些是我为了启用Kotlin所做的改变:

项目build.gradle:

diff --git a/build.gradle b/build.gradle index 486700c..91e4cda 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,15 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.0.5-3' repositories { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0-alpha2' + classpath 'com.android.tools.build:gradle:2.3.0-beta1' classpath 'com.google.gms:google-services:3.0.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files 

应用程序build.gradle:

 diff --git a/app/build.gradle b/app/build.gradle index 345dab0..e59f91c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'com.android.application' -apply plugin: 'com.neenbedankt.android-apt' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' android { compileSdkVersion 25 @@ -39,6 +40,13 @@ android { incremental true javaMaxHeapSize "5g" } + sourceSets { + main.java.srcDirs += 'src/main/kotlin' + } +} + +kapt { + generateStubs = true } dependencies { @@ -71,11 +79,15 @@ dependencies { compile 'com.artemzin.rxjava:proguard-rules:1.2.1.0' // Dagger 2 - apt 'com.google.dagger:dagger-compiler:2.7' - testApt 'com.google.dagger:dagger-compiler:2.7' + kapt 'com.google.dagger:dagger-compiler:2.7' + //testApt 'com.google.dagger:dagger-compiler:2.7' compile 'com.google.dagger:dagger:2.7' provided 'javax.annotation:jsr250-api:1.0' + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } apply plugin: 'com.google.gms.google-services' +repositories { + mavenCentral() +} 

错误发生在这里:

 sObjectGraph = DaggerObjectGraph .builder() .appModule(new AppModule(context.getApplicationContext())) .build(); 

DaggerObjectGraph不再被定义。

任何帮助将不胜感激。

只要删除

 kapt { generateStubs = true } 

问题是你正在使用

DaggerObjectGraph

这在dagger2中不再可用。

这里是匕首2的文档 。

这是一个工作版本

在您的应用程序级别build.gradle添加下面的依赖关闭

  compile "com.google.dagger:dagger:2.9" kapt "com.google.dagger:dagger-compiler:2.9" provided 'javax.annotation:jsr250-api:1.0' 

也添加到相同的文件

 kapt { generateStubs = true } 

然后创建组件和模块类,并注入你想要的任何活动或片段。

这是一个示例组件类

 @Singleton @Component(modules = arrayOf(AppModule::class, NetModule::class)) interface AppComponent { fun gson() : Gson fun retrofit(): Retrofit fun okHttpClient(): OkHttpClient } 

这是一个示例模块类

 @Module class AppModule(internal var mApplication: Application) { @Provides @Singleton fun providesApplication(): Application { return mApplication } } 

在我的应用程序类

 class App : Application() { companion object { @JvmStatic lateinit var component: AppComponent } override fun onCreate() { super.onCreate() appComponent = DaggerAppComponent.builder() .appModule(AppModule(this)) .netModule(NetModule(Constants.BASE_URL)) .build() } } 

这是如何在匕首2中初始化匕首组件。您现在可以为不同的活动或片段创建子组件并注入它们。