android – Kotlin和Dagger2

前端之家收集整理的这篇文章主要介绍了android – 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

App 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
}
原文链接:https://www.f2er.com/android/317770.html

猜你在找的Android相关文章