Android R class resolved despited not being imported

Viewed 23

Normally on Android you need to import the R class but in this project the class accesses R despite it's not being imported in that class, how does this work? If I copy that class to another project I have open Android Studio says I need to import the R class, how come?

This is the class I'm talking about

2 Answers

You are able to use classes in the same package without import.
In Java you have access to public, package local and protected terms.
In Kotlin only public and internal.

Normally on Android you need to import the R class

import is a language thing, more than an Android thing.

but in this project the class accesses R despite it's not being imported in that class

The package attribute on the <manifest> element in AndroidManifest.xml is com.example.android.navigationadvancedsample. That means that R is code-generated as being in that package. The class that you refer to is also in that same package. And, in Java and Kotlin, you do not need to explicitly import classes in the same package as your own.

Related