This is a hands-on exercise to go along with the Introduction to Gradle Build Tool for Developers training module. In this exercise you will go over the following:
- See dependencies for a project
- Adding dependencies
- Examining dependency conflict resolution
- Completed the first and second hands-on exercise
- You can perform the exercises in the same Gradle project used in previous exercises
- Open the terminal run
./gradlew :app:dependencies. You will see dependencies grouped by configurations.
- To see the dependencies of just the runtimeClasspath configuration run
./gradlew :app:dependencies --configuration=runtimeClasspath
Let's add some more functionality to our application. Currently it only prints a greeting message. Let's make it fetch some data from a url and print it. We will use the Google Http Client for this.
We need to get the Gradle dependency statement for this library. Follow the steps below to get this:
-
In the search bar type
google http client -
One of the top results will be
com.google.http-client » google-http-client, click on it, and then click on the1.44.1version -
Click on the
Gradle (Kotlin)tab -
Copy just the module ID and version, not the dependency configuration
-
Open the version catalog file which is
gradle/libs.versions.toml -
Under the
librariessection, add an entry calledgoogle-http-clientand set it to the copied contents:
google-http-client = "com.google.http-client:google-http-client:1.44.1"- If using IntelliJ, hit the Gradle refresh icon
- In
app/build.gradle.kts, in thedependenciessection add animplementationdependency tolibs.google.http.client. Notice that instead of dashes you will use dots:
implementation(libs.google.http.client)
- Run
./gradlew :app:dependencies --configuration=runtimeClasspathagain and notice the additional dependencies - both direct and transitive.
- Notice in the dependencies, the
google-http-clientlibrary depends onguava, which is also a direct dependency.
- The
guavaversiongoogle-http-clientdepends on is older than the version in the direct dependency, so you will see Gradle handles the conflict by using the highest version of theguavalibrary.
- Open the
app/src/main/java/com/gradle/lab/App.javafile and replace the contents after the package declaration with the following:
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.javanet.NetHttpTransport;
import java.io.IOException;
public class App {
public String getGreeting() {
return "Hello World!";
}
public String getUrl() {
// This is a small website and easily prints.
return "https://wiby.me/";
}
public static void main(String[] args) throws IOException {
App app = new App();
System.out.println(app.getGreeting());
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(app.getUrl()));
String rawResponse = request.execute().parseAsString();
System.out.println("\n---------\n");
System.out.println(rawResponse);
}
}- Then open the terminal and execute
./gradlew :app:run, notice the website contents are also printed.
If you get stuck you can refer to the project files in this repository.








