0

I make a research, and I delve deep into Android resources files. According to this link, it says that Android externalize and separate users' resources from the code to allow using these resources by Ids that will be generated in R.class, here is the text:

Once you externalize your application resources, you can access them using resource IDs that are generated in your project's R class. 

(1): Does being the 'res' folder exists means generating the R.java class? In other words, is R.java a representation to "ids" assigned to any values inside 'res' folder?

(2): Is it possible to place my 'layout' or 'string' files in any other folders aside from 'res'?

4
  • For no. 2 yes though you need to override something in your Gradle file. However your drawable, layout etc. must be located in the new 'res' location. Commented Jul 7, 2017 at 9:38
  • @Enzokie so what do u mean is, that i can delete the "res" folder and create another folder named for example "my_res" and inside it i can place all my resources(strings, color,layouts,...etc)..right= Commented Jul 7, 2017 at 9:48
  • @Enzokie i tried to change the name of the default res folder to my_res and then i received a message that i have to propagate my change to gradle..i checked gradle files but i do not know how to set the new name of the resource file to gradle Commented Jul 7, 2017 at 9:54
  • yes you can do that and you need to add additional code in your Gradle file also. Commented Jul 7, 2017 at 10:03

2 Answers 2

1

Is R.java a representation to "ids" assigned to any values inside 'res' folder?

It is most likely the idea of R.java.

Is it possible to place my 'layout' or 'string' files in any other folders aside from 'res'?

Yes you can do that but before you start make sure you checked the project structure view and not the other options like android structure view.

In your build.gradle

android { .... sourceSets { main { res.srcDirs = [ "src/main/module-res/compose" ]; } } } 

Then create the desired directory similar to what you have declared. Here is an example image showing that it will work.

enter image description here

What's good about it is that you can make more res folder (just add it in the array and separate it with comma). Take note that each res folder must have its own drawable, layout etc. folder.

Example:

res.srcDirs = [ "src/main/module-res/compose", "src/main/module-res/design", "src/main/module-res/extra" ]; 
Sign up to request clarification or add additional context in comments.

Comments

1

R.java file is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory. when you create any component in the xml file, id for the corresponding component is automatically created in this file.

when creating an layout file using android studio it will automatically put it under layout folder, but when you create it manually it will be classified as the parent folder, eg : create layout file under drawable will be called like R.drawable.mLayout, android studio will show it as warning but it will work fine, so yes you can put any type of xml under any folder and it will work fine, put it's easier to classify as default for more readability and clearer architecture

2 Comments

thank you for your answer.(1)what do you mean by creating a layout by androidstudio?do u mean the genearted layout when i create a new activity?(2)what if i deleted the res folder and placed all my resources either inside another folder i created named for example "my_res" or inside "main" directory that contains my java classes??
creating layout using android studio by right click > new > layout, if your create your own folder under res it won't work because it's out of his default folders.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.