How do I use FontResource in CMP 1.6.0 beta02? I have composeResources/font/myfont.ttf in shared fol...
j
How do I use FontResource in CMP 1.6.0 beta02? I have composeResources/font/myfont.ttf in shared folder. Trying to do like this from another module:
Copy code
Font(resource = FontResource("myfont"), weight = FontWeight.Normal, style = FontStyle.Normal)
Should the path or id be different somehow?
m
I don’t thing that’s the way it is supposed to be used. Have a look here: https://proandroiddev.com/how-to-use-resources-in-compose-multiplatform-77a6552b505d
j
What do you mean supposed to work? If refer to the Res thing cant use that as not working multi module, need to refer to the actual FontResource myself manually.
Same way I am able to do:
Copy code
Image(painterResource(DrawableResource("files/compose-multiplatform.xml")), null)
I guess I maybe need todo FontResource("font/myfont.ttf") not sure.
c
Isn't the compile time resources feature limited to a single module for now? I thought I read that somewhere but not sure anymore
j
Yeah but I dont want to use that, as of that exact reason 😄 I expect I can use same code that Res class generating but do it manually? At least Jetbrains has that in demo project. Maybe fonts working different dont know. Trying to rule out some things.
I am having all my resources in multiple modules and need to be able to refer to DrawableResource, StringResource, FontResource etc for now.
c
Ah okay.
j
Like if I use Res.font.myFont, what does that code gives me? As I cant using it in my shared folder I cannot see that code myself generated 😄
m
I was told that this is internal API and should not be used. See: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1706812458506069?thread_ts=1706810321.133819&cid=CJLTWPH7S If you want to make resources available to other modules you can provide your own accessor methods in the module which owns the resource. I am not sure but the current restriction could be due to some Java module system limitations.
j
Well yeah thats fine, everything in beta/dev I expect break, even if using the intented version 😛
But until they fixed multi module I need to use the well "wrong" way.
m
Like if I use Res.font.myFont, what does that code gives me?
It gives you statically checked access. It avoids typos.
j
Yeah I know, but if I cant use it from module A, thats dependency to shared module B (Where resources is), how do I?
I cant access shared modules Res class or interface backwards, would be circular gradle module deps not compile 😛
m
You misunderstood me. If module A owns your resource then you can access it there via Res…. and you could provide module A with some method
getMyFont
or whatever which you can access from module B, C, D, …
j
So if I have a module A, I can put all my resources into that one, and iOS will access it through the shared module depending on module A? From what I tested thats not possible do or?
"You misunderstood me. If module A owns your resource then you can access it there via Res…. and you could provide module A with some method
getMyFont
or whatever which you can access from module B, C, D, …" Also this defeats the entire purpose of having the code generated for Res 😛
Then I can just hardcode the paths to all my files myself, which I do today 😛 Reproduced the entire source code from jetbrains resources library today, as not supporting all file types I need.
If I need an enum/interface, whatever its the same I already have 😛 And avoids static typos in my code, by always using my Resource class map for strings, diemsnsions, videos, images etc.
@Michael Paus Thanks for input! 🙂 I managed to sort out it works when I do:
Copy code
FontResource("font/myfont.ttf")
And yeah probably breaks in future release, but thats how it is, I am on the edge each release anyway. Once they provide me a window in upcoming Jetbrains releases of going forward not doing this I will update. Until then I need to stick with beta02 I guess 😄
u
FontResource("font/myfont.ttf")
not working in IOS
j
Then not doing it right. Also probably want doing: fontResource(Res.font.myFont)
u
Done that, but not working in multi-module. resource not found font/myfont.ttf
only main project resources i am able to see in build
j
Yeah multi module not supported yet. Need use interface to shared module or such through common module all modules can access. At least for now. Thats what I do and inject with Koin.
u
made it work. Just add in main module's commanMain before dependencies
resources.srcDirs("${project(":module")._projectDir_}/src/commonMain/composeResources")
🎉 1
m
I am wondering whether such tricks might lead to a duplication of resources in different artifacts?
243 Views