You need to integrate a third-party library in your Gradle project. What steps would you follow?

Integrating a third-party library into a Gradle project means locating its Maven coordinates, declaring it as a dependency with the appropriate configuration, and ensuring a repository that hosts it is registered. Gradle then handles downloading and resolving it, along with its own transitive dependencies, automatically.

Key Points: • Find the library's coordinates — group, artifact, and version (GAV) — usually from Maven Central or the vendor's documentation. • Add the dependency in the dependencies block using implementation (internal use) or api (exposed to consumers of your own library). • Ensure the repositories block includes a repository that actually hosts the artifact, such as mavenCentral() or a vendor-specific repository. • Run gradle build (or sync in an IDE) to trigger Gradle to fetch and cache the dependency locally. • Check the library's transitive dependencies with dependencyInsight if it introduces unexpected version conflicts.

Example: To add Google's Gson library, you'd add implementation 'com.google.code.gson:gson:2.10.1' to the dependencies block, confirm mavenCentral() is in the repositories block, and then simply import and use Gson classes once Gradle syncs.

Code Example:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.google.code.gson:gson:2.10.1'
}

Interview Tip: A concise interview answer is:

"I'd find the library's Maven coordinates, add it to the dependencies block with implementation or api depending on whether it should be exposed to consumers, and make sure the right repository is registered in the repositories block. Then gradle build handles fetching and resolving it, including its own transitive dependencies."