开发者

why does android create the R.java file

why not just leave the data( strings.xml, main.xml ) in the .xml files? why convert it into the R.java?

it seems that android can read xml ( eg. androidmanifest.xml ). so why not just leave the strins.xml data and main.xml data in .xml form and se开发者_StackOverflow中文版nd those xml's to the android device?

I am new ... maybe I will eventually see that there is a lot more in the R.java other than mere static variables that derived directly from those .xml's.

thanks, Shannon


so that you can easily reference you object that you have created in you layout, drawable and string files. this class represents all the resources that can be instantiated inside a code or can be used inside your coding


So that you can easily reference your resources ids from your code with the R class object avoiding compile errors, I think


If you have a bug in your program, you want it detected as early as possible.

If resources are retained as XML any bug will be found at runtime (either test-driven or user-driven). Testing can never prove the absence of all defects, so some bugs may be found by the users of your software!

However, if resources are converted to source in R then all references to them will be checked by the compiler. Any bugs (relating to resource references) are detected at this much earlier stage, long before your customers get to seem them.


Transferring the id references of the XML content into code makes it easy to use these references while coding and the correct usage is visible during compile time. So runtime exceptions are avoided.

I do not know if this is the core reason but that's what counts for me :-).


R.java is a handler to all the resources.

You create your resources, and the R.java automatically build a connection between your code and your res. You can easily fetch your various kind of resources from R, and android provide lots of API supporting R.java and its handlers(those ints). You dont need to manually open a stream to res files, do file reading, images decoding and etc. Its great convience.


R.java doesn't contain any data. It's generated by aapt during the build process when it prepares resources to pack them into the APK, including compiling XML files in a format that's easier to read and parse. R.java contains static int members that are only indexes to the packed resources.

See A Detailed Look at the Build Process, and also take a look into a project's bin/ folder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