Home > Mobile >  Package folder named 'case' in Android Studio causes hilt error
Package folder named 'case' in Android Studio causes hilt error

Time:04-29

I found a very strange behavior in Android Studio. When I try to create a package case then Android Studio marks the folder as folder and not as a package:

Screenshot from Android Studio showing package folders and only the folder case does not have a dot on the icon

This is not just a UI problem. When I place a class inside the case folder and I want to use dependency injection to inject an instance of this class into a view model, the class is not found by the hilt compiler and leads to an error:

@HiltViewModel annotated class should contain exactly one @Inject annotated constructor.

And the hilt generated java file is missing the class in the case folder from the constructor.

However, I can use the class normally without dependency injection in the app, so kotlin in general seems to be ok with it.

I would have blamed it as a hilt bug, but since Android Studio recognizes the folder differently I kind of curious if somebody knows why.

CodePudding user response:

You cannot use the Java keyword. case is a keyword.

For more keys you can check: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

  • Related