Ensuring consistent dependency versions across multiple Gradle modules means centralizing the version declaration in one place — either the root build.gradle's subprojects block or a shared version catalog — so every module inherits the same version instead of declaring it independently. This avoids drift and version conflicts between modules.
Key Points: • A subprojects block in the root build.gradle can declare a shared dependency, applying it identically to every subproject. • A version catalog (gradle/libs.versions.toml) is the more modern approach, defining versions and aliases centrally that modules reference via libs.someLibrary. • Centralizing versions means bumping a library version in one place updates it consistently across the entire project. • platform() / BOM-style dependencies (similar to Maven's dependencyManagement) can also be used to align versions across modules without forcing every module to use the same library. • This approach reduces the risk of subtle bugs caused by two modules unknowingly running different versions of the same library at runtime.
Example: Instead of every module separately declaring implementation 'com.example:library:1.2.3', defining it once inside a subprojects block in the root build.gradle guarantees all current and future modules automatically use that exact version.
Code Example:
// root build.gradle
subprojects {
dependencies {
implementation 'com.example:library:1.2.3'
}
}Interview Tip: A concise interview answer is:
"I'd centralize the dependency version either in a subprojects block in the root build.gradle or, more commonly today, in a version catalog like libs.versions.toml, so every module references the same version by alias. That way a version bump happens in one place and automatically applies consistently across all modules."