Anyone successfully integrated Firebase for iOS (c...
# multiplatform
a
Anyone successfully integrated Firebase for iOS (crashlytics, messaging, remote config etc) without using the cocoapods plugin?
h
any update?
f
I’m curious as well.
a
if you want to access from iOS sources, you can generate the same bindings as the cocoapods plugin does using Kotlin Native cinterop yourself. This is very rough test code that I’m in the process of moving to a plugin, I have it inside the
kotlin.sourceSets
block:
Copy code
val firebaseSdkDefinition: File = project.file("src/nativeInterop/cinterop/firebase.def")
    val sourcePackagesRoot = File(project.projectDir.parentFile!!, "iosApp/DerivedData/iosApp/SourcePackages")
    val firebaseIosSdkCheckoutsLocation = File(sourcePackagesRoot, "checkouts/firebase-ios-sdk")
    val firebaseIosSdkArtifactsLocation = File(sourcePackagesRoot, "artifacts/firebase-ios-sdk")
    val firebaseCoreHeader = File(firebaseIosSdkCheckoutsLocation, "FirebaseCore/Sources/Public/FirebaseCore/FirebaseCore.h")
    val firebaseCrashlyticsHeader = File(firebaseIosSdkCheckoutsLocation, "Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FirebaseCrashlytics.h")
    val firebaseMessagingHeader = File(firebaseIosSdkCheckoutsLocation, "FirebaseMessaging/Sources/Public/FirebaseMessaging/FirebaseMessaging.h")
    val firebaseAnalyticsHeaderArm64 = File(
        firebaseIosSdkArtifactsLocation,
        "FirebaseAnalytics/FirebaseAnalytics.xcframework/ios-arm64/FirebaseAnalytics.framework/Headers/FirebaseAnalytics.h"
    )
    val firebaseAnalyticsHeaderArm64x86Simulator = File(
        firebaseIosSdkArtifactsLocation,
        "FirebaseAnalytics/FirebaseAnalytics.xcframework/ios-arm64_x86_64-simulator/FirebaseAnalytics.framework/Headers/FirebaseAnalytics.h"
    )
    targets
        .filterIsInstance<KotlinNativeTarget>()
        .filter { target: KotlinNativeTarget ->
            target.konanTarget.family == Family.IOS
        }
        .forEach { target: KotlinNativeTarget ->
            println("KotlinNativeTarget: ${target.name}")
            target.compilations.getByName("main").cinterops.create("firebase") {
                includeDirs(
                    firebaseCoreHeader.parentFile,
                    firebaseCrashlyticsHeader.parentFile,
                    firebaseMessagingHeader.parentFile,
                    when (val konanTarget: KonanTarget = target.konanTarget) {
                        KonanTarget.IOS_ARM64 -> firebaseAnalyticsHeaderArm64
                        KonanTarget.IOS_SIMULATOR_ARM64,
                        KonanTarget.IOS_X64 -> firebaseAnalyticsHeaderArm64x86Simulator
                        else -> TODO("Unsupported target: $konanTarget")
                    }.parentFile
                )
                definitionFile = firebaseSdkDefinition
                compilerOpts("-DNS_FORMAT_ARGUMENT(A)=", "-D_Nullable_result=_Nullable")
            }
        }
The firebase.def file in the first line contains:
Copy code
package = your.app.cinterop.generated.firebase
language = Objective-C
headers = FirebaseCore.h FirebaseCrashlytics.h FirebaseAnalytics.h FirebaseMessaging.h
to make this work, you either need to go to XCode and change the DerivedData location, or override it in the xcodebuild command, this will determine where packages are checked out to
I will update when I have something cleaner, like a simple Gradle plugin, it’s really not nice code 🙈
example of what is generated
The steps I took: 1. XCode->File->add package dependencies 2. paste https://github.com/firebase/firebase-ios-sdk.git into search bar 3. select FirebaseAnalytics 4. go to build phases and add whatever else you need from Firebase (in my case FirebaseMessaging, FirebaseCrashlytics & FirebaseRemoteConfig), this will update your project.pbxproj file 5. XCode->settings->locations 6. change DerivedData from default to relative 7. advanced->custom->relative to workspace (products and intermediates field should be automatically filled) 8. build the iOS app, even if it fails it should checkout the packages to iosApp/DerivedData, the headers will be somewhere in there 9. do something like this i.e: a. put the firebase.def file somewhere with reference to the headers b. use the
CInteropSettings
DSL in the
kotlin.sourceSets
block for iOS targets to generate the Kotlin Native bindings from the Firebase headers