For Interoperability, do I only need to do: ```kot...
# multiplatform
c
For Interoperability, do I only need to do:
Copy code
kotlin {
    cocoapods {
        // Configure a dependency on AFNetworking.
        // The CocoaPods version notation is supported.
        // The dependency will be added to all macOS and iOS targets.
        pod("AFNetworking", "~> 3.2.0")
    }
}
? Because I did that and then on my ios part I cant import it because it says that AFNetworking is an unresolved reference
m
this will only work on XCode
the bottom of the page
Current Limitations
c
So in order to use it I need to open it on Xcode? And build my framework from there?
m
yes, if you use the pod option of cocoapods plugin
if you don't use it, so your build will works on gradle/IDE
c
But how should I be able to build a framework using gradle inside xcode?
m
I think you can don't use the pod config here, and just add your dependency in your Podfile
c
but I need the swift lib on my framework, not on my app
m
so, you have to create the c_interop file in your ios source folder
s
So you can get code completion with this, but I only found that I had code completion in the source set of iosX86
m
c
Is there any example of something like that? I was looking into https://github.com/JetBrains/kotlin-native/tree/master/samples/cocoapods
s
don’t use
iosMain
- it doesn’t work for anything
m
Copy code
configure(listOf(iosArm32, iosArm64, iosX64)) {
    compilations.getByName("main") {
        source(sourceSets.get("iosMain"))
        val firebaseAuth by cinterops.creating {
            packageName("cocoapods.FirebaseAuth")
            defFile(file("$projectDir/src/iosMain/c_interop/FirebaseAuth.def"))
            compilerOpts("-F$projectDir/src/iosMain/c_interop/modules/FirebaseAuth-6.2.1")
        }
    }
}
m
why not works?
s
This got code completion in the x86 source code for me.
make sure you call the gradle task to initialize your podspec file
also - make sure you are using 1.3.71 as that’s what I was using
I was also using the IntelliJ EAP I think
c
thanks, will try that!
m
well, for works good, and I am using the Android Studio 3.6 (stable)
no problem at all
c
@magnumrocha @spierce7 I tried both solutions, but neither worked. My
build.gradle.kts
looks like this right now:
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    id( "com.android.library")
    id("dev.icerock.mobile.multiplatform-resources")
    kotlin("multiplatform")
    id("org.jetbrains.kotlin.native.cocoapods")
}

version = "1.0"

apply(plugin="kotlinx-serialization")

android {
    compileSdkVersion(29)
    buildToolsVersion("29.0.3")
    defaultConfig {
        minSdkVersion(16)
        targetSdkVersion(29)
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "<http://proguard-rules.pro|proguard-rules.pro>")
        }
    }
}

kotlin {
    val kotlin_version = "1.3.70"
    val coroutines_version = "1.3.4"
    val serialization_version = "0.20.0"
    val ktor_version = "1.3.2"

    //select iOS target platform depending on the Xcode environment variables
    val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iOSTarget("ios") {}

    android("android")

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-properties-common:$serialization_version")
        implementation("io.ktor:ktor-client:$ktor_version")
        implementation("io.ktor:ktor-client-core:$ktor_version")
        implementation("io.ktor:ktor-client-json:$ktor_version")
        implementation("io.ktor:ktor-client-serialization:$ktor_version")
        implementation("com.benasher44:uuid:0.1.0")
        implementation("com.soywiz.korlibs.klock:klock:1.9.1")
        implementation("com.soywiz.korlibs.krypto:krypto:1.11.0")
        implementation("dev.icerock.moko:resources:0.9.0")
        implementation("com.google.android:flexbox:2.0.1")
    }

    sourceSets["androidMain"].dependencies {
        implementation("androidx.preference:preference-ktx:1.1.0")
        implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-properties:$serialization_version")
        implementation("io.ktor:ktor-client-android:$ktor_version")
        implementation("io.ktor:ktor-client-core-jvm:$ktor_version")
        implementation("io.ktor:ktor-client-json-jvm:$ktor_version")
        implementation("io.ktor:ktor-client-serialization-jvm:$ktor_version")
    }

    sourceSets["iosMain"].dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-properties-native:$serialization_version")
        implementation("io.ktor:ktor-client-ios:$ktor_version")
        implementation("io.ktor:ktor-client-core-native:$ktor_version")
        implementation("io.ktor:ktor-client-serialization-native:$ktor_version")
        //implementation("io.ktor:ktor-client-json-native:$ktor_version")
    }

    cocoapods {
        // Configure fields required by CocoaPods.
        summary = "MyLib"
        homepage = "<https://google.com>"

        // The name of the produced framework can be changed.
        // The name of the Gradle project is used here by default.
        frameworkName = "MyLib"

    }

}

multiplatformResources {
    multiplatformResourcesPackage = "com.christiansousa.mylib"
}

val packForXcode by tasks.creating(Sync::class) {
    val targetDir = File(buildDir, "xcode-frameworks")
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"

    val framework = kotlin.targets
        .getByName<KotlinNativeTarget>("ios")
        .binaries.getFramework(mode)

    inputs.property("mode", mode)
    dependsOn(framework.linkTask)

    from({ framework.outputDirectory })
    into(targetDir)


    /// generate a helpful ./gradlew wrapper with embedded Java path
    doLast {
        val gradlew = File(targetDir, "gradlew")
        gradlew.writeText("#!/bin/bash\n"
                + "export 'JAVA_HOME=${System.getProperty("java.home")}'\n"
                + "cd '${rootProject.rootDir}'\n"
                + "./gradlew \$@\n")
        gradlew.setExecutable(true)
    }
}

tasks.getByName("build").dependsOn(packForXcode)
Any suggestions?
s
what ide version are you using?
c
Android Studio 3.6.1
Kotlin 1.3.70
s
stop using android studio
use intellij
Use the latest EAP of IntelliJ
use 1.3.71
Stop using the ios target.
m
well, when use the cocoa plugin you don't need the packForXcode, the cocoa plugin will do it for you
🙌 1
s
define your targets explicitly.
Follow the example that I sent you. It’s a working example. Maybe your next step is to get the version I sent you working
you won’t get this working using
iosMain
m
I have a repository in GitHub with this working: https://github.com/magnumrocha/kmp-slate
see here a sample: https://github.com/magnumrocha/kmp-slate/blob/master/kmp_lib/gradle/target_ios.gradle this is my setup for iOS build part...
this have a little Issue in the line 39:
frameworkName = "SlateKt" // NOTE: The build for iOS Simulator works, but for iOS Devices not...
I have reported to JetBrains, and now I am waiting for a solution...
to make this works, just comment this line
and the import of Kotlin library in Swift side will be:
import kmp_lib
instead of
import SlateKt
just that...
c
Ok, thanks to you both, I will look into both examples and try to make it work 😉
m
@spierce7 I don't understand your Issue with
iosMain
, it only won't work if you don't config it
s
he’s having trouble getting resolution and code completion.
I couldn’t get code completion to work with it at all
I could only get code completion to work with one source set.
m
the code completition is a problem of the IDE plugin, the compilation will works as expected...
s
I got it working
I’ve had nothing but inconsistencies and trouble with
iosMain
I’m done using it
it’s forced me to waste days of my time
m
ok but be aware that this is an Issue in the IDE plugin... if you see the code of most KMP libraries they make the use of
iosMain
source folder, and the compilation will works as expected...
I hope they fix it in a near future...