Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin multiplatfrom #3249

Open
shadmanadman opened this issue Feb 14, 2025 · 5 comments
Open

Kotlin multiplatfrom #3249

shadmanadman opened this issue Feb 14, 2025 · 5 comments

Comments

@shadmanadman
Copy link

Hi there. First I know this question might be silly but i'm stuck for weeks now.

I'm trying to use tdlib in a kotlin multiplatfrom project. I successfully integrated the Android part using the JNI. You can see the android structure part:

Image

But i'm confused for the ios and desktop (macos) part. I build the td lib based on the ios example and I have these folders after the build:

Image

first I try to use the xcframework as you can see here:

Image
but I got nowhere with this.

second I try to use the koltin/native by importing the dylib and headers files:

Image

But still I got no progress here.

There may be a misunderstanding, but I can not figure it out.
Please, any clue or point would be appreciated here.

@shadmanadman
Copy link
Author

Update:
So far for ios I managed to access the tdlib functions using cintreop. here is the def file:

package = tdlib
headers = /Users/shadmanadman/AndroidStudioProjects/KMP-TdLib-git/composeApp/native/include/td/telegram/td_json_client.h
libraryPaths.ios = /Users/shadmanadman/AndroidStudioProjects/KMP-TdLib-git/composeApp/native/lib/iOS-simulator
linkerOpts.ios = -L/Users/shadmanadman/AndroidStudioProjects/KMP-TdLib-git/composeApp/native/lib/iOS-simulator -ltdjson

here is the native folder:

Image

but after calling for example td_create_client() from iosMain:

import androidx.compose.ui.window.ComposeUIViewController
import kotlinx.cinterop.ExperimentalForeignApi
import tdlib.*
@OptIn(ExperimentalForeignApi::class)
fun MainViewController() = ComposeUIViewController {
    td_create_client_id()
    App()
}

I get this error:

Undefined symbols for architecture arm64:
  "_td_create_client_id", referenced from:
      _tdlib_td_create_client_id_wrapper0 in ComposeApp[2](ComposeApp.framework.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@shadmanadman
Copy link
Author

shadmanadman commented Mar 2, 2025

Update 2-mar-2025:
I found a better idea. We can create a Kotlin/native library to integrate with tdlib with C interop. Then we use this library in our KMP project. Can we do that?
I think in this way we do not need to integrate tdlib for each platform.

@levlam
Copy link
Contributor

levlam commented Mar 3, 2025

Integration shouldn't be different from integration with any other native shared library. There should be multiple tutorials for this, but I didn't do this myself for an iOS app in Kotlin and can't help with the exact steps.

@shadmanadman
Copy link
Author

@levlam Thank you.
Unfortunately, the number of resources and tutorials is very low in this case. Just some official samples and some answers/questions.
I'm aiming for tdlib. I will continue to update my progress on this issue if it's ok.

@shadmanadman
Copy link
Author

shadmanadman commented Mar 4, 2025

Update 4-mar-2025:

For now, lets forget about kotlin/native.

I successfully managed to run tdlib functions on desktop using JNI. Now there are some key points here:

  • You need to link your jni.c file, which contains your jni function to the actual libtdjson.dylib for macOS (or dll on Windows) and create a new dylib (or dll) file.
  • When you are loading the library, you need to load the new dylib file that you created.
  • When you create a new dylib and linked to the libtdjson.dylib, after running it needs the path of the actual dylib (in this case libtdjson.dylib). You need to specify the -rpath (runtime search path).

Here is a sample to create a new dylib file that will link the jni.c file and libtdjson.dylib , and also specify the -rpath:

clang -shared -o libtdlib_jni.dylib TdLibJNI.c \
  -I$(/usr/libexec/java_home)/include \
  -I$(/usr/libexec/java_home)/include/darwin \
  -L{path to libtdjson.dylib for macOS} \
  -ltdjson \
  -Wl,-rpath,{path to libtdjson.dylib for macOS}

next step is iOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants