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

juliocbcotta

08/10/2022, 3:21 PM
I am trying to enable
nonTransitiveRClass
in my project, but to avoid a bunch of import alias I thought I could encapsulate the R classes from library modules and avoid the class name conflict... How to encapsulate the
R
or
R.string
class in my class ? I tried this
Copy code
object MyR {
    val string: R.string = R.string
}
but I get this
e

ephemient

08/10/2022, 3:28 PM
doesn't work,
R.string
is a class not an object
j

juliocbcotta

08/10/2022, 3:29 PM
I know, I posted the code to illustrate what I tried...
and what I am trying to archive
e

ephemient

08/10/2022, 3:29 PM
you can
Copy code
import my.R as MyR
but will have to do so in every file that uses it
j

juliocbcotta

08/10/2022, 3:30 PM
that is what I am trying to avoid... and I can't do that in my java files
e

ephemient

08/10/2022, 3:33 PM
that isn't a Kotlin question then… but for Java, no way around it unless you create a wrapper for each constant, which breaks lint checks
j

juliocbcotta

08/10/2022, 3:34 PM
it is a kotlin question as I am asking the correct syntax to do it in Kotlin. I want to keep a reference to the R class in my own class and access it.
e

ephemient

08/10/2022, 4:15 PM
the answer is you can't
j

juliocbcotta

08/10/2022, 4:17 PM
Ok, thanks
t

tseisel

08/10/2022, 8:18 PM
Would be nice if
typealias
allowed referencing subclasses like this:
Copy code
typealias MyR = com.my.package.R
val description = MyR.string.some_description
But it doesn't work. You could however define a typealias for each of
R.string
,
R.drawable
... but that's quite verbose.
14 Views