https://kotlinlang.org logo
#compose
Title
# compose
j

joney

12/09/2021, 8:47 AM
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

Albert Chang

12/09/2021, 9:25 AM
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

joney

12/09/2021, 9:36 AM
I see. That makes sense. Thank you.
👍 1
t

Tolriq

12/09/2021, 7:19 PM
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

Albert Chang

12/10/2021, 12:40 AM
@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

Tolriq

12/10/2021, 7:47 AM
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

Albert Chang

12/10/2021, 8:34 AM
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

Tolriq

12/10/2021, 9:00 AM
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

Albert Chang

12/10/2021, 9:07 AM
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

Tolriq

12/10/2021, 9:17 AM
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
4 Views