简体   繁体   English

如何通过 Kotlin Android 扩展插件禁用合成视图属性的生成

[英]How to disable the generating of synthetic view properties by the Kotlin Android extensions plugin

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 . Kotlin Android 扩展插件使用我的应用程序布局文件中的 ID 为每个视图生成静态属性,如文档所述

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;我想禁用这个功能,因为我们使用 DataBinding 而不是 Kotlin 合成视图属性,有时我会不小心导入它们; 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.禁用 Android 扩展插件是不可能的,因为我们使用由同一插件完成的 Parcelize 功能。

There is a features property in the androidExtensions DSL that allows you to provide a list of features to enable. androidExtensions DSL 中有一个features属性,允许您提供要启用的功能列表。 Currently, there is only two available, parcelize and views .目前,只有两个可用, parcelizeviews To disable synthetic view properties, add this to your build.gradle :要禁用综合视图属性,请将其添加到您的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资料来源: https : //github.com/JetBrains/kotlin/blob/6bef27e1e889b17ae84dd2ff99881727f13ac3e5/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/AndroidComponentRegistrar.kt

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 .如今,插件是Android系统的扩展停产所以最好的解决方法是将只通过删除删除此插件apply plugin: 'kotlin-android-extensions'从你build.gradle

Instead of synthetics we should use ViewBinding or DataBinding .我们应该使用ViewBindingDataBinding而不是合成。

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.如第一个链接中所述,如果您还使用parcelizer,则只需将android-extensions插件更改为kotlin-parcelize并更改 import 语句import kotlinx.android.parcel.Parcelizeimport kotlinx.parcelize.Parcelize无论在哪里需要。

More info to migrate from android-extensions to JetPack here .有关从 android-extensions 迁移到 JetPack 的更多信息,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM