5

I am using ViewModel inside BottomSheetDialogFragmet() so I have to mark my BottomSheet with @AndroidEntryPoint.

@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment() {

    private val viewModel: MyAddressesViewModel by viewModels()
    
    ......
}

But when I tried to build my project it is giving me this error:

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing 
org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option 
to get more log output. Run with --scan to get full insights.

And here is my output for run with --stacktrace

https://gist.github.com/javlonrahimov/95de968645cace1dfb6e425381f8014b

If I delete the @AndroidEntryPoint above my BottomSheetFragmen() it is building and installing the app. But when I try to open the bottomSheet the app is crashing as I injected my repositories to the ViewModel:

java.lang.RuntimeException: Cannot create an instance of class uz.unical.other.ui.my_addresses.view_model.MyAddressesViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.getViewModel(SearchAddressDialog.kt:26)
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.onResume(SearchAddressDialog.kt:86)
    at androidx.fragment.app.Fragment.performResume(Fragment.java:3039)
    at androidx.fragment.app.FragmentStateManager.resume(FragmentStateManager.java:607)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:306)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8528)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 Caused by: java.lang.InstantiationException: java.lang.Class<uz.unical.other.ui.my_addresses.view_model.MyAddressesViewModel> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278) 
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54) 
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41) 
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.getViewModel(SearchAddressDialog.kt:26) 
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.onResume(SearchAddressDialog.kt:86) 
    at androidx.fragment.app.Fragment.performResume(Fragment.java:3039) 
    at androidx.fragment.app.FragmentStateManager.resume(FragmentStateManager.java:607) 
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:306) 
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100) 
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:246) 
    at android.app.ActivityThread.main(ActivityThread.java:8528) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 

@AndroidEntryPoint is working without errors on other ordinary Fragments.

UPDATE:

Here is my view model:

@HiltViewModel
class MyAddressesViewModel @Inject constructor(
   private val repository: AddressRepository,
   private val geocoderRepository: GeocoderRepository
) : ViewModel() {}

UPDATE 2: Right now I am annotating BottomSheetDialogFragment() and it is giving me errors while building.

And If I change this

