Pablo Jomer
Pablo Jomer

Reputation: 10418

Change gradle build directory in android studio?

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?

Upvotes: 19

Views: 24729

Answers (5)

Tester2389
Tester2389

Reputation: 159

When creating a new app with the nowadays default Kotlin DSL builDir = "path" indeed doesn't work. However, builDir = file("path") still does, being deprecated nevertheless. While setBuildDir("path") is its direct successor, however meanwhile this as well became drepacted as has to replaced by the following.

At least for Kotlin DSL, latter however is now as well deprecated and you should switch towards the following.

layout.buildDirectory.set(file("C:/Users/YourUserNameHere/AppData/Local/Android/build/${rootProject.name}/${project.name}"))

Despite its name file() indeed works for directory paths as well

Regarding old Groovy Gradle builDir = "path" is still the "non-deprecated" way to go.

Upvotes: 1

Brian M
Brian M

Reputation: 419

Created a new app a few days with

Android Studio Giraffe | 2022.3.1 Patch 4
Build #AI-223.8836.35.2231.11090377, built on November 13, 2023
Runtime version: 17.0.6+0-b2043.56-10027231 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.

and the above no longer work. I hate that.

But found that this one does

allprojects {
    setBuildDir("C:/Users/YourUserNameHere/AppData/Local/Android/build/${rootProject.name}/${project.name}")

}

I use this when I want to run/compile that/any app while it's in Google's "My Drive" In this way, I can code on multiple computers.

Upvotes: 1

Paul Verest
Paul Verest

Reputation: 64012

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

Upvotes: 43

MBulava
MBulava

Reputation: 521

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"

Upvotes: 5

gingerdev
gingerdev

Reputation: 139

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.

Upvotes: -3

Related Questions