The `package androidx.compose.ui.res` provides acc...
# compose
j
The
package androidx.compose.ui.res
provides access to resources as follows (src):
Copy code
@Composable
@ReadOnlyComposable
private fun resources(): Resources {
    LocalConfiguration.current //<---- why?
    return LocalContext.current.resources
}
☝️ I wonder why a call to
LocalConfiguration.current
is needed before accessing the
resources
field. Is this a pattern which I should follow for any values provided with local composition? 👇 So should I do this in my own code?
Copy code
@Composable
@ReadOnlyComposable
private fun myThing(): MyThing {
    LocalThings.current //<---- why?
    return LocalSomeThings.current.myThing
}
a
LocalConfiguration
is read here to make sure when there is a configuration change the resources is re-retrieved as there are cases where configuration changes but context doesn't change. You don't need to do this unless you are sure it's necessary.
💡 2
2
j
I see. That makes sense. Thank you.
👍 1
t
Are there some special rules in R8 to have that call kept? I tried to do that reading a state and not using it and it was removed by R8 in full mode.
a
@Tolriq This shouldn't happen because reading a state or a composition local isn't side effect free and I can't reproduce in my project with R8 full mode enabled. If you really have this problem, file a bug against R8.
t
Ok after some quick test doing
Copy code
val X by XXX.collectAsState()
X
Causes problems with R8 (And the IDE also says X is unused) Doing
Copy code
val X = XXX.collectAsState()
X.value
Does work. Since IDE behavior is consistent with R8 not sure it's R8 side. But maybe it's normal?
a
I still cannot reproduce. IDE and R8 are completely different parts and their behaviors have nothing to do with each other. As I said before you should file an issue against R8 with a repro.
t
So when using the first example the IDE does not say that the X is unused? And R8 does not remove it in your case? I have opened dozens of R8 issues leading to dozens of fixes, but I always first gather the necessary information for them. Telling them the IDE say the variable is unused and R8 remove it, they will logically says it's normal and I'll have nothing to tell them why it's not.
a
The IDE doesn't distinguish standard variable and property with delegate so it will show the read as unused but you can easily fix that with a
@Suppress("unused")
. Reading a property (or whatever you like to call it) with delegate is actually invoking a function so as long as it is not side effect free it shouldn't be stripped out by R8. And by saying I can't reproduce it I mean R8 didn't stripped the invocation out.
t
Ok so switched to R8 3.2.31 and it does work properly in both cases. My issues repro project was still forced to an old 3.0 release 😞 Sorry for the noise.
👍 1