我将简单的Kitlon文件转换为库,文件为:
Display.kt
:
package hello
fun main(args: Array<String>) {
println("Hello World!")
}
已使用以下命令编译到库中:
kotlinc Display.kt -d Display.jar
已使用以下命令交叉检查输出:
kotlin -classpath Display.jar hello.DisplayKt
然后我将其移至文件夹src/main/resources
,然后尝试使用以下代码从其他应用调用它:
Hello.kt
:
package hello
import hello.DisplayKt
fun main(args: Array<String>) {
println("Hi")
}
并定义了build.gradle
文件,如下所示(试图将我读到的所有选项都用于解决我的案例):
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
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']
}
}
}
但是,在运行gradle build
命令后,我收到以下错误:
Unresolved reference: DisplayKt
注意:我对JAVA / KOTLIN和GRADLE非常陌生
答案 0 :(得分:1)
我找到了答案,该函数的完整路径为hello.main