Sunday, November 18, 2012

Where is my R.java?

The most common question related to Android development in NetBeans these days is 'where is my R.java?'

Simple answer: Don't worry, it is not there and it will be created once you build the project.

Why is this so and what is this class for? R.java in your project is a class generated during the build to provide an access to various resources used in your application from Java code. You can find details in in Android developers documentation. For example you will most likely store strings used by your application in res/values/string.xml file. They will be referenced in Java code by identifier defined inside this R class.

Now when the skeleton Android project is created it already contains some of these resources and the Java code for generated activity uses these resource knowing how the generated identifiers look like. Their R class is in the package specified in AndroidManifest.xml file and it is the same package as the main activity uses in this generated code. It means there is no need to modify the source by importing this class. The reason why those as shown as errors is that there was no build to generate R.java (and then compile it to .class and package into .dex and finally create application file .apk) yet. As described above all will be resolved during the build. We may decide to run priming build right after project generation in the future but there are many other more important tasks at the moment.

Note: there is also android.R class. Its purpose is the same as R class defined in your project. It contains references to resources defined in the system. You will use it sometimes to reuse parts of UI to keep the user experience consistent.

1 comment:

  1. hi, when o find the error target to r class i do the import of r class. If i understand what you write in this post, we will have to build main project to create or acess to R class ?

    i try this and i can't run the project in android emulator because of this error. Any sugestion.

    And the android preview don't work or is too slow. Maybe because of this error but i'm not sure.I don't know if it is working in windows, i yet try but in mac os x for now does not work.

    sorry to bother you, i realy would like programming in android in netbeans because to me is much better than the eclipse as java IDE and other languges .

    ReplyDelete