2016-12-24 111 views
6

我正在尝试将Kotlin添加到我的项目中,但启用Kotlin之后,我无法构建Dagger2类不再生成。我尝试了第二个项目,而且我遇到了同样的问题(实际上它更糟,它抱怨Dagger2和DataBinding)。Kotlin和Dagger2

这些都是我做,以使科特林的变化:

项目的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不再被定义。

任何帮助将不胜感激。

+0

我认为你不应该明确使用'kotlin-kapt'。这是一个疯狂的猜测。 – EpicPandaForce

+0

我不知道你是什么意思,但我在网上找到的有关Kotlin的Dagger2的信息清楚地表明需要使用kapt处理器(https://blog.jetbrains.com/kotlin/2015/06/better -annotation处理支承存根合kapt /)。 – Francesc

+0

试试吧。 – EpicPandaForce

回答

2

只是删除

kapt { 
    generateStubs = true 
} 
0

问题是您正在使用

DaggerObjectGraph

这不再dagger2可用。

Here is the documentation for dagger 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类

@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.现在可以为不同的活动或片段创建子组件并注入它们。