Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin gradle build Unresolved reference to kotlin-stdlib functions

I have the following build.gradle file for my package

buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
        jcenter()
        mavenCentral()
    }

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

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.2.21"
}

sourceSets {
    main.kotlin.srcDirs += "./"
}

On running gradle compileKotlin I get the following errors

Starting a Gradle Daemon (subsequent builds will be faster)                                                                                                                                                           
e: /home/ishan/code/kotlin-server/server/ConnectionHandler.kt: (26, 35): Too many arguments for public constructor String() defined in kotlin.String                                                                  
e: /home/ishan/code/kotlin-server/server/ConnectionHandler.kt: (26, 38): Unresolved reference: trim                                                                                                                   
e: /home/ishan/code/kotlin-server/server/ConnectionHandler.kt: (30, 29): Unresolved reference: isEmpty                                                                                                                
e: /home/ishan/code/kotlin-server/server/ConnectionHandler.kt: (40, 9): Unresolved reference: print                                                                                                                   
e: /home/ishan/code/kotlin-server/server/ConnectionHandler.kt: (41, 9): Unresolved reference: print                                                                                                                   
e: /home/ishan/code/kotlin-server/server/ConnectionHandler.kt: (45, 9): Unresolved reference: println                                                                                                                 
e: /home/ishan/code/kotlin-server/server/HttpRequest.kt: (8, 19): Unresolved reference: HashMap                                                                                                                       
e: /home/ishan/code/kotlin-server/server/HttpRequest.kt: (16, 38): Unresolved reference: split                                                                                                                        
e: /home/ishan/code/kotlin-server/server/HttpResponse.kt: (22, 31): Unresolved reference: toByteArray                                                                                                                 
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (3, 22): Unresolved reference: HashMap                                                                                                                   
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (3, 45): Unresolved reference: hashMapOf                                                                                                                 
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (5, 9): Unresolved reference: to                                                                                                                         
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (6, 9): Unresolved reference: to                                                                                                                         
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (7, 9): Unresolved reference: to                                                                                                                         
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (8, 9): Unresolved reference: to

When I use kotlinc -include-runtime command to compile this package, it compiles without any issues and I even have a working .jar file for it.

What am I missing here? If you want to inspect more, I've also created a github issue for this with links to the related code.

like image 901
Ishan Khare Avatar asked Feb 25 '18 11:02

Ishan Khare


2 Answers

As already mentioned, the dependency has to be put in the dependencies block outside buildscript. However, you also have to specify the version of the stdlib to use:

buildscript{
    ...
}
dependencies{
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
like image 43
Zoe stands with Ukraine Avatar answered Sep 28 '22 07:09

Zoe stands with Ukraine


You need to put this dependency:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib"
}

at the top-level, not inside the buildscript:

buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
        jcenter()
        mavenCentral()
    }

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

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.2.21"
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib"
}

sourceSets {
    main.kotlin.srcDirs += "./"
}
like image 130
guido Avatar answered Sep 28 '22 07:09

guido