Add Library to Gradle Kotlin app


I converted simple Kitlon file into Library, the file is:


package hello  

fun main(args: Array<String>) {   
   println("Hello World!")        

had been compiled into library using the command:

kotlinc Display.kt -d Display.jar

The output had been cross checked to be worked using the command:

kotlin -classpath Display.jar hello.DisplayKt

Then I moved it to folder src/main/resources, then tried calling it from another app, using the below code:


package hello 

import hello.DisplayKt

fun main(args: Array<String>) {   

and defined the build.gradle file as below (tried to put all option I read about to solve my case):

// set up the kotlin-gradle plugin
buildscript {
    ext.kotlin_version = '1.1.2-2'

    repositories {

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// apply the kotlin-gradle plugin
apply plugin: "kotlin"

// add kotlin-stdlib dependencies.
repositories {

dependencies {
    //dependencies from a remote repositor
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //local file
    compile files('src/main/resources/Display.jar')
    compile fileTree(dir: 'src/main/resources', include: '*.jar')

jar {
    manifest {
        //Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
        attributes ('Main-Class': 'hello.HelloKt', "Implementation-Title": "Gradle",
                   "Implementation-Version": 1)

    // NEW LINE HERE !!!
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

sourceSets {
    main {
        java {
            srcDirs = ['src/kotlin']
        resources {
            srcDirs = ['src/resources']

but, after running gradle build command, I got the below error:

Unresolved reference: DisplayKt

Note: I’m very very new to JAVA/KOTLIN and GRADLE


Your jar task takes the wrong configuration. Instead use the [official application plugin]
( and it will take care of things for you. It will even generate a launch script and fat jar (if desired).


Gave the same error :frowning:


You’re trying to import ‘DisplayKt’, which is a generated Java class which doesn’t exist from the Kotlin point of view. Instead, you need to import the functions or classes that you use directly. If you use an IDE to develop Kotlin code, it will generate correct imports for you automatically.


Why are you doing it this way? This is not the correct way to handle dependencies in Gradle. You should be using project dependencies. Set up a correct multi-projet Gradle build first before you try to fix things in Kotlin code.

Once you have done that, and assuming that you actually don’t have 2 functions called main, use the correct import as @yole says. If you have 2 functions with the same name, use the fully-qualified name instead of an import. But with the example you gave above even a fully-qualified name won’t work, because for both Display.kt and Hello.kt the fully-qualified name of main is hello.main.


I tried not to use ‘main’ function in the library, but the Kotlin compiler refused to compile it, so I got forced to use it, and ended up having 2 main functions.
I tried to import ‘hello.DisplayKt.main’ but got the same error.
I already included the dependencies in the cradle as shown in my code, but nothing.

As I mentioned, I’m very new to Java, Gradle and Kotlin, so may I did not get exactly what you want to say, can you pls provide some code. Thanks


I use VS code, and tried to import the full path of the function, which is ‘main’ but still getting the same error.


The full path of the function is “hello.main”. As I said, the DisplayKt name does not exist in Kotlin code.

Please consider using IntelliJ IDEA, it gives far more assistance for Kotlin code than VS Code.


Thanks, it’s fine now, but I got confused about the DisplayKt, as it run with me once I used Kotlin -classpath Display.jar hello.DisplayKt