Android Hilt - multi module project cast error
Asked Answered
T

3

11

I am trying to use hilt for my project which contains dynamic features. I am facing an error that I cannot fully understand why. I get an error like this:

java.lang.ClassCastException: com.social.analysis.DaggerApp_HiltComponents_ApplicationC$ActivityRetainedCImpl$ActivityCImpl$FragmentCImpl cannot be cast to com.social.login.intro.IntroFragment_GeneratedInjector
    at com.social.login.intro.Hilt_IntroFragment.inject(Hilt_IntroFragment.java:94)
    at com.social.login.intro.Hilt_IntroFragment.initializeComponentContext(Hilt_IntroFragment.java:58)
    at com.social.login.intro.Hilt_IntroFragment.onAttach(Hilt_IntroFragment.java:50)
    at androidx.fragment.app.Fragment.onAttach(Fragment.java:1602)
    at com.social.login.intro.Hilt_IntroFragment.onAttach(Hilt_IntroFragment.java:40)

My ViewModel in LOGİN MODULE (dynamic features)

class IntroViewModel @Inject constructor(): ViewModel() {
// TODO: Implement the ViewModel
}

My Fragment in LOGIN MODULE

@AndroidEntryPoint
class IntroFragment : BaseFragment<FragmentIntroBinding, IntroViewModel>(
R.layout.fragment_intro
) {

companion object {
    fun newInstance() = IntroFragment()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
}

override fun onInitDataBinding() {
    viewBinding.viewModel = viewModel
}
}

My Base Fragment in UI Module

abstract  class BaseFragment <B: ViewDataBinding, M: ViewModel>(
@LayoutRes
private val layoutId: Int
): Fragment() {

@Inject
lateinit var viewModel: M
lateinit var viewBinding: B
...

My Application Class in App Module

@HiltAndroidApp
class App : SplitCompatApplication() {
}

My Main Activity in App Module

@AndroidEntryPoint
class MainActivity : AppCompatActivity() 

I call the IntroFragment from the App module. Then the application crashes.

The project structure looks like this:

Project Structure

Tinnitus answered 29/7, 2020 at 10:31 Comment(10)
You should use dagger2 instead of hilt when using dynamic feature moduleUniformitarian
did you ever solve this issue?Cabbage
A dagger should be used instead of a dagger-hilt.Tinnitus
I am having this problem even without dynamic features... I have my activity in a simple "com.android.library". @MuratAKSU the only solution you found was to use dagger2?Shipway
As far as I know, this problem only happens when using the dynamic features module. Something else could be causing the error. @ShipwayTinnitus
@MuratAKSU you are totally right. After a clean and rebuild it worked.Shipway
Have you solved this issue? And howCigar
i am facing similar situation so far no solutionBalkanize
me too @MohammadMuddasir, app crashes when my dynamic feature fragment is annotated with AndroidEntryPointNonparticipating
Hi guys, same problem here when trying to use Hilt, anyone available to help? I tried all the solutions but without success Any luck? @kakaranaraIchang
L
12

From an answer to a similar question:

Delete the .gradle directory (in the project base directory) Invalidate Caches and restart Android Studio.

Lucinalucinda answered 12/1, 2021 at 13:58 Comment(2)
I had the same problem and this solved it for me thanksNikolenikoletta
Crown this guy!Bushbuck
L
2

If you are using dynamic feature module, don't forget to remove @AndroidEntryPoint annotation. Because the annotation is from Hilt and you used the Dagger2 as DI.

Lilybelle answered 19/7, 2023 at 13:11 Comment(0)
N
1

For me the problem was I created the EntryPointAccessors from a different scope. Ensure your module and entry point scope is similar to the EntryPointAccessors creation.

Use this if your module scope is Singleton

@EntryPoint
@InstallIn(SingletonComponent::class)
interface ChatModuleDependencies {

    fun timeHelper(): TimeHelper

}

EntryPointAccessors.fromApplication(
    applicationContext,
    ChatModuleDependencies::class.java
)

Use this if your module scope is Activity

@EntryPoint
@InstallIn(ActivityComponent::class)
interface ChatModuleDependencies {

    fun timeHelper(): TimeHelper

}

EntryPointAccessors.fromActivity(
    this,
    ChatModuleDependencies::class.java
)

And etc...

Norry answered 16/4 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.