About R.java

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

About R.java

Postby sasuke » Fri Dec 14, 2007 2:14 am

Hi all,

Everytime we defined string, layout, color, id, etc in our xml files it's always appear in R.java.
we can also defined string, layout, etc in our code (not in xml file).

What's the advantages (and maybe disadvantages :lol: ) between those 2 approach?

I mean, if we defined our string, etc in xml files, does it give a faster access, or maybe the resulting size of the application is smaller than if we defined string, etc in our code?

Thx.
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top

Postby faldureon » Fri Dec 14, 2007 3:27 am

There are a few benefits to having all your strings defined in an xml file and then accessed via variables in code:
  • If you do string matching you'll never type the string differently in two places and have your code not work.
  • Its much easier to do bulk changes if all your strings are in one place.
  • If you ever decide to make your application international and offer it in many different languages, all you have to do is translate your strings.xml file instead of going through code.
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby sasuke » Sun Dec 16, 2007 6:42 am

thanks faldureon.. :D

any other benefits?
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Sun Dec 16, 2007 12:40 pm

Hello sasuke,

faldureon summarized it pretty well.

Internationalization, All in one place --> easy to change, less bugs, Switch Styles by altering just some xml-files and no code, ...

That it, what came to my mind first.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Sun Dec 16, 2007 1:38 pm

I don't know whether my thought right or wrong.

I think the R.java file gave us a faster access to our variables, because we are reffering to our var by it's generated integer value (is it a memory address for the variable?).

What do you think?

Thanks.
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Sun Dec 16, 2007 2:21 pm

Aw yes of course we forgot Compile-Time-Safety :!:
The Compiler/Eclipse will inform you, when you misstyped a reference.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

How is R.java code gened??

Postby Nitinkcv » Tue Dec 25, 2007 6:52 pm

Hi,

Just was curious how does the R.java get code generated. I mean as soon as we change the xml files all the changes are reflected in the R.java. Some plugin??

Thankx,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby faldureon » Tue Dec 25, 2007 11:14 pm

This is only a guess, but i suspect the android plugin parses all the xml files in the res directory and then generates the R.java file based on the values it finds there.
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby plusminus » Wed Dec 26, 2007 11:05 pm

faldureon wrote:This is only a guess, but i suspect the android plugin parses all the xml files in the res directory and then generates the R.java file based on the values it finds there.


Hello faldureon,

you were close, but Eclipse starts the aapt.exe (Android Asset Packaging Tool) with some magic parameters ;) that does what you said.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to General

Who is online

Users browsing this forum: No registered users and 4 guests