Resource IDs
Resources are accessed via the generated Java R class. Each resource in your res directory is assigned a unique integer ID in this class, which is typically used to reference the resource in Java or Kotlin code.
The Droid framework does not generate a separate Swift R class. It just bridges Swift code to the generated Java R class using @dynamicMemberLookup feature, allowing you to access resource IDs as Swift properties directly.
In Android you can use simple R.type.name syntax to access resource IDs, where type is the resource type (like string, drawable, etc.) and name is the resource name defined in XML files.
It is important to note that not all resources are accessible directly through R. Certain resources, such as those provided by the Android framework, must be accessed using a class prefix like android.R.
Droid framework covers both cases as easy as:
let stringId: Int32 = R.string.my_string_resource
let iconId: Int32 = android.R.drawable.ic_dialog_email
If you need to access resource ID with a custom class then you have to write a small helper:
@MainActor
struct mylib: ~Copyable {
static let R = InnerR("com/mycompany/mylib")
}
and use it like this:
let myStringId: Int32 = mylib.R.string.my_string_resource
Here mylib.R represents com.mycompany.mylib.R Java class.
Types
The generated R class contains nested classes for each resource type, such as:
R.id: for view IDsR.attr: for custom attributesR.menu: for menu resourcesR.drawable: for drawable resourcesR.string: for string resourcesR.anim: for animation resourcesR.animator: for animator resourcesR.array: for array resourcesR.bool: for boolean resourcesR.color: for color resourcesR.dimen: for dimension resourcesR.fraction: for fraction resourcesR.integer: for integer resourcesR.interpolator: for interpolator resourcesR.layout: for layout resourcesR.mipmap: for mipmap resourcesR.plurals: for plural resourcesR.raw: for raw resourcesR.style: for style resourcesR.transition: for transition resourcesR.xml: for XML resources