本文介绍了实现一个简单的Dagger2示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Dagger2的新手(我一直使用Koin),并且我正在尝试实现一个简单的示例,但是我真的不知道我缺少什么.这就是我到目前为止所得到的.

I'm new using Dagger2 (I always used Koin) and I'm trying to implement a simple sample but I don't really know what I'm missing. This is what I got so far.

app.gradle :

ext.daggerVersion = '2.23.2'

implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"

AppModule.kt :

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

    @Provides
    @Singleton
    fun provideTestOperator(testOperator: TestOperator) = testOperator

    @Provides
    @Singleton
    fun provideTestClass(testClass: TestClass) = testClass

}

AppComponent.kt :

@Singleton
@Component(modules = [
    AndroidInjectionModule::class,
    AppModule::class
])
interface AppComponent : AndroidInjector<App> {
    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(app: App): Builder
        fun build(): AppComponent
    }
}

TestClass.kt & TestOperator.kt 在同一文件中:

TestClass.kt & TestOperator.kt in the same file:

class TestClass @Inject constructor(private val testOperator: TestOperator) {
    fun getRandomValueFromCTest(): Int = testOperator.generateRandomNumber()
}

class TestOperator @Inject constructor() {
    fun generateRandomNumber(): Int = Random.nextInt()
}

App.kt :

class App : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().application(this@App).build()
    }
}

MainActivity.kt :

class MainActivity : AppCompatActivity() {
    @Inject
    lateinit var testClass: TestClass

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        val x = testClass.getRandomValueFromCTest()
    }
}

推荐答案

AppModule.kt :提供应用程序上下文.无需为您的Test *类编写@singleton @provides(将了解原因)

AppModule.kt: Provide the application context. No need to write @singleton @provides for your Test* classes (will see why)

@Module
class AppModule {
    @Provides
    @Singleton
    fun provideApplication(app: App): Context = app.applicationContext
}

AppComponent.kt :@Component.Builder已弃用IIRC.使用@Component.Factory.并将AndroidInjectionModule::class替换为AndroidSupportInjectionModule::class,因为我们使用的是dagger-android-support和android的*Compat*东西.在此处引用名为ActivityModule::class的新模块.

AppComponent.kt: @Component.Builder is deprecated IIRC. Use @Component.Factory. And replace AndroidInjectionModule::class with AndroidSupportInjectionModule::class since we are using dagger-android-support and android's *Compat* stuff. Refer a new module here called ActivityModule::class.

@Singleton
@Component(modules = [
    ActivityModule::class
    AndroidSupportInjectionModule::class,
    AppModule::class
])
interface AppComponent : AndroidInjector<App> {

    @Component.Factory
    abstract class Factory : AndroidInjector.Factory<App>
}

TestClass.kt& TestOperator.kt :由于您是通过编写@singleton和@provides方法来提供单例的,所以我假设您希望它们是单例的.只需使用@Singleton注释类定义,dagger就可以解决.无需编写@Provides方法.

TestClass.kt & TestOperator.kt: Since you were providing singletons by writing @singleton and @provides method, I assume you want them to be singletons. Just annotate the class definition with @Singleton and dagger will take care of it. No need to write @Provides methods.

@Singleton
class TestClass @Inject constructor(private val testOperator: TestOperator) {
    fun getRandomValueFromCTest(): Int = testOperator.generateRandomNumber()
}

@Singleton
class TestOperator @Inject constructor() {
    fun generateRandomNumber(): Int = Random.nextInt()
}

App.kt :由于不推荐使用@Component.Builder,因此使用工厂而不是构建器.

App.kt: Using factory instead of builder since @Component.Builder is deprecated.

class App : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.factory().create(this)
    }
}

ActivityModule.kt :提供一个用于匕首创建活动的模块.

ActivityModule.kt: Provide a module to dagger to create your activities.

@Module
interface ActivityModule {

    @ContributesAndroidInjector
    fun provideMainActivity(): MainActivity
}

MainActivity.kt :最后,从DaggerAppCompatActivity扩展.

class MainActivity : DaggerAppCompatActivity() {

    @Inject
    lateinit var testClass: TestClass

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        val x = testClass.getRandomValueFromCTest()
    }
}

我认为这应该没有问题.有关更多参考,您可以查看此示例和位于 dagger.dev/android 的新的更简单的文档a>

I believe this should run without issues. For more reference you could look into this sample and the new simpler docs at dagger.dev/android

这篇关于实现一个简单的Dagger2示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:13