简体   繁体   English

Kotlin和Dagger2

[英]Kotlin and Dagger2

I'm trying to add Kotlin to my project, but after enabling Kotlin I can't build as the Dagger2 classes no longer get generated. 我正在尝试将Kotlin添加到我的项目中,但是启用Kotlin后,由于不再生成Dagger2类,因此无法构建。 I tried a second project, and I have the same issue (it's actually worse, it complains about both Dagger2 and DataBinding). 我尝试了第二个项目,但我遇到了同样的问题(实际上更糟,它抱怨Dagger2和DataBinding)。

These are the changes I have done to enable Kotlin: 这些是我为启用Kotlin所做的更改:

Project build.gradle: 项目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

App build.gradle: 应用程序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()
+}

The error happens here: 错误发生在这里:

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

where DaggerObjectGraph is no longer defined. 不再定义DaggerObjectGraph的地方。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Just remove 只需删除

kapt {
    generateStubs = true
}

The Problem is your are using 问题是您正在使用

DaggerObjectGraph DaggerObjectGraph

which is no longer available in dagger2. 在dagger2中不再可用。

Here is the documentation for dagger 2 . 这是匕首2的文档

This is a working version 这是一个工作版本

in your app level build.gradle add the following under the dependencies closure 在您的应用程序级别build.gradle中,在依赖项关闭项下添加以下内容

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

also add this to the same file 还要将此添加到同一文件

kapt {
    generateStubs = true
}

Then create the components and module classes and inject in whatever activity or fragment that you want. 然后创建组件和模块类,并注入所需的任何活动或片段。

Here is a sample Component Class 这是一个示例组件类

@Singleton
@Component(modules = arrayOf(AppModule::class, NetModule::class))
interface AppComponent {

    fun gson() : Gson
    fun retrofit(): Retrofit
    fun okHttpClient(): OkHttpClient
}

Here is a sample Module class 这是一个示例模块类

@Module
class AppModule(internal var mApplication: Application) {

    @Provides
    @Singleton
    fun providesApplication(): Application {
        return mApplication
    }

}

In my application class 在我的应用课上

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()

}


}

This is how dagger components are initialised in dagger 2. You can now create subcomponents for different activities or fragment and inject them. 这就是在Dagger 2中初始化Dagger组件的方式。您现在可以为不同的活动或片段创建子组件并注入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM