Gradle 构建中未解决的 Kotlin 扩展函数引用
Unresolved reference of a Kotlin extension function in the Gradle build
我有多个项目 Gradle 使用 Kotlin 编写的所有代码构建。里面有两个项目:common和client。子项目位于中间文件夹中,例如 "demo"。所以文件夹结构是:
project
demo
client
build.gradle
common
build.gradle
build.gradle
gradle.properties
settings.gradle
settings.gradle:
rootProject.name = 'demo'
include 'demo/client'
include 'demo/common'
客户端依赖于公共项目compile project(":demo/common")
。并且common项目中有扩展功能:
fun <T> List<Future<T>>.getAll(): Long {
var count = 0L
this.forEach {
it.get()
count++
}
return count
}
如果我尝试在客户端项目中使用它,我会在编译时遇到 Unresolved reference: getAll
异常。用法:
...
import org.sandbox.imdg.hazelcast.common.utils.getAll
class CassLoader {
fun loadCalcData(): Long {
...
val futures: List<Future<CalcData>> = items.map { map.putAsync(it.getKey(), it) }
return futures.getAll()
}
}
同时,如果我将 getAll
声明放在客户端项目中,一切都可以正常编译。我很确定函数的导入是正确的,但同时我对 Gradle 有一点经验,所以可能会遗漏一些东西。
- 构建:
gradlew clean build
- 科特林:1.0.2
- Gradle: 2.9
UPD:问题出在文件夹结构中(或者可能是我声明的依赖项错误...)- 如果我删除中间文件夹演示,一切正常。
检查您是否将两个项目都包含在根目录中 settings.gradle
。
使用最少的工作设置检查 this Gist。
settings.gradle:
rootProject.name = 'demo'
include 'client'
include 'common'
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
subprojects {
apply plugin: 'kotlin'
repositories {
jcenter()
maven {
url "http://repository.jetbrains.com/all"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}
}
common/src/main/kotlin/by/dev/madhead/demo/common/Funktions.kt
package by.dev.madhead.demo.common
fun String.revert(): String {
return this.reversed()
}
client/build.gradle
dependencies {
compile project(':common')
}
client/src/main/kotlin/by/dev/madhead/demo/client/App.kt
package by.dev.madhead.demo.client
import by.dev.madhead.demo.common.revert
fun main(args: Array<String>) {
println("Hello".revert())
}
好的,问题出在错误的子项目包含在 gradle.settings 中。应该是
include 'demo:common'
include 'demo:client'
而不是
include 'demo/common'
include 'demo/client'
然后是依赖关系 compile project(":demo:common")
。
我有多个项目 Gradle 使用 Kotlin 编写的所有代码构建。里面有两个项目:common和client。子项目位于中间文件夹中,例如 "demo"。所以文件夹结构是:
project
demo
client
build.gradle
common
build.gradle
build.gradle
gradle.properties
settings.gradle
settings.gradle:
rootProject.name = 'demo'
include 'demo/client'
include 'demo/common'
客户端依赖于公共项目compile project(":demo/common")
。并且common项目中有扩展功能:
fun <T> List<Future<T>>.getAll(): Long {
var count = 0L
this.forEach {
it.get()
count++
}
return count
}
如果我尝试在客户端项目中使用它,我会在编译时遇到 Unresolved reference: getAll
异常。用法:
...
import org.sandbox.imdg.hazelcast.common.utils.getAll
class CassLoader {
fun loadCalcData(): Long {
...
val futures: List<Future<CalcData>> = items.map { map.putAsync(it.getKey(), it) }
return futures.getAll()
}
}
同时,如果我将 getAll
声明放在客户端项目中,一切都可以正常编译。我很确定函数的导入是正确的,但同时我对 Gradle 有一点经验,所以可能会遗漏一些东西。
- 构建:
gradlew clean build
- 科特林:1.0.2
- Gradle: 2.9
UPD:问题出在文件夹结构中(或者可能是我声明的依赖项错误...)- 如果我删除中间文件夹演示,一切正常。
检查您是否将两个项目都包含在根目录中 settings.gradle
。
使用最少的工作设置检查 this Gist。
settings.gradle:
rootProject.name = 'demo'
include 'client'
include 'common'
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
subprojects {
apply plugin: 'kotlin'
repositories {
jcenter()
maven {
url "http://repository.jetbrains.com/all"
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}
}
common/src/main/kotlin/by/dev/madhead/demo/common/Funktions.kt
package by.dev.madhead.demo.common
fun String.revert(): String {
return this.reversed()
}
client/build.gradle
dependencies {
compile project(':common')
}
client/src/main/kotlin/by/dev/madhead/demo/client/App.kt
package by.dev.madhead.demo.client
import by.dev.madhead.demo.common.revert
fun main(args: Array<String>) {
println("Hello".revert())
}
好的,问题出在错误的子项目包含在 gradle.settings 中。应该是
include 'demo:common'
include 'demo:client'
而不是
include 'demo/common'
include 'demo/client'
然后是依赖关系 compile project(":demo:common")
。