@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment(){

to this

@AndroidEntryPoint
class SearchAddressDialog : Fragment() {

it is working perfectly fine.

Can I actually annotate BottomSheetDialogFragment() with @AndroidEntryPoint?

UPDATE 3:

Here is my BottomSheetDialogFragment

Javlon
  • 1,108
  • 2
  • 15
  • 29
  • It seems to be right, can you clean build and build again? – Abhishek AN Sep 29 '21 at 19:00
  • I have tried a clean build. But no success. – Javlon Sep 29 '21 at 19:02
  • what is your navigation version? – Abhishek AN Sep 29 '21 at 19:09
  • `const val navigation_version = "2.3.5"` – Javlon Sep 29 '21 at 19:14
  • have you annotated the view model with: `@HiltViewModel`? – Mark Sep 29 '21 at 19:30
  • @MarkKeen Yes I have – Javlon Sep 29 '21 at 19:31
  • and `@Inject` on the constructor? The error suggests it only knows how to create it using `Class::newInstance` meaning it isn't exposed to dagger as an injectable class. – Mark Sep 29 '21 at 19:33
  • Yes, I have annotated with @Inject. I have updated my question to include my ViewModel. – Javlon Sep 29 '21 at 19:41
  • What version is your kotlin set to? Can you have a look at this https://stackoverflow.com/questions/67410992/upgrading-android-kotlin-version-to-1-5-0-throwing-error-message-on-build – Abhishek AN Sep 29 '21 at 21:59
  • @AbhishekAN my kotlin version is 1.5.31. I have looked at that question but it didn't help. I have updated my question. – Javlon Sep 30 '21 at 04:21
  • Have you used underscores in package names anywhere else? Currently your package namespace is : `uz.unical.other.ui.my_addresses.view_model.MyAddressesViewModel` - try renaming the `my_addresses` and `view_model` package names without underscores. – Mark Sep 30 '21 at 08:21
  • I have renamed my packages. But it didn't work. Same error. – Javlon Sep 30 '21 at 08:27
  • Hmm .. Ok. The only thing I'd suggest is creating your own `ViewModelProvider.Factory` manually - adding to your graph in a module and using Dagger through `AndroidSupportInjection.inject(this)` in the `BottomSheetDialogFragment()` along with injecting the factory - basically just Dagger without Hilt. you have to narrow down where the problem lies with Dagger or Hilt. Standard Dagger does not have a `DaggerBottomSheetDialogFragment` class, so if Hilt does direct mappings through `@AndroidEntryPoint` then it could be the problem. – Mark Sep 30 '21 at 08:35

4 Answers4

2

After long research, I couldn't solve the @AndroidEntryPoint problem. And I have come up with this: I just pass my ViewModel to my BottomSheetDialogFragment on its constructor:

class SearchAddressDialog(
    private val viewModel: MyAddressesViewModel
) : BottomSheetDialogFragment() {
    ........ 
}

And I opened my dialog like this:

val dialog = SearchAddressDialog(viewModel)
dialog.show(childFragmentManager, SearchAddressDialog.TAG)

I have used the ViewModel of the current fragment.

Javlon
  • 1,108
  • 2
  • 15
  • 29
  • 1
    Pretty sure you'll have to use a `FragmentFactory` if you aren't already, if not try rotating your device (create a configuration change), I'd expect it to throw an error as the FragmentManager won't know how to recreate the Fragment. – Mark Oct 05 '21 at 14:01
  • 1
    Yes, I have implemented `FragmentFactory` after some crashes :). And now it is working perfectly. – Javlon Oct 05 '21 at 14:27
  • 1
    I find this answer to be the most helpful ! – abhijat_saxena Oct 08 '21 at 17:19
  • This answer is not correct. DialogFragments, just like any Fragments, should always have a no-arg empty constructor, and no other constructor. The solution would have been to ensure that the ViewModelProviderFactory is a HiltViewModelProviderFactory., – EpicPandaForce Jun 02 '23 at 10:32
0

Did you add @Inject annotation on
AddressRepository and GeocoderRepository classes constructors? maybe you forget to annotate the constructor of the dependent object of your ViewModel class. because of it Hilt/Dagger is unable to resolve the dependency tree.

If this is the case, annotate all dependent class constructors with @Inject the problem will be resolve.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Zeeshan Akhtar
  • 441
  • 1
  • 4
  • 9
  • I have checked all the dependent objects and I think everything is fine. I have updated my question. – Javlon Sep 30 '21 at 04:19
0

First of all, I hope you added hilt-android-gradle-plugin in your project.

Secondly, there was an issue in dagger-hilt long ago regarding this inheritance dependency but it's already been fixed since 2.28. You can see more details in this link. https://github.com/google/dagger/issues/1910

So, for your case without adding @AndroidEntryPoint to BottomSheetDialogFragment should work but did you also try adding with this? Did it work that way?

Also, can you please share your BottomSheetDialogFragment class?

0xAliHn
  • 18,390
  • 23
  • 91
  • 111
  • Yes, I have the `hilt-android-gradle-plugin` in my project. As for the GitHub issue, It didn't help. I have updated my question to include my BottomSheetDialog. – Javlon Oct 02 '21 at 11:59
  • From the logcat its doesn't seem like dagger issue. What is your kotlin version and dagger-hilt version? Also which java version you are using? – 0xAliHn Oct 02 '21 at 12:22
  • `const val kotlin_version = "1.5.31"` `const val hilt_version = "2.38.1"` I am using Java 11 – Javlon Oct 02 '21 at 12:43
0
@AndroidEntryPoint & @HiltViewModel

works perfectly!

Here is my bottom sheet fragment and view model.

@AndroidEntryPoint
class BottomSheetFragment : BottomSheetDialogFragment() {
    companion object {
        const val TAG = "BottomSheetFragment"
    }

    private val viewModel by viewModels<BottomSheetFragmentViewModel>()
    private var _binding: BottomSheetFragmentBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,): View {
        _binding = BottomSheetFragmentBinding.inflate(inflater, container, false)
        return binding.root
    }

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

@HiltViewModel
class BottomSheetFragmentViewModel @Inject constructor(
    private val localData: LocalRepository,
    private val remoteData: RemoteRepository,
) : BaseViewModel() {

    fun test() {}
}

and finally opening it from another fragment like this:

BottomSheetFragment().show(childFragmentManager, BottomSheetFragment.TAG);

Note: using dagger & hilt version 2.41

Hope this will help! Happy coding!!

enam
  • 952
  • 11
  • 11