In a typical AS project there is a folder with resources - res. It usually contains all the resources used. I learned that in it you can create a raw folder in which you can store what I want and there are no problems with it when building a project. Is it possible to create subfolders with resources in the src folder so that when compiling there are no errors and the resources are determined?

For example, I have a lot of images in the project and it’s not convenient to store them only in the drawable folder, I would like to create drawable_1, drawable_2, and so on.

Added as written below:

sourceSets { main { res.srcDirs = [ 'src/main/res', 'src/main/res/drawable', 'src/main/res/drawable/images' ] } } 

When building the project Gradle produces the following error:

 Execution failed for task ':app:mergeDebugResources'. C:\SDKandroid\desisns\AppName\app\src\main\res\drawable\images\one.jpg:1:1: Error: Content is not allowed in prolog. 

    1 answer 1

    Create a drawable2 folder in the drawable folder

    In build.gradle

     sourceSets { main { res.srcDirs = [ 'src/main/res/drawable/drawable2', 'src/main/res/drawable', 'src/main/res' ] } } 

    drawable2 need to cat.png resource from the cat.png folder: in the code - R.drawable.drawable2.cat , in xml - @drawable/drawable2/cat .


    And, in general, if there are a lot of pictures, it is better to create the assets folder and implement any hierarchy there. In the assets folder, create the animals folder and the cat.png image in it. To get a picture in the code, use, for example, this code.

     public Drawable getDrawableFromAssets(String path) { Drawable img = Drawable.createFromStream(getAssets().open(path), null); return img; } 

    Well, like this, for example

     imageView.setImageDrawable(getDrawableFromAssets("animals/cat.png")); 
    • Writes such an error: Error: (1, 1) Error: Content is not allowed in prolog - Denis
    • where and who writes? - Flippy
    • Gradle writes an error: Execution failed for task ': app: mergeDebugResources'. > C: \ SDKandroid \ desisns \ App \ app \ src \ main \ res \ images \ main.jpg: 1: 1: Error: Content is not allowed in prolog. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. - Denis