Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I tried a second project, and I have the same issue (it's actually worse, it complains about both Dagger2 and DataBinding).

These are the changes I have done to enable Kotlin:

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

The error happens here:

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

where DaggerObjectGraph is no longer defined.

Any help would be greatly appreciated.

like image 838
Francesc Avatar asked Dec 24 '16 05:12

Francesc


1 Answers

Just remove

kapt {
    generateStubs = true
}
like image 75
Yurii Kyrylchuk Avatar answered Oct 19 '22 16:10

Yurii Kyrylchuk