I'm trying to use Firebase Objective C code in Kot...
# kotlin-native
t
I'm trying to use Firebase Objective C code in Kotlin. I added this to the "common" build.gradle:
Copy code
compilations.getByName("main") {
    val firebase by cinterops.creating {
        defFile("$projectDir/src/iOSMain/cinterop/firebase.def")
        val pods = "$projectDir/../ios/Pods"
        includeDirs(
            "$pods/FirebaseCore/Firebase/Core/Public"
        )

        compilerOpts("-F$pods/Firebase -F$pods/FirebaseCore")
        linkerOpts("-F$pods/Firebase -F$pods/FirebaseCore")
    }
}
And this is the
firebase.def
file:
Copy code
language = Objective-C
headers = FirebaseCore.h
When trying to edit the Kotlin code I have no issues importing the Firebase framework. But when compiling for iOS (packForXCode) I get this error:
Copy code
ld: warning: could not create compact unwind for _ffi_call_unix64: does not use RBP or RSP based frame
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_FIRApp", referenced from:
      objc-class-ref in combined.o
ld: symbol(s) not found for architecture x86_64
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors
Does anyone know why this is happening? Is there something wrong in my configuration?
k
It’s an xcode issue. Undefined symbols for architecture x86_64: “_OBJC_CLASS_$_FIRApp”
Sorry, let me rephrase. That’s a native issue. The process can’t find firebase when it tries to link. I would confirm xcode/Pods are otherwise set up correctly and then check the gradle config
t
Thanks for your answer. I have no issues when calling the Firebase code from Swift, the app builds successfully. Once I try to reference some Firebase code in a Kotlin file, this error shows up. So I think this is related to the gradle config and not to xcode.
@kpgalligan any idea what could be wrong in the gradle config?
k
Well, the Interop finds the header file, apparently. My guess is that somethings wrong with your linking settings. Exactly what, will be exceptionally difficult to debug without being able to play with it. I’m not really an expert on linker settings for frameworks, but the same flags in compiler and linker argsseems suspect
i
I had the same issue when linker was configured wrong and it couldn’t find 3rd party library
check your linker options
I believe that def file requires linkerOpts
t
@ivan.savytskyi I think this isn't needed because they are already added in the build.gradle. I tried adding them to the def file as well (with absolute paths, no variables), but unfortunately I still got the same exception. Could it be that I am trying to add the wrong directory to the linkerOpts?
k
Any reason you're not using the multiplatform firebase lib? In any case, i'd look at this for def file example https://github.com/RubyLichtenstein/Kotlin-Multiplatform-Firebase/blob/master/common-all/src/iOSMain/c_interop/firestore/FirebaseFirestore.def
The header files define a "thing" you can call. The linker has to actually go out and find the thing. Assuming the header is correct, the only likely problem you have is a path config problem in the linker
j
Any reason you’re not using the multiplatform firebase lib?
@kpgalligan Are you referring to this 👇 ? https://github.com/RubyLichtenstein/Kotlin-Multiplatform-Firebase
k
Yes. Disclaimer, I’ve never used it. Am curious why one wouldn’t
j
That project seems to be binding only the Firestore stuff and not the whole firebase.
k
Ah. OK.
j
I was just checking the project and I’m getting this error 👇
ld: framework not found FirebaseFirestore
probably missing some includeDirs, don’t know.
k
No idea. Haven’t tried it. There’s a lot there, so a lot of room of issues I’d imagin
imagine
j
true 🙂
I believe a “multiplatform firebase lib” is more than welcome in the Kotlin Native community
It seems that passing the path to frameworks folder in the
linkerOpts
through
-F
option is solving the issue
ld: framework not found FirebaseFirestore
Something like:
Copy code
linkerOpts = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.0/lib/darwin/libclang_rt.osx.a -F ${fullpath-to-frameworks-dir} -framework FirebaseFirestore -framework ...