Creating/Importing iOS 的 Kotlin 多平台代码
Creating/Importing Kotlin multiplatform code for iOS
我正在试用 kotlin multiplatform,我在 Android 中构建和测试了公共部分并且工作正常,但现在我正在尝试在 iOS 中实现代码以进行试用。我在 iOS 的 build.gradle
中看到 gradle 任务。
// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
def target = project.findProperty('kotlin.target') ?: 'ios'
dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask
doLast {
def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
def targetDir = getProperty('configuration.build.dir')
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
它说不直接 运行 任务但是根据 this guide 它说直接 运行 它。当我直接 运行 时,我收到一条错误消息
Execution failed for task ':app:copyFramework'.
Could not get unknown property 'configuration.build.dir' for task ':app:copyFramework' of type org.gradle.api.DefaultTask.
我不知道要为 iOS.
构建它需要做什么
谁能帮我解决这个问题?
首先,让我们弄清楚这里发生了什么。由于您必须使用 Xcode 项目来实现 iOS 的最终应用程序,因此应该在构建时以某种方式将框架添加到那里。您在这里混合了两种方法:
copyFramework
任务,应该是 运行 作为 Xcode 构建过程的构建阶段。它将生成的框架复制到 Frameworks 目录,以便从 Objective-C 或 Swift 代码访问它。要了解它的位置,此任务取决于环境变量,即 Xcode 在执行构建时设置的环境变量。这就是你在这里出错的原因。
- 指南中的任务与第一个略有不同。这里最主要的是
packForXcode
将框架复制到指定目录 ../../SharedCode/build/xcode-frameworks
,并且 Xcode 项目已经设置为从那里获取框架(参见 this line)。因此任务不依赖Xcode构建过程,可以直接执行。
我正在试用 kotlin multiplatform,我在 Android 中构建和测试了公共部分并且工作正常,但现在我正在尝试在 iOS 中实现代码以进行试用。我在 iOS 的 build.gradle
中看到 gradle 任务。
// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
def target = project.findProperty('kotlin.target') ?: 'ios'
dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask
doLast {
def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
def targetDir = getProperty('configuration.build.dir')
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
它说不直接 运行 任务但是根据 this guide 它说直接 运行 它。当我直接 运行 时,我收到一条错误消息
Execution failed for task ':app:copyFramework'.
Could not get unknown property 'configuration.build.dir' for task ':app:copyFramework' of type org.gradle.api.DefaultTask.
我不知道要为 iOS.
构建它需要做什么谁能帮我解决这个问题?
首先,让我们弄清楚这里发生了什么。由于您必须使用 Xcode 项目来实现 iOS 的最终应用程序,因此应该在构建时以某种方式将框架添加到那里。您在这里混合了两种方法:
copyFramework
任务,应该是 运行 作为 Xcode 构建过程的构建阶段。它将生成的框架复制到 Frameworks 目录,以便从 Objective-C 或 Swift 代码访问它。要了解它的位置,此任务取决于环境变量,即 Xcode 在执行构建时设置的环境变量。这就是你在这里出错的原因。- 指南中的任务与第一个略有不同。这里最主要的是
packForXcode
将框架复制到指定目录../../SharedCode/build/xcode-frameworks
,并且 Xcode 项目已经设置为从那里获取框架(参见 this line)。因此任务不依赖Xcode构建过程,可以直接执行。