Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

android - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...