18

I just installed Android Studio and I am just learning to build using Gradle. However, with the default project setup, my builds are located in the project directory and I would like to have them placed elsewhere (preferably outside of the project directory). Is it possible to achieve this? Where do I make a change and what change do I make?

lhan
  • 4,585
  • 11
  • 60
  • 105
Pablo Jomer
  • 9,870
  • 11
  • 54
  • 102

3 Answers3

42

in root build.gradle

allprojects {
    buildDir = "/path/to/build/${rootProject.name}/${project.name}"
}

See also Gradle global build directory

and docs https://gradle.org/docs/current/userguide/writing_build_scripts.html

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • Works like a charm. Had do shift my build folder out of `eCryptfs` directory! – xyz Jun 26 '15 at 20:11
  • Yeeeee! I can finally work in our company's Dropbox, thank you! – lorenzo-s Mar 22 '16 at 16:28
  • @AlexeyKuznetsov I tried this and something similar by jsut applying it to subprojects, but, i only get some things move out of the default directory, i still see the R files and the intermediates (like exploded-aar, and things like that) on the default directory, which are the things that i wanted to move out in the first place, any idea what can i do? i've tried everything i've found online as far now – Jorge Aguilar Nov 24 '16 at 18:30
  • 1
    This will cause: `Duplicate content roots detected` for test directories. – Zingam May 02 '20 at 12:26
5

You can pass the "buildDir" property to the gradlew.bat (I'd assume you can do this in the Linux version as well but I haven't tested it)

Example:

gradlew.bat assembleRelease -PbuildDir="C:\BuildFolder"
MBulava
  • 521
  • 3
  • 9
-1

The project iml file has a BUILD_FOLDER_PATH attribute. I haven't tried changing it myself yet, so not sure if it will work. The default value is $MODULE_DIR$/build.

Edit: I did a quick test and this did not work. Once changed, the project needs to reload because the iml file changed. Upon reload it reverts the build directory to default.

gingerdev
  • 143
  • 1
  • 10