Android Studio build/sync time in multi module project

Viewed 880

I have a project with 30+ modules and sub modules(feature base modules).

I've read that build time of a multi module project is less than a single module project, as it was when it had less than 10 modules(Layer base modules). But when module count grows it affects Android Studio index/build/sync time. Moreover, during development of a multi module project Android Studio takes high CPU and Memory usage.

What should I do to optimize build speed and Android Studio performance?

Note 1: There is a complex relation between module dependencies.

Note 2: Gradle parallel has been enabled and I've checked Google Optimize your build speed

2 Answers

I was having the same issue as yours. I solved my problem by using custom gradle dependency resolution. The idea is pretty simple, I publish all my modules to a repository (artifactory in my case) and then in my build.gradle file for modules I depend on the published modules. To switch the dependency from published to local during development I use gradle dependency resolution to resolve to local versions.

def loadedModules = [:]
getSubprojects().forEach {
    loadedModules.put(it.name, it.path)
}
def group = 'np.com.susanthapa.efficientmultimodule'

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter()

        // replace configurations for all modules
        configurations.all {
            // get our own dependencies
            resolutionStrategy.dependencySubstitution.all { dependency ->
                if (dependency.requested instanceof ModuleComponentSelector && dependency.requested.group == group && loadedModules.containsKey(dependency.requested.module)) {
                    def targetProject = findProject(loadedModules.get(dependency.requested.module))
                    if (targetProject != null) {
                        dependency.useTarget targetProject
                    }
                }
            }
        }
    }
}

Then what you can do is unload all the modules from your project except app module and also comment out all the modules from settings.gradle file.

Now to work on a particular module just uncomment that specific module in settings.gradle and also load that module in android studio. The above code will make sure that the local version of the loaded module will be used through out your project. This should reduce the build time as well as android studio indexing.

For more info here is the link to the blog post.

Follow the profiling steps in the link you posted.

In addition or for quicker/less formal profiling you can use the --scan option with a ./gradlew build and check the stats that are published online.

Memory usage can be tuned based on those results and that can give you great benefits. Personally, I've seen builds take literally minutes in garbage collection due to low memory.

You can also increase the memory available to Android Studio based on these results. Help->Edit custom VM options. But I would get to the point you are seeing improvements on the command line (./gradlew) builds first.

Related