I'm trying to use some iOS APIs in my recent multi...
# multiplatform
f
I'm trying to use some iOS APIs in my recent multiplatform project (without CocoaPods) and IntelliJ can't find
Foundation
... Any idea of what i'm doing wrong?
Copy code
package sample

import platform.Foundation.NSString

actual class Sample {
    actual fun checkMe() = 7
}

actual object Platform {
    actual val name: String = "iOS"
}

actual fun formatString(source: String, vararg args: Any): String {
    return ""
//    return NSString.initWithFormat
}
r
Issue is probably in your
build.gradle(.kts)
. Certain configurations don’t work well in the IDE (though they will build from Gradle)
f
Maybe! It compiles via command line!
Here is my
build.gradle
file
Copy code
plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.61'
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId 'org.jetbrains.kotlin.mpp_app_android'
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    packagingOptions {
        exclude 'META-INF/kotlinx-coroutines-core.kotlin_module'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    androidTestImplementation 'androidx.test:runner:1.2.0'
}

kotlin {
    targets {
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos")  \
                               ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'ios') {
            binaries {
                framework()
            }
        }

        fromPreset(presets.android, 'android')
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.3"
                implementation "com.soywiz.korlibs.klock:klock:1.8.4"
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation kotlin('stdlib')
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
            }
        }
        androidTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        iosMain {
            dependencies {
                implementation('org.jetbrains.kotlinx:kotlinx-coroutines-core-native') {
                    version {
                        strictly '1.3.3-native-mt'
                    }
                }
            }
        }
        iosTest {
        }
    }
}

// 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'
        }
    }
}
r
Instead of
targets
block (under
kotlin
), try
Copy code
android()
    def isDevice = System.getenv("SDK_NAME")?.startsWith("iphoneos") == true
    def iosTarget = if (isDevice) {
        presets.getByName("iosArm64")
    } else {
        presets.getByName("iosX64")
    }
    targetFromPreset(iosTarget, "ios") { ... }
f
Same error 😞
The kotlin version used by IDEA is different...
This is the problem 🙂
Thanks for the help!
c
@fcosta how to fix it? I have a similar (probably same) error, I am using kotlin 1.3.61 libs but I've kotlin version in AS so everything in K/N is red (but compiles)
f
What is the plugin version used by IDEA? It seems the versions must match!
c
well in the preference>plugin>kotlin I have 1.3.70, and it doesn't seems that there's any way to downgrade it. And I can't update the project to 1.3.70
r
You can download older plugin versions at https://plugins.jetbrains.com/plugin/6954-kotlin/versions
👍 2
p
I’m having issues downgrading using the downloaded version. I’m currently using IntelliJ 2020.1 RC1 which comes bundled with 1.3.71 but need to use 1.3.61 for my K/N projects. I’ve downloaded
1.3.61-release-IJ2019.3-1
which is apparently compatible with
2019.3 — 2020.1 (rc)
but on installing from disk and restarting, IntelliJ automatically updates to 1.3.71 again. Am I missing a trick?
c
I'm using android studio and it doesn't update the plugin automatically