The build.gradle file is the primary build script for a Gradle project, defining what plugins are applied, where dependencies are fetched from, what dependencies the project needs, and any custom tasks or configuration. It's the equivalent of pom.xml in Maven but written as an executable Groovy or Kotlin script rather than declarative XML.
Key Points: • The plugins block applies functionality like the java or application plugin, which brings in standard tasks (compileJava, test, jar, etc.). • The repositories block declares where dependencies should be resolved from, such as mavenCentral() or a private repository. • The dependencies block lists the libraries the project needs, scoped by configuration (implementation, testImplementation, api, etc.). • Because it's a script, build.gradle can contain arbitrary logic, custom tasks, and conditional configuration, unlike Maven's purely declarative XML. • In multi-project builds, each subproject typically has its own build.gradle alongside a root one that holds shared configuration.
Example: A typical Java library's build.gradle applies the java plugin, points repositories at mavenCentral(), and lists JUnit under testImplementation — three concise blocks that together fully describe how the project is compiled, tested, and packaged.
Code Example:
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'com.google.guava:guava:33.0.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'
}Interview Tip: A concise interview answer is:
"build.gradle defines everything about how a project is built: the plugins block adds capabilities, repositories declares where to fetch dependencies from, and dependencies lists what the project needs. Unlike Maven's XML, it's an actual script, so it can also hold custom tasks and conditional logic beyond static declarations."