Best way to get the gradle build file (as File) (Kotlin or Groovy) from a project



To retrieve the project Gradle build file (build.gradle or build.gradle.kts), I used the following code:

project.basePath?.let {
val connection = GradleConnector.newConnector().forProjectDirectory(File(it)).connect()
val model = connection.model(
val gradleProjectModel = model.get()
val sourceFile = gradleProjectModel?.buildScript?.sourceFile?

Unfortunately, when there is an error in the content of the build.gradle(.kts) file the "model.get()" fails with an exception.

Is there another way to retrieve the build.gradle(.kts) file even if there are some errors in the file content?

I can of course use:

project.basePath?.let {
val file1 = File(it, "build.gradle")
val file2 = File(it, "build.gradle.kts")

but It's a little bit hard-coded!

Thanks a lot for help

Comment actions Permalink

If you know the exact directory where the Gradle script file resides, you can try



Comment actions Permalink

Thanks Nikita for this answer. I'll try this solution.


Please sign in to leave a comment.