How to increase the efficiency of building android project of multiple channels by gradle

I am working on an android project.I want to build this project by gradle. And I need to generate 20 apk files according to the different market channels.The difference of the files is that in the manifest file ,I need to replace the string"UMENG_VALUE"with each channel name. My current way is to use product flavor like this:

productFlavors {
     android.applicationVariants.all{variant ->
                              def manifestFile = "${buildDir}/intermediates/manifests/${variant.dirName}/AndroidManifest.xml"
                        new File(manifestFile).write(new File(manifestFile).getText('UTF-8').replaceAll("@string/channel_id", "${variant.productFlavors[0].name}"), 'UTF-8')
                            variant.processResources.manifestFile = file("${buildDir}/intermediates/manifests/${variant.dirName}/AndroidManifest.xml")

But the problem of this method is that gradle will execute every task according to each flavor right? But in my opinion , most of the steps are exactly the same. So is there a better way for gradle to just compile and dex for only one time and just change the String in manifest file?

Gradle won’t execute each task according to each flavor. Shared code should only get built once.

Thanks for your reply.According to my log of execution, it seem’s that all the procedure of the android build task would execute in each product flavor, and if there are 30 product flavors.It would take nearly 20minutes to finish it. However,the same work ant executing will take 3-4 minutes

The Android plugin specifically adds tasks per flavor, but they should only build what’s different between these flavors (such as the apk). Anyhow, I recommend to take this feedback/question to the Android tooling list. There you’ll find the authors of the Android plugin, which will be more knowledgeable on this.

Thanks a lot