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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
juztcode
  • 1,196
  • 2
  • 21
  • 46

4 Answers4

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.

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!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • how does it mean to invalidate the cache? – juztcode Aug 08 '19 at 12:38
  • for some reason before I did this, the `\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\\R.java` folder had this as mentioned in the below answer, can you tell me about this behaviour? – juztcode Aug 08 '19 at 12:43
  • 1
    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. :) – Reaz Murshed Aug 08 '19 at 14:21
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

Yash
  • 3,438
  • 2
  • 17
  • 33
  • How and why does this folder get created? – juztcode Aug 08 '19 at 12:40
  • 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. https://www.javatpoint.com/R-java-file-in-android – Yash Aug 08 '19 at 12:48
  • Seems that AS no longer creates a `generated` directory anymore (I'm using AS 3.6.1). – SMBiggs Mar 13 '20 at 20:46
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

ssynhtn
  • 1,307
  • 12
  • 18