Is it possible to refer to resource file that is i...
# ios
k
Is it possible to refer to resource file that is inside ".framework" bundle, from arm64/x64 source? E.g. mpp library project, that have sources and few resource files. In jvm files inside jar could be access through classpath, but is it possible to do similar on ios? Now I'm using Bundle.url(forResource) in swift, and then pass path string to kotlin, that will access file.
s
Now I’m using Bundle.url(forResource) in swift, and then pass path string to kotlin, that will access file.
Why not use the same from Kotlin?
a
in case of
.framework
you just should use not
Bundle.main
but
NSBundle.bundleForClass(object_getClass(this)!!)
(with class from framework) like in plugin https://github.com/icerockdev/moko-resources/blob/327eef878d929ec78cc38eb7202b99404a400874/gradle-plugin/src/main/kotlin/dev/icerock/gradle/generator/IosMRGenerator.kt#L46
k
Thx. Both examples are very useful!
Hm.. still can't get it working. Tried both, but pathForResource(,,) always return null. NSBundle.mainBundle.bundlePath and NSBundle.bundleForClass(object_getClass(this)!!).bundlePath return some string with ".app"
I think I should specify somehow bundle... but inside the library I don't know framework name....
e.g. in ios code I was using something like this: Bundle(url: Bundle.main.bundleURL.appendingPathComponent("MyKotlin.bundle"))!
but MyKotlin project have dependency on libA, that actually have code tthat should load resource
any thoughts? @svyatoslav.scherbina @alex009
a
in
NSBundle.bundleForClass(object_getClass(this)!!)
you should give class from framework, not from app
k
NSBundle.bundleForClass(object_getClass(BundlePathResolver())!!).bundlePath return same path as mainBundle
So I have MyApp, that use MyFramework, that use MyLib. file.txt is inside MyFramework.framework (cocoapods). In MyLib, I'm trying to load that file. In that lib I can't give class from MyFramework, as lib could be used in multiple frameworks.
@alex009 in swift code in ios, Bundle(for: NSClassFromString("KotlinInt")!) refer to main bundle and not to kotlin .framework
a
strange. we use it in our projects (in moko-resources)
s
@Konstantin Petrukhnov I guess your framework is static. In this case you can’t get path to the framework bundle with
NSBundle.bundleForClass
, as all code including classes is linked into main application binary. And this is not Kotlin-specific.
k
@svyatoslav.scherbina how do i produce dynamic one? Any example?
s
Try configuring your framework with
isStatic = false
. You may then require enabling
use_frameworks
in your application
Podfile
.
k
@svyatoslav.scherbina It seems that isStatic and export don't work together very well. Example: https://github.com/Ekahau/khtf-iosapp-fw/blob/master/build.gradle If 'isStatic = false' uncommented, then other libs are not exported to framework.
s
This shouldn’t be related at all. Could you recheck with clean build?
How does it reproduce anything related to your report above:
If ‘isStatic = false’ uncommented, then other libs are not exported to framework.
? I can’t find an
export
in your build script. Considering
isStatic
having no effect: your build script doesn’t seem correct. Replacing
binaries.findAll { ... }.every {
with
binaries.matching { ... }.all {
makes
it.isStatic = false
work as expected.
k
With suggested change, it works. Thank you very much!