https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

Christian Sousa

04/01/2020, 4:49 PM
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

magnumrocha

04/01/2020, 4:52 PM
this will only work on XCode
the bottom of the page
Current Limitations
c

Christian Sousa

04/01/2020, 4:54 PM
So in order to use it I need to open it on Xcode? And build my framework from there?
m

magnumrocha

04/01/2020, 4:55 PM
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

Christian Sousa

04/01/2020, 4:56 PM
But how should I be able to build a framework using gradle inside xcode?
m

magnumrocha

04/01/2020, 4:56 PM
I think you can don't use the pod config here, and just add your dependency in your Podfile
c

Christian Sousa

04/01/2020, 4:57 PM
but I need the swift lib on my framework, not on my app
m

magnumrocha

04/01/2020, 4:58 PM
so, you have to create the c_interop file in your ios source folder
s

spierce7

04/01/2020, 4:59 PM
So you can get code completion with this, but I only found that I had code completion in the source set of iosX86
m

magnumrocha

04/01/2020, 4:59 PM
c

Christian Sousa

04/01/2020, 4:59 PM
Is there any example of something like that? I was looking into https://github.com/JetBrains/kotlin-native/tree/master/samples/cocoapods
s

spierce7

04/01/2020, 4:59 PM
don’t use
iosMain
- it doesn’t work for anything
m

magnumrocha

04/01/2020, 5:00 PM
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

magnumrocha

04/01/2020, 5:01 PM
why not works?
s

spierce7

04/01/2020, 5:01 PM
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

Christian Sousa

04/01/2020, 5:02 PM
thanks, will try that!
m

magnumrocha

04/01/2020, 5:03 PM
well, for works good, and I am using the Android Studio 3.6 (stable)
no problem at all
c

Christian Sousa

04/01/2020, 5:25 PM
@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

spierce7

04/01/2020, 5:26 PM
what ide version are you using?
c

Christian Sousa

04/01/2020, 5:26 PM
Android Studio 3.6.1
Kotlin 1.3.70
s

spierce7

04/01/2020, 5:27 PM
stop using android studio
use intellij
Use the latest EAP of IntelliJ
use 1.3.71
Stop using the ios target.
m

magnumrocha

04/01/2020, 5:27 PM
well, when use the cocoa plugin you don't need the packForXcode, the cocoa plugin will do it for you
🙌 1
s

spierce7

04/01/2020, 5:27 PM
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

magnumrocha

04/01/2020, 5:28 PM
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

Christian Sousa

04/01/2020, 5:34 PM
Ok, thanks to you both, I will look into both examples and try to make it work 😉
m

magnumrocha

04/01/2020, 5:34 PM
@spierce7 I don't understand your Issue with
iosMain
, it only won't work if you don't config it
s

spierce7

04/01/2020, 5:35 PM
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

magnumrocha

04/01/2020, 5:35 PM
the code completition is a problem of the IDE plugin, the compilation will works as expected...
s

spierce7

04/01/2020, 5:37 PM
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

magnumrocha

04/01/2020, 5:40 PM
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...
6 Views