The module must be installed.

I am trying to use the new dagger tool for Android for Android , which still works.

Now I want to expand it to my needs.

In mine, MainActivityModuleI added TestModule:

@Module
abstract class MainActivityModule {

    @ActivityScope
    @ContributesAndroidInjector(modules = arrayOf(TestModule::class))
    internal abstract fun contributeMainActivityInjector(): MainActivity
}

TestModule really simple:

@Module
internal abstract class TestModule {

    @Provides
    internal fun provideTest(): String {
        return "foo bar"
    }
}

But I get this error: TestModule must be set

I looked through the generated source code but cannot find any clues what I need to do. I also searched this on Google, but found only simple examples: - (

What did I forget? The full app can be found on GitHub .

Edit

As Jeff Bowman said, it provideTest()should be static. When I create a Java class as follows:

@Module
public class TestModule {

    @Provides
    static String provide() {
        return "foo bar";
    }
}

it works.

So, the last question: how to do it in Kotlin? This does not work:

@Module
internal abstract class TestModule {

    companion object {

        @Provides
        @JvmStatic
        internal fun provideTest(): String {
            return "foo bar"
        }
    }
}

So I need another way to create a static method.

+4
2

yeh : -)

Kotlin static , companion object, Dagger , @Provides @Module. , companion object

@Module
internal abstract class TestModule {

    @Module
    companion object {

        @Provides
        @JvmStatic
        internal fun provideTest(): String {
            return "foo bar"
        }
    }
}
+5

, , / , , :

@Module
internal class TestModule {

    @Provides
    internal fun provideTest(): String {
        return "foo bar"
    }

}
0

Source: https://habr.com/ru/post/1677560/


All Articles