Guys, can anyone help with adding iOS library to s...
# kotlin-native
a
Guys, can anyone help with adding iOS library to shared KMM module? Struggling for 3 days already and cannot make it working... The latest error I get when building app in xcode:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_FIROptions", referenced from:
objc-class-ref in shared(result.o)
"_OBJC_CLASS_$_FIRApp", referenced from:
objc-class-ref in shared(result.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
a
Hello! Please describe how you are adding the framework, using CocoaPods plugin or a
.def
file? Also, see this discussion than might be related.
a
Ive tried to use cocoapods plugin and add library there, but it has same result. then i tried to load and build with Carthage and configure it this way:
Copy code
kotlin {
    android()
    //ios()

    val iosArm64 = iosArm64()
    val iosX64 = iosX64("ios") 

    sourceSets {
        val commonMain by getting

        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.0")
                api("com.google.firebase:firebase-common:19.3.1")
            }
        }

        val iosMain by getting

        configure(listOf(iosArm64, iosX64)) {
            compilations.getByName("main") {
                source(sourceSets.get("iosMain"))
                val firebasecore by cinterops.creating {
                    packageName("cocoapods.FirebaseCore")
                    defFile(file("$projectDir/src/iosMain/c_interop/FirebaseCore.def"))
                    compilerOpts("-F$projectDir/src/iosMain/c_interop/Carthage/Build/iOS/")
                }
            }
        }
    }

    cocoapods {
        summary = "Shared module"
        homepage = "none"

        //pod("FirebaseCore")
        //pod("FirebaseFirestore")
    }


}
shared module is connected to iosApp with Pods
Copy code
use_frameworks!

platform :ios, '9.0'

target 'iosApp' do
    pod 'shared', :path => '../shared'
end
When trying to build for iOS physical device, same error appears but with arm
ld: warning: Could not find or use auto-linked framework 'GoogleDataTransport'
ld: warning: Could not find or use auto-linked framework 'FirebaseCore'
ld: warning: Could not find or use auto-linked framework 'FirebaseCoreDiagnostics'
ld: warning: Could not find or use auto-linked framework 'nanopb'
ld: warning: Could not find or use auto-linked framework 'FIRAnalyticsConnector'
ld: warning: Could not find or use auto-linked framework 'GoogleAppMeasurement'
ld: warning: Could not find or use auto-linked framework 'PromisesObjC'
ld: warning: Could not find or use auto-linked framework 'FirebaseAnalytics'
ld: warning: Could not find or use auto-linked framework 'GoogleUtilities'
ld: warning: Could not find or use auto-linked framework 'FirebaseInstallations'
Undefined symbols for architecture arm64:
 
"_OBJC_CLASS_$_FIROptions", referenced from:
   
objc-class-ref in shared(result.o)
 
"_OBJC_CLASS_$_FIRApp", referenced from:
   
objc-class-ref in shared(result.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Additional info. Using koltin plugin 1.4.10. The reason why I didn't use existing firebase libs that they are below 1.4, and it didn't even try to build saying lib is below 1.4
t
Why do you still have the
val firebasecore by cinterops.creating
code? Why not just uncommented the firebase pods from cocoapods and remove your cinterop code? I think that should work.
a
I tried that also, same result, decided to try this way
also task for xcode is present in buid.gradle
Copy code
val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    kotlin.targets.forEach { println("name = ${it.name}") }
    val framework =
        kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
Also Ive tried to simplify and use some common ios library and just cocoapods. Here is the whole gradle file:
Copy code
plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id("kotlin-android-extensions")
    kotlin("native.cocoapods")
}
group = "com.sixt.lastapp"
version = "1.0-SNAPSHOT"

repositories {
    gradlePluginPortal()
    google()
    jcenter()
    mavenCentral()
}

kotlin {
    android()
    ios()

    sourceSets {
        val commonMain by getting

        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.0")
                api("com.google.firebase:firebase-common:19.3.1")
            }
        }

        val iosMain by getting
    }

    cocoapods {
        summary = "Shared module"
        homepage = "none"

        ios.deploymentTarget = "13.2"
        pod( "AFNetworking", "4.0")
    }
}

android {
    compileSdkVersion(29)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(24)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    kotlin.targets.forEach { println("name = ${it.name}") }
    val framework =
        kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
Now when building in Xcode it says that cannot find AFNetwork library:
ld: framework not found AFNetworking
clang: error: linker command failed with exit code 1 (use -v to see invocation)
t
If you are using cocoapods you don't need the packForXcode task
Instead of using the packForXcode task you need to include the kotlin framework using cocoapods
In xcode add the Kotlin pod in your Podfile
a
looks loke default xcode templeate provided by KMM somehow didnt support pods, though i installed them
but with empty ios project I finally achieved success import and build
404 Views