【问题标题】:Unresolved reference: Platform, in multiplatform project未解决的参考:平台,在多平台项目中
【发布时间】:2020-07-17 09:32:38
【问题描述】:

我创建了一个 kotlin 共享库项目(在 Windows 上使用 Android Studio),android 方面工作正常,但由于某种原因,在 Kotlin 中编写 iOS 特定代码时,我似乎无法导入 platform图书馆。我按照here 的说明开始操作。

我确实将include ':Shared' 放入了我的settings.gradle 中,并将这个implementation project(':Shared') 放入了我的应用程序build.gradle

我的最终目标是让该库仅具有将在 iOS 和 Android 之间共享的业务逻辑。我只是想让一个示例项目运行,以便我知道它有效。

这就是我的文件结构:

我的build.gradle Shared 模块:

apply plugin: 'java-library'
apply plugin: 'kotlin-multiplatform'

/*We are doing three things in the codebase below:

   1.  Listing out the target for the shared code. For Android, JVM target.
       For iOS, target depends on the device type, i.e. simulator or a real device.

   2.  We have defined iOS, Android and common source sets, which will allow different
       configuration for each source set.

   3.  We have created a task for Xcode to generate framework and add it to our iOS project.
   */
kotlin{
    targets{
        //        //Xcode sets SDK_NAME environment variable - based on whether the
        //        //target device is a simulator or a real device, the preset should vary
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
                               ? presets.iosArm64 : presets.iosX64


        //outputKinds - FRAMEWORK would mean that the shared code would be exported as a FRAMEWORK
        // EXECUTABLE - produces a standalone executable that can be used to run as an app
        fromPreset(iOSTarget, 'ios'){
            binaries{
                framework('Shared')
            }
        }

        //create a target for Android from presets.jvm
        fromPreset(presets.jvm, 'android')
    }

    //we have 3 different sourceSets for common, android and iOS.
    //each sourceSet can have their own set of dependencies and configurations
    sourceSets{
        commonMain.dependencies{
            api 'org.jetbrains.kotlin:kotlin-stdlib-common'
        }
        androidMain.dependencies{
            api 'org.jetbrains.kotlin:kotlin-stdlib'
            implementation fileTree(dir: 'libs', include: ['*.jar'])
            implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        }

        iosMain{

        }
    }
}
configurations{
    compileClasspath
}


// This task attaches native framework built from ios module to Xcode project
// Don't run this task directly,
// Xcode runs this task itself during its build process when we configure it.
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew)
//and gradlew is in executable mode (chmod +x gradlew)
task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
    final def framework = kotlin.targets.ios.binaries.getFramework("Shared", mode)
    inputs.property "mode", mode
    dependsOn framework.linkTask
    from { framework.outputFile.parentFile }
    into frameworkDir
    doLast {
        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}

tasks.build.dependsOn packForXCode

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

commonMain代码:

//This function will be the general function declaration that will be used as
//actual in our platform specific code.
expect fun getCurrentDate() : String

//This is the common function that will be called by Android and iOS app
fun getDate():String{
    return "Current Date is ${getCurrentDate()}"
}

androidMain代码:

import java.util.*

actual fun getCurrentDate(): String{
    return Date().toString()
}

iosMain 代码(这就是问题所在):

//can't get this import to work.
//import platform.Foundation.NSDate

actual fun getCurrentDate(): String{
   //return NSDate().toString()
    return ""
}

我的安卓MainActivity(这个工作):

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import getDate// function from our Shared Module

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.dateLbl).text = getDate()
    }
}

对于平台库为何无法工作的任何帮助或建议,我们将不胜感激。

【问题讨论】:

    标签: android kotlin kotlin-multiplatform


    【解决方案1】:

    我只是在 Windows 上尝试过,iOS 平台似乎不可用,即使只是用于导入。我认为您无法在 Windows 上编辑 Apple 目标。本地可用的目标是:

    android_arm32/
    android_arm64/
    android_x64/
    android_x86/
    linux_arm32_hfp/
    linux_arm64/
    linux_x64/
    mingw_x64/
    mingw_x86/
    

    您可以在此处查看本地目标:~/.konan/kotlin-native-windows-1.3.72/klib/platform

    【讨论】:

    • 嗨,我也看到了,这很奇怪,因为根据this 文章,平台库默认是可用的。无需指定特殊链接标志即可使用它们。 Kotlin/Native 编译器自动检测哪些平台库被访问并自动链接所需的库。
    • ~/..konan\kotlin-native-windows-1.3.72/konan/platformDef/ios 包含一些iOS的包,例如UIKit.dfef和posix.def等。也许是什么能用吗?
    • 即使要使用posix,我们也需要平台:import platform.posix.*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2018-12-22
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多