6

I have been receiving this error on Dagger-Hilt and I don't know why, I even downgraded to a simple project to see if I could find my problem, but apparently I am doing everything correctly. I created the Application class, declared it on the Manifest file, created a single Module that provides a String and I get the error when I try to inject it on the main activity, the error says "D:\Programacion\Kotlin\TryingHilt\app\build\tmp\kapt3\stubs\debug\com\y4kuzabanzai\tryinghilt\MainActivity.java:7: error: [Hilt] public final class MainActivity extends androidx.appcompat.app.AppCompatActivity { ^ @EntryPoint com.y4kuzabanzai.tryinghilt.MainActivity must also be annotated with @InstallIn [Hilt] Processing did not complete. See error above for details."

Here my code:

Application Class

@HiltAndroidApp
class MyApp : Application() {
}

Module class

@Module
@InstallIn(ApplicationComponent::class)
class TestModule {

    @Singleton
    @Provides
    fun providesString(): String {
        return "Something"
    }
}

MainActivity Class

@EntryPoint
class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var injectedString: String

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

        Log.d("MyTag", "onCreate: $injectedString")
    }
}

1 Answer 1

8

You should you @AndroidEntryPoint here with the activity. @Entrypoint used for some different purpose and its not the right use.

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var injectedString: String

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

        Log.d("MyTag", "onCreate: $injectedString")
    }
}
3
  • But is the way they say you should use it, how must I do this then?
    – Tony
    Jun 8, 2021 at 14:55
  • Nope this is the link for which @EntryPoint used for dagger.dev/hilt/entry-points. For your case you should use @AndroidEntryPoint
    – 0xAliHn
    Jun 8, 2021 at 14:57
  • Thank you so much Sir!! I feel so stupid, I read it wrong, thanks a million times
    – Tony
    Jun 8, 2021 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.