getting rid of buildDir deprecation
Asked Answered
U

6

35

My personal preference is to override gradle's default "buildDir" from "build" and use "out" instead. Now for the longest time, I just used the expression

buildDir = "$projectDir/out"

but using buildDir is deprecated. I actually didn't find a recommended way to accomplish this same result, though I have seen evidence that this deprecation might disappear in a post-gradle-8.0 world. I'm using gradle 8.3, and I was still getting the deprecation indication in my build.gradle.

Urbanna answered 28/11, 2023 at 14:14 Comment(0)
A
79

To get rid of buildDir deprecation warning, update your build.gradle file with layout.buildDirectory

The recommended replacement for buildDir is layout.buildDirectory. This property provides a more flexible and consistent way to access the build directory.

Earlier:

tasks.register('clean', Delete) {
    delete rootProject.buildDir
}

Now:

tasks.register('clean', Delete) {
    delete rootProject.layout.buildDirectory
}
Abert answered 14/1 at 9:1 Comment(5)
How did you know that?Zygophyllaceous
Is this compatible with Gradle 7.x on Android?Westmoreland
@RonenFestinger Links were available at the time of writing, but since google keep modifying and updating the content, I didn't posted the link, which is exactly what happened here. Anyways, if you have any better suggestion, please let me know. Thanks!Abert
@Westmoreland It should, but I haven't tested it on Gradle 7.x. Also, is there any specific requirement for not updating your Gradle?Abert
No. It is just to have clarity for proper dev documentation and documented test cases for this change in my product.Westmoreland
N
7

I would write it like this to keep it as clean as possible:

layout.buildDirectory.set(layout.projectDirectory.dir("out"))
Nicker answered 28/11, 2023 at 18:28 Comment(1)
your suggestion and mine both worked within subprojects. so I'm using yours now. thanksUrbanna
L
2

If you are getting buildDir is depreciated. We can use the

layout.buildDirectory.get()

works with gradle version 8.4

Livesay answered 13/5 at 10:44 Comment(0)
S
2

You should replace this with

rootProject.layout.buildDirectory

reason for deprecation as per the official Gradle doc is that The Project.buildDir property is deprecated. It uses eager APIs and has ordering issues if the value is read in build logic and then later modified. It could result in outputs ending up in different locations.

Sundew answered 23/5 at 21:27 Comment(0)
U
1

For at least now, I am using the following:

layout.buildDirectory.set(new File("$projectDir/out"))

I think it's ugly, but at least it works.

Urbanna answered 28/11, 2023 at 14:15 Comment(0)
B
1

simple and short layout.buildDirectory.dir("<directory_name>")

Bader answered 3/4 at 19:2 Comment(1)
in gradle 8.3, that syntax did not work for me. care to offer a correction? TIAUrbanna

© 2022 - 2024 — McMap. All rights reserved.