


There are still projects that are written in Java and may not be fully migrated to Kotlin yet, and hence Kotlin Synthetics may not be a consistent way of getting the ViewIds in your project.īecause of these issues, Kotlin Synthetics is now being deprecated. Kotlin only - Kotlin Synthetics can only be used with Kotlin. Pollutes the namespace - You can have the same view ID in different layouts, and you can accidentally import a view of some other layout, which will later at runtime throw NullPointerException. But what if you have multiple layout files based on configurations where some views are present in some layout files and missing in some? Here you have to then manually check for null. Partial null safety - It is generally null safe as all your views must be present in the layout file for it to generate. Type safety - All views are picked from your layout file with the type known to it. Kotlin Synthetics will automatically generate a class ready for you to use. No boilerplate code - Just configure it once in your Gradle file and you are all set to go. This was so convenient and such fun to use, so why is it being deprecated It calls findViewById internally only once and then caches it. You just have to import this plugin in your Gradle file, and you are all set to directly refer to the view reference variables. For every layout file, Kotlin Synthetics creates an autogenerated class containing your view- as simple as that.

In my opinion, using this plugin is still better than the methods introduced, we just need to pay attention to IDs.īecause With the Android Kotlin Extensions Gradle plugin released in 2017 came Kotlin Synthetics.
