https://kotlinlang.org logo
#ios
Title
# ios
k

Konstantin Petrukhnov

10/28/2019, 12:23 PM
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

svyatoslav.scherbina

10/28/2019, 1:33 PM
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

alex009

10/28/2019, 2:43 PM
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

Konstantin Petrukhnov

10/29/2019, 6:23 AM
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

alex009

10/30/2019, 7:47 AM
in
NSBundle.bundleForClass(object_getClass(this)!!)
you should give class from framework, not from app
k

Konstantin Petrukhnov

10/30/2019, 7:52 AM
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

alex009

10/30/2019, 9:25 AM
strange. we use it in our projects (in moko-resources)
s

svyatoslav.scherbina

11/01/2019, 9:37 AM
@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

Konstantin Petrukhnov

11/01/2019, 10:01 AM
@svyatoslav.scherbina how do i produce dynamic one? Any example?
s

svyatoslav.scherbina

11/01/2019, 10:25 AM
Try configuring your framework with
isStatic = false
. You may then require enabling
use_frameworks
in your application
Podfile
.
k

Konstantin Petrukhnov

11/04/2019, 7:09 AM
@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

svyatoslav.scherbina

11/05/2019, 7:57 AM
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

Konstantin Petrukhnov

11/18/2019, 6:04 AM
With suggested change, it works. Thank you very much!
5 Views