Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building many product flavors is too slow

I have problem, when I am still adding new flavors, it takes more and more time to build. I add it like this:

productFlavors {
   okapps {
      applicationId = 'cz.anywhere.okapps'
      signingConfig = AdamSigningVariable
      versionCode = 41
      versionName = "3.0.1"

      android.sourceSets {
         okapps.res.srcDirs = ['src/adam_okapps_resources/res', 'src/okapps/res']
      }
   }
   ...
   ...
}

When I comment all other flavors and only one is uncommented and build, it takes about 10 seconds. But when I build all (about 180 flavors), it takes almost 5 minutes.

like image 857
jan hruska Avatar asked Mar 11 '15 11:03

jan hruska


People also ask

When would you use a product flavors in your build setup?

When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.

What is the build flavor?

Android Product Flavors are also known as Android build types or Android build variants are the native Android app development way to implement different versions of the same application with minor changes.

What is product flavor?

Product flavours lets you create multiple variants of an android app while using a single codebase. To create product flavours you need to define rules in the build.

Why is Gradle build slow?

Dynamic Dependencies slow down your build since they keep searching for the latest builds every time. To improve the performance we need to fix the version in place. Use only those dependencies that you need. For example google maps dependency, instead of importing compile 'com.


1 Answers

You can try and ignore the other product flavor you are not using, we did it as following:

Add to build.gradle

android {
    productFlavors{
    .
    .
    .
    }

    if (project.rootProject.file('dev.props').exists()){
        def devProps = new Properties()
        devProps.load(project.rootProject.file('dev.props').newDataInputStream())
        def currentDevFlavor = devProps.DEV_FLAVOR

        android.variantFilter { variant ->
            def flavorName = variant.getFlavors().get(0).name
            if(currentDevFlavor  && !flavorName.equals(currentDevFlavor)) {
                variant.setIgnore(true);
            }
        }
    }
}

And then add a file dev.props with the line DEV_FLAVOR=aflavorname

This way when u sync, gradle will act like there is only one flavor, and u dont need to comment anything.

You can also add dev.props to .gitignore.

like image 108
MTZ4 Avatar answered Oct 27 '22 08:10

MTZ4