Kotlin and Dagger2

I am trying to add Kotlin to my project, but after turning on Kotlin I can’t build as Dagger2 classes are no longer generated. I tried the second project, and I have the same problem (in fact, this is worse, it complains about both Dagger2 and DataBinding).

These are the changes I made 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 is here:

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

where DaggerObjectGraph is no longer defined.

Any help would be greatly appreciated.

+6
source share
2 answers

Just uninstall

 kapt { generateStubs = true } 
+2
source

The problem is that you are using

DaggerObjectGraph

which is no longer available in dagger2.

Here is the documentation for dagger 2 .

This is a working version.

at your build.gradle application level add the following under dependency closure

  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 classes of components and modules and enter any actions or fragments that you want.

Here is an example Component Class

 @Singleton @Component(modules = arrayOf(AppModule::class, NetModule::class)) interface AppComponent { fun gson() : Gson fun retrofit(): Retrofit fun okHttpClient(): OkHttpClient } 

Here is an example of a 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() } } 

Here's how the dagger components in dagger 2 are initialized. Now you can create subcomponents for various actions or fragments and enter them.

0
source

Source: https://habr.com/ru/post/1013489/


All Articles