When I add an import of the form kotlinx.android.synthetic.main.<layout-name>.view.*
to a Kotlin source it alters the behavior of the Android Studio editor. Specifically, it now considers any property of type View
to have a property corresponding to each view in the layout file that is assigned an id. I am assuming that I am doing this in a class that is not an Activity
or Fragment
.
For example, say I have a ViewHolder
declared in the source file and I add a property to it called x
of type View
. Further, say that I have a layout named item_test
that has three view declarations that have been assigned the ids, a
, b
, and c
. When I add a synthetic import of the form above with a layout-name of item_test
suddenly x
has three new properties, a
, b
, and c
. Or, more correctly, the editor makes it appear as though x
has these properties.
After some research, I have concluded the following:
The addition of layout view ids as properties is done blindly. Any view id implied by a such a synthetic import is added to any property of the class that is of type
View
(or a subclass ofView
). This includes properties of such type inherited by the class.Because the properties are added blindly it is incumbent upon the developer to ensure that the runtime view corresponding to the synthetic import is assigned before the synthetic properties are accessed or an exception will be thrown.
If two or more such imports are specified in a file then the union of their view ids will be blindly added to all class properties of type
View
.The purpose of allowing multiple imports is to account for the case where one layout file includes another.
Are these conclusions correct?
Are any other interesting subtleties around the implementation of this feature?
I am using version 1.1.2-5 of the kotlin-gradle-plugin.