久久久久久久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問題)
主站蜘蛛池模板: 成人久久视频 | 亚洲成人精品久久 | 午夜午夜精品一区二区三区文 | 国产91 在线播放 | 欧美精品在线免费观看 | 夜久久| 国产午夜精品久久久久免费视高清 | 国产精品久久久久一区二区三区 | 99久久日韩精品免费热麻豆美女 | 91精品国产综合久久精品图片 | 999久久久久久久久6666 | 99re热这里只有精品视频 | 男女在线网站 | 中文字幕视频在线看5 | 亚洲精品3 | 性色av一区二区三区 | 午夜精品久久久久久久久久久久 | 福利网站在线观看 | 欧美激情久久久久久 | 日本黄色的视频 | 亚洲精品免费视频 | 一区二区三区四区国产 | 国产精品亚洲一区 | 国产日韩欧美中文 | www.日本精品| 精品国产一区二区三区久久 | 国产高清视频一区二区 | 国产成人99久久亚洲综合精品 | 国产精品久久久久久久久免费丝袜 | 中文在线播放 | 亚洲精品在线免费 | 日韩av在线一区二区三区 | 亚洲成人精品在线观看 | 久久久久国产精品一区二区 | 中文字幕一区二区不卡 | 久久大| 国产农村妇女毛片精品久久麻豆 | 黄色片免费在线观看 | 尤物视频在线免费观看 | 日韩精品一区二区三区在线播放 | 国产在视频一区二区三区吞精 |