Hi, I am trying to create native application. I am...
# multiplatform
f
Hi, I am trying to create native application. I am using klib library. The problem is that the klib library is built for x64 so i cannot use it as a dependency in arm builds. So i am wondering if there is any way to have the same code for multiple architectures but different dependencies. I cannot put the code into common, because in the code i am using the library so i would have to put the dependency into common souce set. I can create multiple source sets, but then i would have to copy code or create symlink. So is there any better way ?
f
This will not help me, because i do have the same code for all platform only the dependencies are different. Hierarchical source sets would need me to copy code.
I need something like kotlin multiplatform is resolving maven dependencies with ending for every platform. For example ktor have multiple artifacts in maven named
ktor-client-core-linuxx64
and
ktor-client-core-iossimulatorarm64
and the build system automatically detects which one to use when you add the dependency using
io.ktor:ktor-client-core:$ktor_version
. I need the same, but for klib libraries added as file dependency.
a
If i understand you correctly You can use
commonMainApi
in the implementation block you can just mention like
io.example.depend:some:1.0
. And it will resolve the platform automatically, you won’t need to manually add platform dependency to each sourceSets block
f
I could do that if the library was in maven repository but it is not. The library is
.klib
file generated by me using the
cinterop
tool. So i need something simillar but for files
a
MavenLocal ?
f
The link is great but the cinterop config in gradle expects that the binaries of the library for the specific architecture is present on the system the project is being built on. That is not the case on my system. I have the klib files built for other architectures. The cinterop config creates them and then uses them.
I got it working. I had to abandon the klib files and copy all related
.so
libraries from all architectures to the project and then create
.def
file with configurations pointing to those shared libraries ... it is not ideal. I would much prefer to have few klib files than a lot of shared libraries from other architectures, but it works
a
awesome! @František Jeřábek can you share the code snippet of this configuration ?
f
@Anmol Verma sure. I did something similar that is in the stackoverflow link you suggested. I have this in my
build.gradle.kts
which adds library to each target.
Copy code
listOf(linuxArm64(),linuxX64()).forEach {
    it.compilations["main"].cinterops {
        val glib by creating
    }
}
Than in the
.def
file i have different linker options pointing to the shared libraries i copied from other architecture systems. The def file looks like this
Copy code
headers = gio/gio.h glib.h gio/gunixfdlist.h

compilerOpts= -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/gio-unix-2.0

linkerOpts = -lglib-2.0 -lgio-2.0
linkerOpts.linux_arm32_hfp = -L src/nativeInterop/armhf_libs/glib
linkerOpts.linux_x64 = -L /usr/lib/x86_64-linux-gnu
linkerOpts.arm64 = -L src/nativeInterop/arm64_libs
Now it automatically builds the
klib
files for each architecture. Its not exactly what i wanted, because i had already build the klib files myself and now i have multiple shared libraries for each architecture instead of one klib file, but it works
147 Views