https://kotlinlang.org logo
Title
k

Konstantin Petrukhnov

11/19/2019, 4:42 PM
Going back to my problem with text file inside bundle. Now I have dynamic library, and calling next code in Swift resolved to kotlin framework:
let b1 = Bundle(for: KotlinInt.self).bundlePath
But calling similar code in Kotlin code resolved to main app bundle (same with specific MyClass that inside framework):
val bundle1 = NSBundle.bundleForClass(object_getClass(Int)!!).bundlePath
Calling to get all bundles, successfully resolve all bundles as list:
val bundle7 = NSBundle.allBundles().joinToString { (it as NSBundle).bundlePath }
Is there some better way, than iterate through all bundles and try to find resource in each of it?
(bundle as NSBundle).pathForResource(fileName, null)
s

svyatoslav.scherbina

11/20/2019, 8:32 AM
val bundle1 = NSBundle.bundleForClass(object_getClass(Int)!!).bundlePath
This code is not equivalent and not supposed to work as you expect.
iterate through all bundles and try to find resource in each of it
What’s wrong with this? Alternatively you could find a bundle by name.
k

Konstantin Petrukhnov

11/21/2019, 4:12 AM
I have library(separate project), that is used in multiple other projects. There is no way for library to know name without passing it from code that actually combines multiple libraries into same bundle.
Iterating through all bundles is acceptable workaround, but there is chance, that same filename appear in some other library (e.g. Alamofire), and it will cause some bugs.
Here is diagram, where library C need to load file that belong to library C. It used in 2 separate apps (and 2 separate bundles), so resolving by name is not possible.

https://raw.githubusercontent.com/Ekahau/khtf/master/docs/concept.png

(a,b,c,d,e - are libraries that done as independent projects)
s

svyatoslav.scherbina

11/21/2019, 7:07 AM
Iterating through all bundles is acceptable workaround, but there is chance, that same filename appear in some other library (e.g. Alamofire), and it will cause some bugs.
Not sure if choosing a unique name is a problem. For example, you can include some unique name of your library (e.g. package name) into the resource path (e.g. as a subdirectory name).
k

Konstantin Petrukhnov

11/21/2019, 8:35 AM
yes, that is what I'm doing currently 🙂
thank you for help!
m

marcinmoskala

11/23/2019, 11:54 PM
Doesn’t seem like a long-term solution though 😕