未解决的参考:平台,在多平台项目中

5 android kotlin kotlin-multiplatform

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

我没有把include ':Shared'我的settings.gradle也该 implementation project(':Shared')在我的应用build.gradle

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

这是我的文件结构:

文件结构

build.gradleShared模块:

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"
Run Code Online (Sandbox Code Playgroud)

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()}"
}
Run Code Online (Sandbox Code Playgroud)

androidMain 代码:

import java.util.*

actual fun getCurrentDate(): String{
    return Date().toString()
}
Run Code Online (Sandbox Code Playgroud)

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

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

actual fun getCurrentDate(): String{
   //return NSDate().toString()
    return ""
}
Run Code Online (Sandbox Code Playgroud)

我的机器人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()
    }
}
Run Code Online (Sandbox Code Playgroud)

任何有关平台库为何不起作用的帮助或建议将不胜感激。

Kev*_*gan 0

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

android_arm32/
android_arm64/
android_x64/
android_x86/
linux_arm32_hfp/
linux_arm64/
linux_x64/
mingw_x64/
mingw_x86/
Run Code Online (Sandbox Code Playgroud)

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