-1

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.

GenError
  • 885
  • 9
  • 25
  • 1
    `case` is a Java keyword, so I am not at all surprised that you are having problems with it in a Java package name. – CommonsWare Apr 28 '22 at 16:18
  • I'm surprised that this causes only problems with hilt and not with anything else. And that Android Studio didn't show a warning like "using Java keywords is not recommended" when it even shows the folder differently. - Thanks for the refresher! – GenError Apr 28 '22 at 16:43
  • The reason it causes problems with Dagger is that Dagger generates Java code. – Nitrodon Apr 28 '22 at 18:25

1 Answers1

1

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

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32