Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude res folder from gradle build flavours?

I have a requirement to remove a specific res folder from a flavour.

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        res.srcDirs = ['res']
        aidl.srcDirs = ['src']
        assets.srcDirs = ['assets']
    }
}

productFlavors {
    flavor1 {
        sourceSets {
            flavor1 {
                resources {
                    exclude 'res/drawable-mdpi/*'
                }
            }
        }
    }
    flavorDimensions "flavor"
}

But still drawable-mdpi folder is coming to the apk.

So could anyone please specify what mistake am I making.

Thanks Vivek

like image 944
vivek Avatar asked Oct 21 '15 15:10

vivek


People also ask

How do I get rid of .gradle folder?

Inside the project you can find the . gradle folder. Inside you can find all settings and other files used by gradle to build the project. You can delete these files without problems.

What are Buildtypes and product Flavours in gradle and what can you use them for?

Build Variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Build Type applies different build and packaging settings. An example of build types are “Debug” and “Release”.


1 Answers

I finally solved this problem!

I have found this link.

And did this:

  1. add an xml file to res/raw folder. I named it resources_discard.xml, here is it:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
    tools:discard="@raw/hd/*" />
    
  2. this file is placed in the correct directory structure for my flavor called lite ("src/lite/res/raw")

This way, contents of res/hd folder are not included in lite builds, effectively reducing my lite build apk size by 50%

Update: to exclude some images from different flavors, you have to put the images in the "assets" folder, and in gradle declare:

    flavor {
        aaptOptions {
            ignoreAssetsPattern '/folder:*.jpg:*.png' //use : as delimiter 
        }
    }

I also learned that you can't have subfolders in /res/raw folder.

like image 162
lxknvlk Avatar answered Sep 22 '22 08:09

lxknvlk