0
votes

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

1 Answers

1
votes

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")
    }
}