razzi.abuissa.net

Razzi's guide to gradle

Gradle is a build tool frequently used by Java projects.

It’s nice because it handles dependency downloading automatically. It’ll even download the version of gradle specified by your project, which is quite nice since it solves the bootstrapping problem of needing to download a specific version of a package manager to get the package manager in the first place. Cool!

note on ubuntu version

Gradle on debian/ubuntu is super out of date as of 2026, it’s version 4.4 which was released in 2017. There are ways to install it outside of apt, like sdkman and snap, but hopefully me or somebody else can make apt install gradle get a reasonably up-to-date version up again! 4.4 is so old it can’t even bootstrap newer gradle versions like 9. 4.4 doesn’t run with recent java versions either.

initializing a gradle project

$ gradle init

If you have a recent enough gradle version, it’ll ask you for what type of project you want.

If you choose application, you can run it like so:

$ ./gradlew run
Reusing configuration cache.

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 264ms
2 actionable tasks: 1 executed, 1 up-to-date
Configuration cache entry reused.

It’ll give you the choice of Groovy or Kotlin for the gradle build file. Most examples you see online are using Groovy, which is a lot like Java but with a lighter syntax, not requiring semicolons or as many parenthesis.

Kotlin also doesn’t require semicolons and has a lot of niceties, but it’s newer, so most settings.gradle are still written in Groovy. Note that if you’re using Kotlin, the settings will go into a settings.gradle.kts, which shows Groovy is still very much the default.

adding a java dependency

Let’s say you want to make some http requests using the okhttp http client library.

You can add the dependency to your app/build.gradle like so:

dependencies {
    // ... there may be other dependencies already
    implementation("com.squareup.okhttp3:okhttp:5.3.2")
}

From there you can follow the documentation example here and gradle will download the okhttp dependency before running your app automatically.

You’ll end up with a source that looks like:

app/src/main/java/org/example/App.java

package org.example;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class App {
    public static void main(String[] args) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url("http://example.com").build();
        try (Response response = client.newCall(request).execute()) {
          System.out.println(response.body().string());
        }
    }
}

Which you can run like so:

$ ./gradlew run
Reusing configuration cache.

> Task :app:run
<!doctype html><html lang="en"><head><title>Example Domain</title>...

a note about version control and the gradle wrapper

Gradle initializes a jar file in gradle/wrapper/gradle-wrapper.jar.

It’s strange to me but they expect you to track that in source code! Even though it’s a binary… see this article.

There is an open issue to get gradle to bootstrap its own wrapper: https://github.com/gradle/gradle/issues/11816. There is a bit of resistance to this idea because it could install and run a malicious executable. I hope eventually they work out the issues and you can just track the gradlew script, and the gradle.bat if your application supports windows environments.

source code

https://github.com/gradle/gradle