# Android Gradle SourceSets ###### tags: `Android Gradle` ![](https://i.imgur.com/nB6hg0p.png) [TOC] # Introduction ## What is it? **SourceSets** is used to divide our resources in different folders. ## Why do we need it? We might have encountered many times, as our project grows, our resources become too big that is difficult to actually manage them. Here comes sourceSets usage. # How to use it? ## 1. Set up resources folders For a newly made project, we may just see folders(such as drawable, layout, values, etc.) inside the res folder. However, we have to divide them so we will just move them all inside a folder. >**Attention:** >Just by dividing the resources will not be enough, as the system will not be able to read your resources. For instance, you need to set up SourceSets settings inside the build.gradle(:app).[color=red] ### Resources Folder Movement Example We have to move original folders in res, into a new folder in the same res. **Note:** We cannot use any naming folder, we have to keep the same naming such as drawable, layout, values, etc. >![](https://i.imgur.com/aIOAIww.png)[color=lightblue] ### Real Project Example As we can see, in this example, resources have already been divided into many folders. All these folders contain the same folders as the newly made project res. >![](https://i.imgur.com/8lRLQpR.png)[color=lightblue] Here we can see that the folders in res, may contain all original folders or just some, depending on each project or division need. >![](https://i.imgur.com/GLFZEjJ.png)[color=lightblue] # 2. Set up SourceSets in build.gradle(:app) ## Why we need to set up? By just dividing the resources into folders inside res is not enough. In normal circunstances, the **project will automatically read the res folder** and look for **default folders: drawable / assets / values / layout**. However, if we did not specify anything, it will not show anything. **Example to help understanding:** Let's see our image below, just for testing, we just annotated the sourceSets setting and as we did synchronization, we found our that in **android folder mode view**, res folder no longer has any files inside it. >![](https://i.imgur.com/ukaW1RS.png)[color=red] ## How to set up? Inside **build.gradle(:app) or build.gradle(:moduleName)**, we add our SourceSets setting **inside the android {} sector**. We will show how to do it in our set up example. ### "CAN" & "MUST NOT" do actions We have to know that we: > **CAN:** [color=lightgreen] * **Deal with various type of resources: assets, res, etc.** * **Access to other folder resources (No matter which folder)** **String with theme folder access from different folders:** ![](https://i.imgur.com/53cq7c9.png =600x550) **Layout/string/drawable/color folder access:** ![](https://i.imgur.com/8wktZLa.png =600x800) > **MUST NOT:** [color=red] * Have same naming files inside folders aside from values folder. **Ex:** Having activity_main.xml in layout folder in different folders. **Consequences:** Duplicate resources error. * Name same string/theme/color resources **Ex:** See image example below. **(Correct Way)** **Consequences:** Duplicate resources error. ![](https://i.imgur.com/Q337D7T.png) ### Set up Example We have to set up what resources need to be processed, therefore, we have to state the location of these resources according to their type: assets/ res/ etc, by using **type.srcDirs = 'location'**. If we check below image, we will notice that we just named all the folders we divided in **res.srcDirs** (srcDirs = src Directories) **build.gradle(:app) inside android{}**: ```java= sourceSets { main { jniLibs.srcDirs = ['libs'] res.srcDirs = [ 'src/main/res', 'src/main/res/common', 'src/main/res/feedback', 'src/main/res/match', 'src/main/res/splash', 'src/main/res/main', 'src/main/res/hot', 'src/main/res/captcha', 'src/main/res/login', 'src/main/res/usercenter', 'src/main/res/store', 'src/main/res/guessing', 'src/main/res/search','src/main/res/discover','src/main/res/anchor'] assets.srcDirs = ['src/main/assets'] } } ``` # Note ## View Resources Even though we divided the resources, we will not see that division in **Android Project Folder View Mode**. All resources will be compile into the same folder, therefore, be aware when you name the resources. But you can still see the division in project mode. **Android View Mode VS Project View Mode:** ![](https://i.imgur.com/8F55WfD.png) ## There can be 2 res folder By stating another name on res folder, we can also add it in our sourceSets and get project to also read its files. ![](https://i.imgur.com/29KPEhf.png)