Kotlin gradle构建对kotlin-stdlib函数的未解析引用

时间:2018-02-25 11:46:18

标签: gradle kotlin build.gradle

我的包

有以下build.gradle文件
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 += "./"
}

在运行gradle compileKotlin时,我收到以下错误

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

当我使用kotlinc -include-runtime命令编译这个包时,它编译没有任何问题,我甚至有一个工作.jar文件。

我在这里缺少什么?如果您想要进行更多检查,我还为此创建了github issue,其中包含指向相关代码的链接。

2 个答案:

答案 0 :(得分:3)

你需要把这种依赖:

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

位于顶层,而不是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 += "./"
}

答案 1 :(得分:1)

如前所述,依赖项必须放在buildscript之外的依赖项块中。但是,您还必须指定要使用的stdlib的版本:

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