Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbose android injection does not actually perform any injection #171

Open
matejdro opened this issue Sep 9, 2019 · 0 comments
Open

Verbose android injection does not actually perform any injection #171

matejdro opened this issue Sep 9, 2019 · 0 comments

Comments

@matejdro
Copy link

matejdro commented Sep 9, 2019

When doing android injection the verbose way (without ContributesAndroidInjection), calling AndroidInjection.inject() will not actually do anything (fields will not be injected).

This is after applying workaround for the #170.

Sample code:

@Singleton
@Component(
    modules = [
        AppModule::class,
        TextModule::class
    ]
)
interface AppComponent {
    fun inject(app: MyApp)
}

@Module(
    subcomponents = [MainActivitySubcomponent::class]
)
interface AppModule {
    @Binds
    @IntoMap
    @ClassKey(MainActivity::class)
    fun bindMainActivityInjectorFactory(
        factory: MainActivitySubcomponent.Factory
    ): AndroidInjector.Factory<*>
}

@Module
object TextModule {
    @Provides
    @JvmStatic
    fun provideString() = "Test"

    // Inject empty string map as a workaround for
    // https://github.com/JakeWharton/dagger-reflect/issues/170
    @Provides
    @JvmStatic
    fun provideEmptyStringMap()
            : Map<@JvmSuppressWildcards String, @JvmSuppressWildcards Provider<AndroidInjector.Factory<*>>> =
        emptyMap()
}

@Subcomponent
interface MainActivitySubcomponent : AndroidInjector<MainActivity> {
    @Subcomponent.Factory
    interface Factory : AndroidInjector.Factory<MainActivity>
}


class MyApp : Application(), HasAndroidInjector {
    @Inject
    lateinit var androidInjector: DispatchingAndroidInjector<Any>

    override fun onCreate() {
        Dagger
            .create(AppComponent::class.java)
            .inject(this)

        super.onCreate()
    }

    override fun androidInjector(): AndroidInjector<Any> = androidInjector
}

class MainActivity : Activity() {
    @Inject
    lateinit var injectedString: String

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)

        Log.d("MainActivity", "Injected string: $injectedString")
    }
}

Above code is supposed to print Injected string: Test into logcat when activity starts, but instead it crashes with lateinit property injectedString has not been initialized since injection was not performed.

Running this with normal annotation processing dagger works fine.

Reproduced with:

    implementation 'com.google.dagger:dagger-android:2.24'

    // Reproduced on Sun Aug 11 03:26:53 UTC snapshot
    implementation 'com.jakewharton.dagger:dagger-reflect:0.2.0-SNAPSHOT'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant