久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 appComponent dagger 2 中動態添加測試模塊?

Dynamically add test modules in appComponent dagger 2?(在 appComponent dagger 2 中動態添加測試模塊?)
本文介紹了在 appComponent dagger 2 中動態添加測試模塊?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

您好,是否可以在我的 AppComponent 中添加測試模塊?

Hi is it possible to add test modules in my AppComponent?

下面是我的 appComponent 的真實表示

Below is my real representation of my appComponent

@Singleton
@Component(modules = arrayOf(MainModule::class,
        AnalyticsModule::class,
        MainAndroidBinding::class,
        AccountAndroidBinding::class,
        AndroidSupportInjectionModule::class,
        HomeAndroidBinding::class,
        NetworkModule::class))
interface ApplicationComponent : AndroidInjector<DaggerApplication> {
    fun inject(myApplication: MyApplication)
    override fun inject(instance: DaggerApplication)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(applicaton: Application): Builder
        fun build(): ApplicationComponent
    }

}

我可以像這樣直接將測試模塊添加到 testAppComponent 中,但它并沒有為我提供動態添加不同 testModules 的靈活性.

I could just add the test modules directly to the testAppComponent like this but it doesnt offer me much flexibility to dynamically add different testModules.

@Singleton
@Component(modules = [
    (MainModuleTest::class),
    (TestMainAndroidBindingTest::class),
    (AccountAndroidBindingTest::class),
    (AnalyticsModuleTest::class),
    (AndroidSupportInjectionModule::class),
    (NetworkModuleTest::class)])
interface TestAppComponent : ApplicationComponent {

    fun inject(launchActivityTest: LaunchActivityTest)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(applicaton: Application): Builder

        fun build(): TestAppComponent
    }
}

這是我的 MyApplication 類

Here is my MyApplication class

class MyApplication : DaggerApplication() {

    companion object {

        private lateinit var application: MyApplication

        fun get(): MyApplication {
            return application
        }
    }


    @Inject
    lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    lateinit var applicationComponent: ApplicationComponent

    override fun onCreate() {
        super.onCreate()
        application = this
    }

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        applicationComponent = DaggerApplicationComponent.builder()
                .application(this)
                .build()
        applicationComponent.inject(this)
        return applicationComponent
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

}

在 LaunchActivityTest 上,這是我設置它以使用此 testApp 組件的方式

On the LaunchActivityTest this is how i set it up to use this testApp component

  @Before
    fun setUp() {
        val app = InstrumentationRegistry.getTargetContext().applicationContext as MyApplication
        val testAppComponent = DaggerTestAppComponent.builder().application(app).build()
        app.applicationComponent = testAppComponent
        testAppComponent.inject(this)
    }

我一直在關注本指南,直到我偶然發現我的 DaggerTestAppComponent 沒有公開模塊讓我動態添加自己,因為我的 AppComponent 類擴展了 AndroidInjector,它會自動為你添加模塊

I was following this guide until i stumbled o the point where my DaggerTestAppComponent doesnt expose the modules for me to dynamically add myself due to the fact that my AppComponent class extends AndroidInjector which automatically adds the modules for you

https://proandroiddev.com/writing-espresso-儀器測試-with-dagger2-kotlin-d30f12c4769b

上面動態添加它的模塊是這樣的:

The above dynamically adds its modules like this:

testAppComponent = DaggerTestAppComponent.builder()
                .appModule(AppModule(app))
                .apiModule(TestApiModule())
                .prefModule(TestPrefModule())
                .build()

在我的情況下我不能這樣做,除非我重做我的 AppComponent 以便它不會擴展 AndroidInjector.如果我這樣做,那么在我真正的 impl 代碼中我必須手動設置模塊.

I cant do that in my case unless i redo my AppComponent so that it doesnt extend AndroidInjector. If i do that then in my real impl code i have to manually set the modules.

還有其他方法嗎?

推薦答案

您應該在組件構建器中添加一個函數并使用BindsInstance".

You should add a function to your component builder and use "BindsInstance".

示例組件

@Singleton
@Component(modules = {
    AndroidSupportInjectionModule.class,
    ApplicationTestModule.class,
    ActivityBuilder.class})
public interface TestExampleComponent extends AndroidInjector<DaggerApplication> {

void inject(TestApplication app);

@Override
void inject(DaggerApplication instance);

@Component.Builder
interface Builder {
    @BindsInstance
    TestExampleComponent.Builder application(DaggerApplication application);
    Builder applicationModule(ApplicationTestModule appTestModule);
    TestExampleComponent build();
  }
}

在這個組件中,我使用BindsInstance"添加了applicationModule函數,我可以通過ApplicationTestModule.

In this component, I added applicationModule function with using "BindsInstance" and I can pass ApplicationTestModule.

然后你可以添加不同的測試模塊.

Then you can add different test modules.

TestApplicationComponent appComponent = DaggerTestAppComponent.builder().application(this).
applicationModule(appTestModule).build();
appComponent.inject(this);

這篇關于在 appComponent dagger 2 中動態添加測試模塊?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 日韩一区二区三区视频 | 91xxx在线观看 | 成人亚洲精品久久久久软件 | www.日韩系列 | 国产精品久久网 | 91视视频在线观看入口直接观看 | 国产精品中文字幕在线 | 免费成人高清 | 最新中文字幕在线播放 | 3p视频在线观看 | 91porn国产成人福利 | 亚州精品成人 | 成人国产网站 | 国产精品黄色 | 亚洲欧美视频一区二区 | 欧美久久久 | 日韩综合网| 国产精品久久久久久高潮 | 亚洲精品九九 | 国产精品网址 | 午夜精品一区二区三区在线视频 | 日本高清不卡视频 | 欧美又大粗又爽又黄大片视频 | 日韩视频精品在线 | 久久99精品视频 | 午夜欧美a级理论片915影院 | 欧美一区在线视频 | 亚洲成人毛片 | 国产女人与拘做受免费视频 | 国产一区久久精品 | 国产不卡一区 | 免费h在线 | 国产精品成人一区二区三区 | 成人免费一区二区三区视频网站 | 精品一区二区在线观看 | 伊人久久综合 | 综合激情网 | 欧美日韩在线免费观看 | 午夜天堂精品久久久久 | 久久久精品一区二区 | 国产精品99久久久久久久久久久久 |