Grouping few of many sourceSets having exactly same configuration
<p>Lets say I have the following <code>sourceSets</code>:</p>
<pre><code>sourceSets {
    flavor1 {
        assets.srcDirs = ['repo-assets/flavor1']
        res.srcDirs = ['repo-res/flavor1']
    }
    flavor2 {
        assets.srcDirs = ['repo-assets/flavor2']
        res.srcDirs = ['repo-res/flavor2']
    }
    flavor3 {
        assets.srcDirs = ['repo-assets/flavor1']
        res.srcDirs = ['repo-res/flavor1']
    }
    flavor4 {
        assets.srcDirs = ['repo-assets/flavor2']
        res.srcDirs = ['repo-res/flavor2']
    }
}
</code></pre>
<p>If you notice <code>flavor1</code> and <code>flavor3</code> have same srcDirs and so does <code>flavor2</code> and <code>flavor4</code>.</p>
<hr />
<h3>Trying Possibility#1</h3>
<p>I was trying to figure out if there is a way to avoid the redundancy by using something like this:</p>
<pre><code>sourceSets {
    flavor1, flavor3 {
        assets.srcDirs = ['repo-assets/flavor1']
        res.srcDirs = ['repo-res/flavor1']
    }
    flavor2, flavor4 {
        assets.srcDirs = ['repo-assets/flavor2']
        res.srcDirs = ['repo-res/flavor2']
    }
}
</code></pre>
<p>The above does not work (already tried). Looking for something similar so that i can just provide a common set of <code>sourceDirs</code> for a set of flavors. Anyone tried doing something similar and can provide some pointers?</p>
<hr />
<h3>Trying Possibility#2</h3>
<p>Does the name of <code>sourceSets</code> need to be same as that of flavors?</p>
<p>Can i name the <code>sourceSets</code> separately and then map them to <code>productFlavors</code> like this?</p>
<pre><code>productFlavors {
    flavor1 {
      sourceset = &quot;src1&quot;
    }
    flavor2 {
      sourceset = &quot;src2&quot;
    }
    flavor3 {
      sourceset = &quot;src1&quot;
    }
    flavor4 {
      sourceset = &quot;src2&quot;
    }
}

sourceSets {
    src1 {
    }
    src2 {
    }
}
</code></pre>
<hr />
<h3>Trying Possibility#3</h3>
<p>Can the sourcesets be dynamically assigned via tasks somehow to achieve the same stuff?</p>
<hr />
<h2>UPDATE</h2>
<p><strong>Douglas's</strong> answer sort of helped me get very close to what i was looking for eventually (reducing the code in build.gradle). He used Possibility#3 above. Thanks Douglas! Any better alternative from bounty hunters is still welcome (something closer to possibilities #1 and #2 above). If nothing comes up the bounty is Douglas's already when the period ends as I've accepted his answer. But still will remain optimistic about finding a better alternative.</p>