23

The Kotlin Android Extensions plugin generates static properties for each view with an ID from my app layout files, as it is described in the documentation.

I would like to disable this feature, because we use DataBinding instead of the Kotlin synthetic view properties, and sometimes I import them by accident; also it adds extra build overhead for something we don't use.

Disabling the Android extensions plugin is not possible, because we use the Parcelize feature which is done by the same plugin.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • For the importing by accident issue, one mitigation is to edit your Android Studio settings in Settings > Editor > General > Auto Import, and add to the excluded packages list. – Can_of_awe Jan 14 '19 at 16:21

2 Answers2

24

There is a features property in the androidExtensions DSL that allows you to provide a list of features to enable. Currently, there is only two available, parcelize and views. To disable synthetic view properties, add this to your build.gradle:

android {
   // ...
}

androidExtensions {
    features = ["parcelize"]
}

Source: https://github.com/JetBrains/kotlin/blob/6bef27e1e889b17ae84dd2ff99881727f13ac3e5/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt#L57

Westy92
  • 19,087
  • 4
  • 72
  • 54
kyhule
  • 651
  • 2
  • 6
  • 12
  • 1
    Thanks, this works almost fully, my only problem is that it still generates the synthetic classes. But if you try to compile the app, it fails. – Daniel Zolnai Apr 12 '19 at 06:35
  • 10
    This doesn't change anything for me, on Android Studio 3.4. References to the synthetic objects still compile and function. – Joe Lapp May 12 '19 at 22:33
  • 4
    This works for me on Android Studio 3.5.3. Note that `androidExtensions` is a top-level closure, meaning it goes outside of the `android` closure. – CommonsWare Jan 02 '20 at 00:15
  • 2
    This does not work for me on Android Studio 4.0 Beta 5, even as a top level closure. It's still generating the synthetic properties – PhillyTheThrilly May 14 '20 at 17:03
  • @kyhule what if we don't use parcelize either? Would `features = []` suffice? – c_idle Jun 30 '20 at 02:14
  • @coltonidle That was my case, I just removed the whole `apply plugin: 'kotlin-android-extensions'` line, it works perfectly. – John Jul 13 '20 at 11:56
  • I don't have that plugin at all. – c_idle Jul 14 '20 at 22:30
  • I tried the same on android studio 4.0.1 and it does work. It seems in editor we can see the Kotlin synthetics but when I run the code it gives error "Unresolved reference: synthetic". At least no one in the team will be able to use it by mistake. – Hitesh Bisht Aug 01 '20 at 12:26
11

Nowadays, android-extensions plugin is discontinued so the best solution will be to just remove this plugin by removing apply plugin: 'kotlin-android-extensions' from your build.gradle.

Instead of synthetics we should use ViewBinding or DataBinding.

As explained in the first link, if you're also using parcelizer, you just need to change android-extensions plugin to kotlin-parcelize and change the import statement import kotlinx.android.parcel.Parcelize for import kotlinx.parcelize.Parcelize wherever is needed.

More info to migrate from android-extensions to JetPack here.

pauminku
  • 3,526
  • 3
  • 22
  • 24