Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Hilt - multi module project cast error

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

like image 496
Murat AKSU Avatar asked Jul 29 '20 10:07

Murat AKSU


1 Answers

From an answer to a similar question:

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

like image 149
Zizoh Avatar answered Sep 21 '22 15:09

Zizoh