我正在用Kotlin本地人和iOS做实验。我尝试以雷文德利希上的示例作为起点。这个例子有点老,所以我更新了代码,以适应Kotlin多平台1.3.61。我正在使用AppCode构建代码。
我正在处理Kotlin文件( build.gradle.kts),这个例子是使用build.gradle:
plugins {
id "org.jetbrains.kotlin.platform.native" version "1.3.0"
}
components.main {
def productsDir = new File("").absolutePath
targets = ['ios_arm64', 'ios_x64']
outputKinds = [EXECUTABLE]
allTargets {
linkerOpts '-rpath', '@executable_path/Frameworks'
linkerOpts "-F${productsDir}"
}
dependencies {
cinterop('AFNetworking'){
packageName 'com.afnetworking'
compilerOpts "-F${productsDir}"
linkerOpts "-F${productsDir}"
includeDirs{
allHeaders "${productsDir}/AFNetworking.framework/Headers"
}
}
}
}
task copyExecutable() {
doLast {
def srcFile = tasks['compileDebugIos_x64KotlinNative'].outputFile
def targetDir = getProperty("konan.configuration.build.dir")
copy {
from srcFile.parent
into targetDir
}
}
} 我尝试将其转换为我自己的build.gradle.kts文件:
plugins {
kotlin("multiplatform") version "1.3.61"
kotlin("xcode-compat") version "0.2.5"
}
repositories {
mavenCentral()
}
kotlin {
val productsDir = "/Users/trond/Desktop/native_meteor/native_meteor/"
iosX64("ios")
{
compilations.getByName("main")
{
val myInterop by cinterops.creating {
defFile(project.file("src/nativeInterop/cinterop/afnetworking.def"))
packageName ("com.afnetworking")
compilerOpts ("-F${productsDir}")
// linkerOpts ("-F${productsDir}")
// 3
includeDirs{
allHeaders ("${productsDir}/AFNetworking.framework/Headers")
}
}
}
}
xcode {
setupApplication("native_meteor")
}
}据我所见,cinterops工具正在做它的工作,并且正在创建一个klib文件和一个afnetworing.kt (build/classes/kotlin/ios/main/.)
但是我不能使用库AFNetworking!我尝试在ViewController.kt文件中添加导入指令:
import com.afnetworking.*但这一点没有得到承认。因此,该项目没有建立:
> Task :native_meteor:compileKotlinNative_meteor FAILED
e: /Users/trond/Desktop/native_meteor/native_meteor/src/native_meteorMain/kotlin/ViewController.kt: (15, 8): Unresolved reference: com
e: /Users/trond/Desktop/native_meteor/native_meteor/src/native_meteorMain/kotlin/ViewController.kt: (37, 23): Unresolved reference: AFHTTPSessionManager
e: /Users/trond/Desktop/native_meteor/native_meteor/src/native_meteorMain/kotlin/ViewController.kt: (40, 38): Unresolved reference: AFJSONResponseSerializer 有谁能弄清楚这件事吗?
发布于 2020-01-10 12:33:56
也许检查结果.klib的内容以确保使用正确的包名是有意义的。它可以使用~/.konan/kotlin-native-macos-1.3.61/bin/klib CLI工具来完成。
另外,我建议您查看一下Kotlin/Native中的这示例,它还利用了AFNetworking并使用了最新的编译器版本。
发布于 2020-01-13 14:00:00
谢谢你指点我这个方向。我研究了.klib文件,发现它包含了我期望的包。然后,我查看了我在Appcode的Gradle窗格中拥有的Gradle源代码集。在这里,我发现.klib库是在"iosMain“而不是"native_metorMain”下定义的。然后我发现行iosX64("ios")应该是iosX64("meteor_main")。
在此之后,当我试图在AFNetworking中编译时,我得到了一个错误:"ld: framework“。然后,我在-F文件中为linkerOpts ( linkerOpts= -F{ framwork} -framework AFNetworking )添加了afnetworking.def {满路径到框架}。
现在,该项目成功构建。
https://stackoverflow.com/questions/59628344
复制相似问题