【问题标题】:Unresolved reference of a Kotlin extension function in the Gradle buildGradle 构建中未解决的 Kotlin 扩展函数引用
【发布时间】:2016-05-27 09:50:41
【问题描述】:

我有使用 Kotlin 编写的所有代码的多项目 Gradle 构建。其中有两个项目: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")。并且在普通项目中有一个扩展功能:

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
  • 等级:2.9

UPD:问题出在文件夹结构中(或者可能是我声明的依赖项错误...) - 如果我删除中间文件夹 demo,则一切正常。

【问题讨论】:

  • 神奇的clean/build为我解决了这个问题

标签: gradle extension-methods kotlin


【解决方案1】:

好的,问题出在 gradle.settings 中包含的错误子项目中。应该是

include 'demo:common'
include 'demo:client'

而不是

include 'demo/common'
include 'demo/client'

然后是依赖compile project(":demo:common")

【讨论】:

    【解决方案2】:

    检查您是否将这两个项目都包含在 root 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())
    }
    

    【讨论】:

    • 仍然没有成功。如果扩展在客户端子项目中,一切都编译得很好,然后当我在客户端子项目中移动它时会中断。这两个子项目都包含在 settings.gradle 中。更新:但是您的设置一切正常 - 我应该检查是否有任何差异。
    • 问题出在项目的结构上。我在项目中有一个中间文件夹(比如“demo”)以及其中的 common 和 client 文件夹。更新了问题以反映这一事实。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2018-11-21
    • 2021-02-17
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多