4. Using Kotlin With Gradle

You can use Gradle to manage the build process for Kotlin projects. This worksheet takes you through a simple example of this, which you can use as a template for your own projects.

4.1. Working From The Command Line

  1. Download gradledemo.zip and unzip it. Then cd into the gradledemo directory and examine its contents.

    build.gradle is the Gradle buildfile that manages the project. It will ensure that basic dependencies (Kotlin and JUnit) are downloaded if necessary. You may need to add to the list of dependencies if using this as a template for your own project, and you will also need to modify the value of mainClassName. The first part of the value is the name of the package containing your code and the second part is the name of the class containing the program entry point, as generated by the Kotlin compiler.

    Source code for the application and its unit tests can be found in subdirectories of the src directory. The application is under main/kotlin/gradledemo, as the file ExamGrades.kt. The tests are under test/kotlin/gradledemo, in tests.kt. You will need to use an equivalent directory structure in your own projects, replacing gradledemo with the package name that you’ve used in the source files.

    The remaining files and directories provide the Gradle wrapper. This allows to run Gradle even it you haven’t already installed it on your system. The first time you run the wrapper, the needed files will be downloaded for you.

  2. To see the tasks supported by the buildfile, do

    ./gradlew tasks

    (If you are doing this on a Windows system, omit the leading ./.)


    This may be quite slow the first time it runs, since it may need to download Gradle support files and other dependencies, and start the Gradle daemon.

  3. Examine the program source code, then build and run the program with

    ./gradlew run

    You should see this output:

    Sarah Jones: Distinction
    Ben Smith: Pass
    David Armitage: Pass
    Amanda Benson: Distinction
    John Dobson: Fail

    Gradle should also report ‘BUILD SUCCESSFUL’.

  4. Examine the code for the unit tests, then build and run the tests with

    ./gradlew test

    Gradle should report ‘BUILD SUCCESSFUL’, indicating that the code compiled and all the tests passed. Gradle also generates an HTML report on the tests. You can view this by opening the file build/reports/tests/test/index.html in a browser.


    Part of the report generated by Grade’s ‘test’ task

  5. Try generating a standalone JAR file containing the application, like so:

    ./gradlew jar

    This should produce a JAR file in build/libs, which you can run using

    java -jar gradledemo.jar
  6. Finally, you can clean up the project using

    ./gradlew clean

    This will remove the build subdirectory entirely.

4.2. Importing Into IntelliJ

The gradledemo project also supports the use of IntelliJ.

  1. Run IntelliJ and choose ‘Import Project’ from the Welcome screen, then select the gradledemo directory. On the next screen, make sure that the ‘Import project from external model’ option is selected and that Gradle is highlighted, then click Next.

    On the next screen, you can leave the import options at their defaults if you wish and click Finish.

  2. Once the project has finished importing, you can activate the Gradle tool by clicking on the relevant button on the right of the IDE window, or by choosing View ‣ Tool Windows ‣ Gradle from the menubar.

    Expand the list of tasks to find the ‘run’ and ‘test’ tasks; the former will be under ‘application’ whereas the latter will be under ‘verification’. Double-click on a task to run it. Output from Gradle will appear in a panel at the bottom of the IDE.