The Curious Case of the Missing R
The Java and Kotlin code both refer to these R
values. The R
class gets code generated by the IDE and build tools based on our current roster of resources. Each resource gets a corresponding R
value based on the type of resource (R.layout
, R.id
, R.string
, etc.) and the name of the resource (R.layout.activity_main
, R.id.showElapsed
, R.string.elapsed
, etc.).
Sometimes, when you are working on your Java or Kotlin code, you will run into cases where you cannot seem to use the R
class to reference your resources. There are two main scenarios:
- The IDE says that it does not know what
R
is - The IDE says that it knows what
R
is but does not recognize a particular resource that you are trying to reference (e.g., you haveR.layout.foo
, and eitherlayout
orfoo
are reported as not being recognized)
These problems come about due to where that R
class lives and when it is created (or updated).
Where R Lives
All of our Java and Kotlin classes reside in some “package”. In the starter project, that package is com.commonsware.helloworld
. Our one-and-only class, MainActivity
, is in that package. The samples shown in this chapter are in different packages, such as com.commonsware.jetpack.sampler.simplebutton
for the Kotlin edition of the SimpleButton
sample.
The R
class will be code-generated by the build tools in the Java package identified by the package
attribute in the <manifest>
element. So, for a project whose package is com.commonsware.jetpack.sampler.simplebutton
, the fully-qualified class name for R
is com.commonsware.jetpack.sampler.simplebutton.R
.
Java and Kotlin share a common rule: you do not need to import
classes that are in the same package as the class that you are in. So, in our projects, MainActivity
can refer to R
without an import
statement, as both MainActivity
and R
are in the project’s package (e.g., com.commonsware.jetpack.sampler.simplebutton
).
If you try referencing R
, and Android Studio complains that it does not know what R
is, consider whether your class is in a different package than the one defined in the <manifest>
element. If it is, you will need to add an import
statement to pull in your R
class.
When R Is Created
R
is created as part of building your project. To allow code-completion and other assistance features of Android Studio to work, R
gets re-created whenever you add, rename, or remove a resource, as R
is an “index” of all of the resources in your app. However, occasionally, you will use some feature of Android Studio that happens to add, rename, or remove a resource, but Android Studio fails to regenerate that R
class.
If Android Studio knows what R
is but does not recognize some particular resource, try manually building the project, via Build > Rebuild Project from the Android Studio main menu. This forces R
to get regenerated, and it may clear up your problem.
When R Is Not Created
Sometimes, though, Android Studio does not know what R
is because R
is missing entirely.
If there is some bug in one of your resources — or, as it turns out, if there is some bug in your manifest — R
may not be regenerated. If Android Studio says that it does not know what R
is, and either you already have the import
statement or you should not need one (since R
and your code are in the same package), then it is likely that there is some problem in a resource or your manifest.
If you manually build the project — again, via Build > Rebuild Project from the Android Studio main menu — you should get a build error telling you exactly what resource is flawed, so you can try to fix it.
Package Names
Since the package
value is used for Java code generation, it has to be a valid Java package name. Java convention says that the package name should be based on a reverse domain name (e.g., com.commonsware.myapplication
), where you own the domain in question. That way, it is unlikely that anyone else will accidentally collide with the same name.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.