I am eager to study Dagger 2, which in theory does not seem so difficult, but in practice I have already spent 2 days to launch the project, but to no avail.

@Module class ActivityModule { @Provides @NonNull @Singleton fun getActivitys() : TestClass = TestClass() } @Module class AppModule(val context: Context) { @Provides @Singleton fun getContexts() : Context = context } @Component(modules = [(ActivityModule::class), (AppModule::class)]) @Singleton interface AppComponent { fun inject(mainActivity: MainActivity) } 

App

 class App : Application() { lateinit var appComponent: AppComponent override fun onCreate() { super.onCreate() appComponent = buildComponent() } fun getComponent() : AppComponent = appComponent private fun buildComponent(): AppComponent { return DaggerAppComponent.builder() .appModule(AppModule(this)) .activityBindgsModule(ActivityModule()) .build() } } 

MainActivity

 class MainActivity : AppCompatActivity() { @Inject lateinit var testClass: TestClass override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) App().getComponent().inject(this) Log.d("MAIN", testClass.getString()) } } 

Registered in the manifesto

 <application android:name=".App" ... 

In Build dependencies prescribed such, tried to update, does not help.

  compile "com.google.dagger:dagger:2.11" compile "com.google.dagger:dagger-android:2.11" compile "com.google.dagger:dagger-android-support:2.11" kapt "com.google.dagger:dagger-compiler:2.11" kapt "com.google.dagger:dagger-android-processor:2.11" 

When running the project on the emulator, during the build I get an error in the Messanger section

 Error:(25, 17) Unresolved reference: DaggerAppComponent Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details 

But if I download a ready-made example with GitHub Link to this project, everything compiles, but if I start a new project, I get errors. This example I wrote will do another article.

Updated

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 defaultConfig { applicationId "info.android_developer_community.dagerss" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation "com.google.dagger:dagger:2.14" implementation "com.google.dagger:dagger-android:2.14" implementation "com.google.dagger:dagger-android-support:2.14" kapt "com.google.dagger:dagger-android-processor:2.14" kapt "com.google.dagger:dagger-compiler:2.14" kapt "com.google.dagger:dagger-android-support:2.14" } 
  • This is the reason why Kotlin does not fly up and why not all JVM based languages ​​fly: groovy, scala, ceylon and their name is legion - Barmaley
  • Try rebuild project. - Yuriy SPb
  • @Barmaley, I don’t think that it’s about Kotlin) Most likely something is missing somewhere in the code and, if rewritten in Java, will give the same thing - YuriySPb
  • @Yuriy SPb I copied the code from github to java into a clean project. The project is assembled, but with an error already on the emulator. Then I converted using the java plugin to kotlin got an error. Probably Kotlin not correctly generated something. - Segrei Ulanov
  • 2
    @CegreiUlanov, it seems that you lack this in the build script: apply plugin: 'kotlin-kapt' - Yuriy SPb

1 answer 1

First of all:

 App().getComponent().inject(this) 

will create a new instance of the App class, and you need to get an existing one. This is done like this:

 (applicationContext as App) 

In your case, the final line will look like this:

 (applicationContext as App).getComponent().inject(this) 

And secondly:

As you can understand, the error indicates that DaggerAppComponent not known as a class, and therefore is not imported. You can try to register the import yourself, you need to specify the package in which you have AppComponent , just add the word "Dagger" to the class name (this class will be generated during the compilation process)

UPD:

You need to add apply plugin: 'kotlin-kapt' to the beginning of the file. Also need to add

 kapt { generateStubs = true } 

in android for Kotlin to generate stubs

  • I registered what you gave in the example, and followed your recommendation, but the error still pops up. - Segrei Ulanov
  • @CegreiUlanov modules and components do you have in separate files? - zTrap
  • But now he still swears at the package, with the same error and that name. - Segrei Ulanov
  • This time all the modules and components are located in my di - Segrei Ulanov folder
  • import info.android_developer_community.dagerss.di.ActivityModule import info.android_developer_community.dagerss.di.AppComponent import info.android_developer_community.dagerss.di.AppModule import info.android_developer_community.dagerss.di.DaggerAppComponent