R.java is very important in android application development. It is used to identify all resource data such as id, layout, image, drawable resource, etc. R.java is generated by android studio automatically. But it is sometimes confused for developers to use it. This article will help you to resolve these problems about R.java you may encounter.
1. Where R.java Is Saved At?
- R.java is commonly saved at directory like
- You can also find it in the android studio left side Project view list like Example/app/build/generated/source/r/debug/com.dev2qa.example.image/R.class.
2. Why Can Not Find R Class In Android Studio?
- This is because you select the wrong subview in the android studio Project view. You can change the project view’s subview as following.
- Click the ” View —> Tool Windows —> Project ” menu item in the top menu bar.
- The above action will show the Project view in the android studio left panel. At the left top in the project view, there is a drop-down list where you can choose the project view’s subview.
- Choose Project in subview drop-down list, then you can see all the folders under the current project will be listed at below panel. You can find the R class in the directory tree also. If you choose other subviews, it will display different content of that subview.
3. What Data Is Saved In R.java?
- Double click the R.java file, it will display its content in the right panel. The source code includes a lot of int-type variables, each int variable refers to one resource in the current android application.
- You can find there are some inner public static final classes defined in R.java also, each inner class represents one kind of resource such as id, layout, string, color, drawable, etc.
- You do not need to care about those int variables and their values. And do not edit them also. If you change the value, you may encounter some strange error when building the android project.
4. How To Resolve Package R Dose Not Exist Error?
- Sometimes you may encounter strange errors about R.class. Such as package R dost not exist, R can not be resolved as a type. You can fix these errors follow the below steps.
- Click ” Build —> Clean Project ” or ” Build —> Rebuild Project ” in the top menu bar in android studio. This will let android studio regenerate R.java again. It will clean the cache and include all newly added resources.
- If the above method still does not take effect, you should check your AndroidManifest.xml file carefully. You need to make sure that the package value in the AndroidManifest.xml root XML element matches your activity package.
- In my example, the manifest root XML element’s package value is com.dev2qa.example and the launcher activity class is in package com.dev2qa.example.image. So after changing the package value, the error is resolved.
- If the error still exists, you should check AndroidManifest.xml carefully to find the potential code that may generate the error. Or you may need to restart android studio or even your computer to fix it 🙂
I suffer from the R directory being non-existent after starting a new application. After the build to start the app, the R directory does not exist in Android Studio 3.2.1 or in the path, or in the actual folder where it is supposed to reside. How to get the R directory to build with the creation of a completely new application?