在下面的Kotlin/JVM System.exit(-1)程序中,使用错误退出代码停止程序的执行:
fun main(args: Array<String>) {
if (args.size < 2) {
println("too few args!")
System.exit(-1)
}
println("Hello, ${args[1]} from ${args[0]}")
}
Run Code Online (Sandbox Code Playgroud)
Kotlin/Native无权访问任何Java类,包括System.那么Kotlin/Native程序使用错误代码停止执行程序的等效函数是什么?
我正在尝试在私有CocoaPod中添加使用Kotlin/Native构建的销售框架,但是我收到错误:
pod repo push myCocoapodsRepo myProject.podspec --verbose"[iOS] xcodebuild: fatal error: lipo: input file (/Users/jeandaube/Library/Developer/Xcode/DerivedData/App-auugdpsmbbpvarfzghxatkvwftsn/Build/Products/Release-iphonesimulator/App.app/Frameworks/MyProject.framework/MyProject) must be a fat file when the -remove option is specified
我是否应该以某种方式改变我首先使用Konan导出框架的格式?
我在Kotlin/Native中看到了另一个函数,它在Kotlin JVM或JS中不存在.它是什么?
我正在尝试从JetBrains学习Kotlin Native如何使用本教程:https : //kotlinlang.org/docs/tutorials/native/interop-with-c.html
但是这些教程仅适用于macOS和Linux。有时我使用Windows计算机,但我想知道它在那里的工作方式。
如何在Windows中添加libcurl库,如何将其添加到libcurl.def文件中?
在任何地方都没有解释。
我创建了 Kotlin 原生项目以在 iOS 和 android 之间共享代码。我对 cocoapods 进行了集成,以便使用 POD 文件在 iOS 项目中使用,项目在 iOS 和 Android 上成功运行,但是当我尝试在 Kotlin 原生项目中使用 iOS pod 库时,我开始收到以下错误。
我知道我必须先从 Xcode 运行 pod install 才能在 Kotlin 本机项目中编译库。
所以 iOS pod 库应该通过 cinterop 进行转换,以便在 Kotlin Native 项目中使用。
我运行下面的命令只是为了检查框架是否成功编译。
./gradlew :SharedCode:packForXCode
得到这个错误
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':SharedCode:cinteropAFNetworkingIos'.
> Cannot perform cinterop processing for AFNetworking: cannot determine headers location.
Probably the build is executed from command line.
Note that a …Run Code Online (Sandbox Code Playgroud) android kotlin gradle-kotlin-dsl kotlin-native kotlin-multiplatform
我正在尝试理解Kotlin源代码在编译时经历的旅程.文件说明
当针对JVM时,Kotlin生成Java兼容的字节码.当针对JavaScript时,Kotlin会转向ES5.1并生成与包括AMD和CommonJS在内的模块系统兼容的代码.当定位本机时,Kotlin将生成特定于平台的代码(通过LLVM).
我的理解是,当Kotlin瞄准JVM时,代码被编译/转换为字节码,然后JVM将其解释为机器代码.这是JIT(及时)编译的一个例子吗?
当定位javascript时,使用单词"transpiles".编译到底的代码究竟是什么,并且在任何步骤中进一步解释或编译?
当定位本机时,代码是直接编译到机器代码吗?LLVM采取了哪些步骤?
最后,这是否意味着Kotlin既是编译语言又是解释语言?
我正在开发一个支持 JVM、iOS 和 macOS 的 Kotlin/Native 多平台项目。我的设置有以下模块:
- common
- ios
- jvm
- macos
Run Code Online (Sandbox Code Playgroud)
我想使用一些本机代码作为actual类并将一个expected类放入common. 但是,对于多个目标(iOS 和 macOS),实际的类实现是相同的。有没有办法可以设置我的源代码(也许在 Gradle 中),这样我就不必维护实际类的 2 个相同副本?
我正在探索 Kotlin Native,并且有一个程序,其中有一群Workers执行并发操作(在 Windows 上运行,但这是一个普遍问题)。
现在,我想添加简单的日志记录。一个组件,它通过将字符串作为新行附加到以“附加”模式保持打开的文件中来简单地记录字符串。
(理想情况下,我只有一个“全局”功能......
fun log(text:String) {...} ]
Run Code Online (Sandbox Code Playgroud)
...我可以从任何地方打电话,包括从其他工作人员“内部”打电话,这会起作用。这里的含义是,由于 Kotlin Native 关于在线程之间传递对象的规则,执行此操作并不简单(TLDR:您不应该传递可变对象。请参阅: https: //github.com/JetBrains/kotlin-native/blob /master/CONCURRENCY.md#对象传输和冻结)。另外,我的日志函数理想地接受任何冻结的对象。)
我想出的是使用DetachedObjectGraph 的解决方案:
首先,我创建一个分离的记录器对象
val loggerGraph = DetachedObjectGraph { FileLogger("/foo/mylogfile.txt")}
Run Code Online (Sandbox Code Playgroud)
然后使用loggerGraph.asCPointer()(asCPointer())获取COpaquePointer分离图:
val myPointer = loggerGraph.asCPointer()
Run Code Online (Sandbox Code Playgroud)
现在我可以将此指针传递给工作人员(通过工作人员的执行函数的生产者 lambda),并在那里使用它。或者我可以将指针存储在@ThreadLocal全局变量中。
对于写入文件的代码,每当我想要记录一行时,我都必须DetachedObjectGraph再次从指针创建一个对象,attach()以便获取对我的 fileLogger 对象的引用:
val fileLogger = DetachedObjectGraph(myPointer).attach()
Run Code Online (Sandbox Code Playgroud)
现在我可以在记录器上调用日志函数:
fileLogger.log("My log message")
Run Code Online (Sandbox Code Playgroud)
这就是我在查看 Kotlin Native 中可用于并发的 API(从 Kotlin 1.3.61 开始)时想到的,但我想知道更好的方法是什么(使用 Kotlin,而不是诉诸 C …
我们假设一个 KMP 项目设置为具有示例 iOS 应用程序,其中添加了 KMP 模块的输出框架作为依赖项。
我在 KMP 模块中有一个函数sampleFuncForStringArrayList(names: ArrayList<String>),可以打印计数、迭代和打印 ArrayList 项目。
当我从 iOS 示例应用程序调用此函数时,我收到索引越界异常,因为 NSMutableArray在 iOS 应用程序环境中count为2,而在 KMP 模块中作为 ArrayList 接收时count为24576 。
此问题仅发生在releaseFramework 中。debugFramework工作正常。
//Swift
let namesStringList = NSMutableArray(array: ["Alice", "Bob"])
print("NSMutableArray COUNT : \(namesStringList.count)")
Main().sampleFuncForStringArrayList(names: namesStringList)
//Kotlin
public class Main {
public fun sampleFuncForStringArrayList(names: ArrayList<String>){
println("names.isNullOrEmpty() ${names.isNullOrEmpty()}")
println("names.count ${names.count()}")
names.forEach {
println("Hello $it")
}
}
}
Run Code Online (Sandbox Code Playgroud)
预期输出
NSMutableArray COUNT : 2
names.isNullOrEmpty() false
names.count 2
Hello …Run Code Online (Sandbox Code Playgroud) 我读过这些 SO 帖子1、2、3,它们也面临着类似的问题。.klib我正在尝试在我的 KMM Android 项目中使用 a 。Klib 是从library.hC 头文件构建的。这就是我所做的:
在 KMM 项目中使用以下 Gradle 块shared:
kotlin {
...
androidNativeArm64 { // target
compilations.getByName("main") {
val mylib by cinterops.creating {
defFile(project.file("mylib.def"))
packageName("c.mylib")
// Options to be passed to compiler by cinterop tool.
compilerOpts("-I/home/me/CLionProjects/mylib/")
// Directories for header search (an analogue of the -I<path> compiler option).
includeDirs.allHeaders("/home/me/CLionProjects/mylib/")
// A shortcut for includeDirs.allHeaders.
includeDirs("/home/me/CLionProjects/mylib/")
}
}
binaries { …Run Code Online (Sandbox Code Playgroud) gradle android-studio kotlin-native kotlin-multiplatform kmm