Creating a Basic Multi-Project Build
Declaring Dependencies between Subprojects
Sharing Build Logic between Subprojects
Fine Tuning the Project Layout
Understanding Configuration and Execution
Structuring Software Products
Structuring Large Projects
Tweaking Project Structure
Composing Builds
Authoring Sustainable Builds
Organizing Build Logic
Following Best Practices
Developing Gradle Tasks
Authoring Tasks
Incremental build
Writing Gradle Task Types
Configuring Tasks Lazily
Avoiding Unnecessary Task Configuration
Developing Parallel Tasks
Developing Gradle Plugins
Starting Plugin Development
Designing Plugins
Implementing Plugins
Testing Plugins
Publishing Plugins
Other Developing Gradle Topics
Writing Custom Gradle Types and Service Injection
Shared Build Services
Dataflow Actions
Testing a Build with TestKit
Using Ant from Gradle
Authoring JVM Builds
Building Java & JVM projects
Testing Java & JVM projects
Java Toolchains
Toolchains for JVM projects
Toolchain Resolver Plugins
Managing Dependencies
JVM Plugins
Java Library Plugin
Java Application Plugin
Java Platform Plugin
Groovy Plugin
Scala Plugin
Working with Dependencies
Terminology
Learning the Basics
What is Dependency Management?
Declaring Repositories
Declaring Dependencies
Understanding Library and Application Differences
Viewing and Debugging Dependencies
Understanding Resolution
Verifying dependencies
Declaring Versions
Declaring Versions and Ranges
Declaring Rich Versions
Handling Changing Versions
Locking Versions
Controlling Transitives
Upgrading Versions
Downgrading and Excluding
Sharing Versions
Aligning Dependencies
Handling Mutually Exclusive Dependencies
Fixing Metadata
Customizing Resolution
Preventing accidental upgrades
Producing and Consuming Variants of Libraries
Declaring Capabilities of a Library
Modeling Feature Variants and Optional Dependencies
Understanding Variant Selection
Declaring Variant Attributes
Sharing Outputs of Projects
Transforming Artifacts
Publishing Libraries
Setting up Publishing
Understanding Gradle Module Metadata
Signing Artifacts
Customizing Publishing
Maven Publish Plugin
Ivy Publish Plugin
Improving Performance of Gradle Builds
Using the Build Cache
Enabling and Configuring
Why use the Build Cache?
Understanding the Impact
Learning Basic Concepts
Caching Java Project
Caching Android Project
Debugging Caching Issues
Troubleshooting
Using the Configuration Cache
Inspecting Gradle Builds
Authoring C++/Swift Builds
Building C++ projects
Testing C++ projects
Building Swift projects
Testing Swift projects
Gradle on CI
Jenkins
TeamCity
GitHub Actions
Travis CI
Reference
Core Plugins
Gradle & Third-party Tools
User Manual PDF
Gradle comes with a built-in task, called
init
, that initializes a new Gradle project in an empty folder.
The
init
task uses the (also built-in)
wrapper
task to create a Gradle wrapper script,
gradlew
.
The first step is to create a folder for the new project and change directory into it.
$ mkdir demo
$ cd demo
From inside the new project directory, run the
init
task using the following command in a terminal:
gradle init
.
When prompted, select the
3: library
project type and
4: Kotlin
as implementation language.
Next you can choose the DSL for writing buildscripts -
1 : Groovy
or
2: Kotlin
.
For the other questions, press enter to use the default values.
The output will look like this:
$ gradle init
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 3
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 4
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1
Project name (default: demo):
Source package (default: demo):
BUILD SUCCESSFUL
2 actionable tasks: 2 executed
The
init
task generates the new project with the following structure:
├── gradle (1)
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew (2)
├── gradlew.bat (2)
├── settings.gradle.kts (3)
└── lib
├── build.gradle.kts (4)
└── src
├── main
│ └── kotlin (5)
│ └── demo
│ └── Library.kt
└── test
└── kotlin (6)
└── demo
└── LibraryTest.kt
rootProject.name
assigns a name to the build, which overrides the default behavior of naming the build after the directory it’s in.
It’s recommended to set a fixed name as the folder might change if the project is shared - e.g. as root of a Git repository.
include("lib")
defines that the build consists of one subproject called
lib
that contains the actual code and build logic.
More subprojects can be added by additional
include(…)
statements.
Our build contains one subproject called
lib
that represents the Kotlin library we are building.
It is configured in the
lib/build.gradle(.kts)
file:
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
(4)
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.2")
(5)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
api("org.apache.commons:commons-math3:3.6.1")
(6)
implementation("com.google.guava:guava:31.1-jre")
(7)
tasks.named<Test>("test") {
useJUnitPlatform()
(8)
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
(4)
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
(5)
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
api 'org.apache.commons:commons-math3:3.6.1'
(6)
implementation 'com.google.guava:guava:31.1-jre'
(7)
tasks.named('test') {
useJUnitPlatform()
(8)
This dependency is exported to consumers, that is to say found on their compile classpath.
This dependency is used internally, and not exposed to consumers on their own compile classpath.
Use JUnit Platform for unit tests.
class LibraryTest {
@Test fun someLibraryMethodReturnsTrue() {
val classUnderTest = Library()
assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'")
The generated test class has a single
kotlin.test
test.
The test instantiates the
Library
class, invokes a method on it, and checks that it returns the expected value.
More information about the features the
java-library
plugin adds to any JVM library project, such as API and implementation separation, can be found in the
Java Library Plugin documentation
.
The first time you run the build, Gradle will check whether or not you already have the required dependencies in your cache under your
~/.gradle
directory. If not, the libraries will be downloaded and stored there. The next time you run the build, the cached versions will be used. The
build
task compiles the classes, runs the tests, and generates a test report.
You can view the test report by opening the HTML output file, located at
lib/build/reports/tests/test/index.html
.
You can find your newly packaged JAR file in the
lib/build/libs
directory with the name
lib.jar
.
Verify that the archive is valid by running the following command:
$ jar tf lib/build/libs/lib.jar
META-INF/
META-INF/MANIFEST.MF
lib/Library.class
You should see the required manifest file —
MANIFEST.MF
— and the compiled
Library
class.
All of this happens without any additional configuration in the build script because Gradle’s
java-library
plugin assumes your project sources are arranged in a
conventional project layout
.
You can customize the project layout if you wish
as described in the user manual
.
You will often want the name of the JAR file to include the library
version
.
This is achieved by setting a top-level
version
property in the build script:
build.gradle.kts
version = "0.1.0"
build.gradle
version = '0.1.0'
Next to the version, other important identity properties of a library are it’s
name
and
group
.
The name is directly derived from the subproject name that represents the library.
It’s
lib
in the example so you probably want to adjust it by changing the name of the
lib
folder and the corresponding
include(…)
statement in the
settings.gradle(.kts)
file.
The
group
is used to give your library full coordinates when published.
You can define it directly in the build script by setting the
group
property similar to how you set the version (shown above).
You’ll notice that the resulting JAR file at
lib/build/libs/lib-0.1.0.jar
contains the version as expected.
Another common requirement is customizing the manifest file, typically by adding one or more attributes.
Let’s include the library name and version in the manifest file by
configuring the
jar
task
.
Add the following to the end of your build script:
build.gradle.kts
tasks.jar {
manifest {
attributes(mapOf("Implementation-Title" to project.name,
"Implementation-Version" to project.version))
build.gradle
tasks.named('jar') {
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
To confirm that these changes work as expected, run the jar task again, and this time also unpack the manifest file from the JAR:
$ ./gradlew jar
$ jar xf lib/build/libs/lib-0.1.0.jar META-INF/MANIFEST.MF
Now view the contents of the META-INF/MANIFEST.MF file and you should see the following:
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: lib
Implementation-Version: 0.1.0
The additional JAR will be produced as part of the assemble or build lifecycle tasks and will be part of the publication.
The resulting file is found in lib/build/libs, with a name using the conventional classifier -sources.
The best way to learn more about what your build is doing behind the scenes, is to publish a build scan.
To do so, just run Gradle with the --scan flag.
$ ./gradlew build --scan
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service.
Do you accept these terms? [yes, no] yes
Gradle Terms of Service accepted.
Publishing build scan...
https://gradle.com/s/5u4w3gxeurtd2
Click the link and explore which tasks where executed, which dependencies where downloaded and many more details!
That’s it! You’ve now successfully configured and built a Kotlin library project with Gradle.
You’ve learned how to: