3

I'm just trying to learn android development, and am using the latest version of android studio, however, I cannot find the R.java file in my folder tree view:

enter image description here

I've also cleaned and rebuilt my project but still, I can see neither the .../source/R folder nor the R.java file.

1
  • 1
    have you tried invalidate cache, restart and rebuild? Commented Aug 7, 2019 at 16:43

4 Answers 4

6

i'm learning Android apps as well and i was searching for the R class like you
but i found out that in new versions it dosn;t exit you can read about here https://developer.android.com/studio/releases/gradle-plugin

Faster R class generation for library projects: Previously, the Android Gradle plugin would generate an R.java file for each of your project's dependencies and then compile those R classes alongside your app's other classes. The plugin now generates a JAR containing your app's compiled R class directly, without first building intermediate R.java classes. This optimization may significantly improve build performance for projects that include many library subprojects and dependencies, and improve the indexing speed in Android Studio.

Sign up to request clarification or add additional context in comments.

Comments

4

From this tutorial.

Android R.java is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory.

If you create any component in the activity_main.xml file, id for the corresponding component is automatically created in this file. This id can be used in the activity source file to perform any action on the component.

Hence you should not worry about the R.java file, as this will be generated automatically when you will build the project. The R.java might not be generated in some cases though, mostly if there is an error in your layout files. Check your layout files and look for errors in there. Fix the error and you should be able to get the reference of the R.java file in your activity.

To import the R.java file in your activity, use the automatic import option. In most cases, Alt + Enter does the trick for you for automatic import.

If you are still having trouble finding the R.java, you might consider invalidating the cache of the Android Studio from File -> Invalidate Caches/Restart.

enter image description here

Hope that helps!

3 Comments

how does it mean to invalidate the cache?
for some reason before I did this, the \app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\<package_name_tree>\R.java folder had this as mentioned in the below answer, can you tell me about this behaviour?
Yes, that is the path where the R.java is located. The file is generated automatically there when you build the project. Great to know that you have found the file. :)
3

Try this:

\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\<package_name_tree>\R.java

package_name_tree means : If package is a.b.c the goto: /a/b/c/R.java

3 Comments

How and why does this folder get created?
Android R.java is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory. javatpoint.com/R-java-file-in-android
Seems that AS no longer creates a generated directory anymore (I'm using AS 3.6.1).
2

As of gradle plugin version 4.0.0 and Android Studio 4.0

R.java is now generated as a compiled R.jar file in this directory: project_dir/app/build/intermediates/compile_and_runtime_not_namespaced_r_class_jar/debug/R.jar

You can use a java decompiler like jd-gui if you want to view the source of the R.java that should have been generated in previous versions of Android Studio

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.