【问题标题】:Kotlin and Dagger2Kotlin 和 Dagger2
【发布时间】:2016-12-24 05:25:56
【问题描述】:

我正在尝试将 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

应用构建.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。

任何帮助将不胜感激。

【问题讨论】:

  • 我认为你不应该明确使用kotlin-kapt。这是一个疯狂的猜测。
  • 我不明白你的意思,但是我在网上找到的带有 Kotlin 的 Dagger2 的信息清楚地表明需要使用 kapt 处理器 (blog.jetbrains.com/kotlin/2015/06/…)。
  • 不用它试试。
  • 我设法通过将我的 Dagger2 组件和模块转换为 Kotlin,然后遍历所有可注入类并将它们也转换为 Kotlin 来构建。虽然这可行,但它不是正确的解决方案,因为关于 Kotlin 的整个想法是互操作性,并且必须立即将所有内容转换为 Kotlin 以便我可以编译是不对的。如果我删除 kapt,我将无法编译可注入的 Kotlin 文件。
  • 那我猜错了,抱歉。我认为问题出在 kapt2 上,但我想不是

标签: android kotlin dagger-2


【解决方案1】:

删除

kapt {
    generateStubs = true
}

【讨论】:

    【解决方案2】:

    问题是你正在使用

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 2018-07-11
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多