https://youtrack.jetbrains.com/issue/KT-42121
As the @Parcelize functionality is extracted (KT-42120), the rest of the Android Extensions functionality can be deprecated in favour of View Binding.
The existing Android Extensions plugin will continue to work, however, a warning message will be shown.
记得曾经在《谁才是ButterKnife的终结者?》一文中大胆猜测ViewBinding将替代KAE,果然被不幸言中,近期KAE被正式废弃。
KAE被废弃是因为存在以下几方面问题:
KAE被废的同时官方也给出了解决方案:迁移至ViewBinding。因为ViewBinding解决了KAE的主要缺陷:Null safety 与 Type safety,安全性更高。
开启ViewBinding很简单,只需在build.gradle
中增加一下配置
android {
...
buildFeatures {
viewBinding true
}
}
ViewBinding会在编译期根据layout文件生成对应的class
<LinearLayout ... >
<TextView android:id="@+id/name" />
<ImageView android:cropToPadding="true" />
<Button android:id="@+id/btn"
android:background="@drawable/rounded_button" />
LinearLayout>
针对上面activity_main.xml
,会生成ActivityMainBinding
的class,
public final class ActivityMainBinding implements ViewBinding {
@NonNull
public final TextView name;
@NonNull
public final Button btn;
@NonNull
public View getRoot() {}
}
两个成员name
和btn
,分别对应layout中的TextView和Button。getRoot()
方法返回layout的跟节点,上例中即LinearLayout
<LinearLayout
...
tools:viewBindingIgnore="true" >
...
LinearLayout>
通过tools:viewBindingIgnore="true"
可以禁止ViewBinding的生成,所以class中没有ImageView对应的成员
在Activity中创建ViewBinding实例
private lateinit var binding: ActivityMainBinding
override fun onCreate (savedInstanceState: Bundle?) {
super.onCreate (savedInstanceState)
binding = ActivityMainBinding.inflate (layoutInflater)
val view = binding.root
setContentView (view)
}
并在适当的时候访问其成员
binding.name.text = viewModel.name
binding.btn.setOnClickListener {viewModel.userClicked ()}
可以在Fragment的onCreateView
中创建binding,然后在onDestroyView
中销毁
override fun onCreateView (
inflater: LayoutInflater,
container: ViewGroup ?,
savedInstanceState: Bundle?
): View? {
_binding = ActivityMainBinding.inflate (inflater, container, false)
val view = binding.root
return view
}
override fun onDestroyView () {
super.onDestroyView ()
_binding = null
}