简体   繁体   中英

Creating/Importing Kotlin multiplatform code for iOS

I am trying out kotlin multiplatform and I have the common portion build and tested in Android and working fine but now I am trying to implement the code in iOS to try out. I see the gradle task in my build.gradle for iOS.

// 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'
        }
    }
}

It says not to run the task directly but according to this guide it says to run it directly. When I run it directly I get an error saying

Execution failed for task ':app:copyFramework'.

Could not get unknown property 'configuration.build.dir' for task ':app:copyFramework' of type org.gradle.api.DefaultTask.

I dont know what I have to do to build this for iOS.

Can anyone help me out here on what to do?

First of all, let's figure out what is going on here. As you have to use the Xcode project to implement the final app for iOS, the framework should be somehow added there at the build time. You are mixing two approaches here:

  1. The copyFramework task, that should be run as a build stage of the Xcode build process. It copies produced framework to the Frameworks directory, to make it accessible from the Objective-C or Swift code. To learn where it is, this task depends on environment variables, that Xcode sets while executing the build. That the reason why you get an error here.
  2. The task from the guide slightly differs from the first one. The main thing here is that packForXcode copies the framework to the designated directory ../../SharedCode/build/xcode-frameworks , and the Xcode project is already set to take frameworks from there(see this line ). Therefore, the task does not depend on the Xcode build process and can be executed directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM