Move build folders (main and all modules) to different path/folder/hdd/ramdisk

Hi,

I want to move all gradle build folders to one another folder/structure outside project, is that possible?

I use Android Studio + Gradle. It’s android multi module project, so there is a bunch of build folders:
project/.gradle (optional to move)
project/build
project/module1/build
project/module2/build
project/module3/build

What I want to do is move all this (cache/temp) folders to different location outside of project.

I expected, that I will set buildDir=Z:/build/ in my global gradle.properties (and something in project based local.properties what will identify project) and it will create same struncture as in project folder on my Z:/ drive insude build folder, but it didn’t and build failed.
I think, that it try to create all modules + main build folder inside one folder (all together and some files are rewritten by modules), so it fails to build.

What is my goal? Move all temp/cache files to different hard drive or ramdisk (for all project, because I work on more projects at ± same time). Reasons for it is 1) speed of storage (ramdisk in my case is 20x faster than my SSD in laptop) and 2) Lifetime of primary SSD. I don’t see a reason why I should use primary SSD for infinite writing of tons of temp files when it can be stored on some SSD/Ramdisk alocated just for builds/cache/temp files.

My goal is structure like:
Z:\build\projectX\build
Z:\build\projectX\module1\build
Z:\build\projectX\module2\build

Z:\build\projectY\build
Z:\build\projectY\module1\build
Z:\build\projectY\module2\build

In worst case share same foder:
Z:\build\build
Z:\build\module1\build
Z:\build\module2\build

Thanks a lot for help
Same question on Stack Overflow

You have to set the buildDir for each project individually, then it should work.
If you don’t want to configure this within the build as you only want that for your local worktree, you can use an init script to configure the build as you like it.

I tried this solution too, but it do what I described before:
It create just one Z:/build folder and all modules and main build file are inside this folder. It overriding same files inside this folder (from modules), build failed with tons of errors.

Android studio show warning about duplicated roots in same folder:

Duplicate content roots detected

Path [Z:/build/generated/source/buildConfig/debug] of module [Project.assets.main] was removed from modules [Project.common.main, Project.material_calendar_view.main, Project.simpleratingbar.main, …]" Also 12 more paths were deduplicated. See idea log for details

idea.log contains almost same info: idea.log (github.com)

So it really didn’t work for my project.

Second thing, that gradle ignore local.properties variables (project.buildDir=Z\:/build/
or buildDir=Z\:/build/) it works only when it’s in gradle.properties file.

If I run it with single module project (app module only), it works.
Z:\build\ contains same files (identical) as \project\app\build.
That’s the problem with multimodule projects… All modules are mixed in same folder.

I tried this solution too, but it do what I described before:
It create just one Z:/build folder and all modules and main build file are inside this folder. It overriding > same files inside this folder (from modules), build failed with tons of errors.

Then you didn’t do it.
With “individually” I also meant individual values.
Each project should have a unique buildDir or you end up with a big mess as you have noticed.

Second thing, that gradle ignore local.properties variables (project.buildDir=Z\:/build/
or buildDir=Z\:/build/) it works only when it’s in gradle.properties file.

That’s because there is no such thing as a local.properties file.
Not as far as Gradle is concerned.
This is an Android Gradle Plugin specific thing and setting Gradle native essential settings like buildDir are expected to not work from there.

Do you mean every single module must have this configuration? Because I tried it with just one project but it’s multimodule.

Imagine, I clone: android/nowinandroid: A fully functional Android app built entirely with Kotlin and Jetpack Compose (github.com)
this Project. It has about 20-25 modules in this project.
If I set it for this project (just one project) it create a mess as I described before.
But no of modules has it’s own gradle.properties file, and even if it has, that’s unusable… That doesn’t make sense manage build folder for 20-25 modules :frowning:

You confuse Maven or Eclipse terms with Gradle terms.
What you call a “project” is a “build”.
What you call a “module” is a “project”.
What you call “multimodule” is a “multi-project build”.

So when I said you have to set if for each module individually, then in your foreign terms it would mean for each module, yes.

But no of modules has it’s own gradle.properties file, and even if it has, that’s unusable…

Even if it would have, it would not help, because gradle.properties in subprojects are also meaningless for Gradle, if they have any effect, then again just because of some plugin you apply.

hat doesn’t make sense manage build folder for 20-25 modules

Well, if you want to relocate it, you have to do it.
As I said, you can for example do it in an init script if you just want to do it on your machine.
Or you live with the default as nearly everyone else.
Or you configure it centrally in a convention plugin that you then apply to all those projects, but also then each project needs its individual build folder, there is no alternative to that fact.

Thanks a lot for explanation and your time.

I think, that I’m not enough experienced to use init script (Gradle is for me like blackbox required to build Android project, nothing more what I can ever understand :)).
So for me, it’s no way for now.

Something like

allprojects {
    buildDir = "Z:/build/$group/$name/"
}

in <GRADLE_USER_HOME>/init.d/strange-build-dirs.gradle should do I think.

Unless you have multiple work trees for the same project of course, then maybe more something path based