SoFunction
Updated on 2025-03-11

Potential pitfalls encountered in using Dagger2 in Kotlin

What is Dagger2?

Dagger2 is an open source library based on Java annotations and completes dependency injection during the compilation stage. It is mainly used for decoupling between modules and facilitates testing.

1. Kotlin Dagger2 configuration

apply plugin: ''
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

...

dependencies {
...
 //dagger2 
 compile ["dagger"]
 kapt ["dagger-compiler"]

 //dagger2 android a dagger2 About Android Enhancement Library Optional compile ["dagger-android"]
 //Optional compile ["dagger-android-support"]
 //Optional kapt ["dagger-android-processor"]
}

AppComponent

@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
 fun inject(app: BaseApplication)
}

AppModule

@Module
class AppModule(val app: Application) {
 @Provides
 @Singleton
 fun provideApplication() = app
}

Application

class BaseApplication : Application() {

 override fun onCreate() {
  ()
  initApplication()
  DaggerCoreComponent
    .builder()
    .coreModule(CoreModule(this))
    .build();
 }
}

After completing the above configuration, you can happily use Dagger2 in Kotlin.

For more information about Kotlin Dagger2 configuration, please refer to this:https:///article/

2. The pitfalls encountered by Kotlin using Dagger2

At that time, I wanted to inject presenter into the activity. The code is as follows. I didn't pass when making Project. I couldn't figure it out.

Pit 1

class MainActivity : AppCompatActivity() , {


 @Inject
 var mPresenter :  ? = null


 override fun onCreate(savedInstanceState: Bundle?) {
 //******//
 (savedInstanceState)
 ().build().inject(this)

 mPresenter?.subscribe()

 }


 @Module
 inner class PresenterModules {

 @Provides
 fun providePresenter():  {
  return MainPresenter(this@MainActivity)
 }
 }
}

@Component(modules = arrayOf(::class))
interface MainActivityComponent {
 fun inject(activity: MainActivity)
}

Then I converted the kotlin code into bytecode and then into java code, and found that Presenter is private.

And we all know that the injected object cannot be private

public final class MainActivity extends AppCompatActivity implements View {
 @Inject
 @Nullable
 private Presenter mPresenter;
-----Omitted
}
//So change the above code to @Inject
 @JvmField
 var mPresenter :  ? = null

//or@Inject
lateinit mPresenter : 

Compilation will be successful

Pit 2

 @Inject
 @JvmField
// @Named("preneter")// Error The correct way is as follows @field:[Named("preneter")]
 var mPresenter :  ? = null
 @Module
 inner class PresenterModules {

 @Provides
 @Named("preneter")
 fun providePresenter():  {
  return MainPresenter(this@MainActivity)
 }

 @Provides
 @Named("hello")
 fun provide():String{
  return "hello"
 }
 }

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.