我创建了一个kotlin共享库项目(使用Windows上的Android Studio),android方面可以正常工作,但是由于某种原因,当在Kotlin中编写iOS特定代码时,我似乎无法导入{{1 }}库。我正在按照here的说明进行操作。
我确实将platform
放入了include ':Shared'
,并将这个settings.gradle
放入了我的应用implementation project(':Shared')
我的最终目标是让该库具有将在iOS和Android之间共享的业务逻辑。我只是想让一个示例项目运行,以便我知道它可以工作。
这是我的文件结构:
我的build.gradle
模块的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
我的Android //can't get this import to work.
//import platform.Foundation.NSDate
actual fun getCurrentDate(): String{
//return NSDate().toString()
return ""
}
(可行):
MainActivity
对于平台库为何不起作用的任何帮助或建议,将深表感谢。
答案 0 :(得分:0)
我只是在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