我们可以在 Android 中创建自定义合成 Kotlin 扩展吗?
Can we create custom Synthetic Kotlin Extensions in Android?
我对 Android 的 Kotlin 合成扩展很感兴趣,并且想我们是否可以对自定义文件做同样的事情,比如我们保留在项目中的原始 XML 文件。例如,让我们考虑一下 Kotlin 中的合成视图。
import kotlinx.android.synthetic.main.fragment_profile_management.*
textview_shop_name.text = merchant.establishmentName
生成的代码是这样的:
TextView textView = (TextView) _$_findCachedViewById(com.goharsha.testapp.R.id.textview_shop_name);
Intrinsics.checkExpressionValueIsNotNull(textView, "textview_shop_name");
Merchant merchant3 = this.merchant;
if (merchant3 == null) {
Intrinsics.throwUninitializedPropertyAccessException("merchant");
}
textView.setText(merchant3.getEstablishmentName());
并且_$_findCachedViewById
方法也被生成为相同的class如下:
private HashMap _$_findViewCache;
public View _$_findCachedViewById(int i) {
if (this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View view = (View) this._$_findViewCache.get(Integer.valueOf(i));
if (view != null) {
return view;
}
View view2 = getView();
if (view2 == null) {
return null;
}
View findViewById = view2.findViewById(i);
this._$_findViewCache.put(Integer.valueOf(i), findViewById);
return findViewById;
}
因为这是 Android 特有的,我猜这可以在 Kotlin 中完成,也许我可以将其扩展为自定义原始 XML 文件,例如配置文件,并将它们解析为一个对象可能很有趣。
但是,我找不到如何做到这一点。我知道 Kotlin 中的扩展函数,但在这里,一个完整的文件是基于导入综合生成的。还有这个神奇的是,当我反编译应用程序时,没有找到这个 Kotlin import。
我也尝试查看 core-ktx 和 view-ktx 库,但到目前为止还没有成功。知道如何做到这一点吗?
是的,可以做到这一点,您需要做三件事才能完成。
- 写一个Gradle插件
- 编写 Kotlin 编译器插件(API 未记录,所以准备好迎接真正的挑战)
- 编写一个 IntelliJ 插件(用于在合成字段上导航到 XML 文件的 go-to 等功能)
这些合成视图生成是在这条路线上完成的。如果您想尝试制作这样的东西,这里有一些资源。
我对 Android 的 Kotlin 合成扩展很感兴趣,并且想我们是否可以对自定义文件做同样的事情,比如我们保留在项目中的原始 XML 文件。例如,让我们考虑一下 Kotlin 中的合成视图。
import kotlinx.android.synthetic.main.fragment_profile_management.*
textview_shop_name.text = merchant.establishmentName
生成的代码是这样的:
TextView textView = (TextView) _$_findCachedViewById(com.goharsha.testapp.R.id.textview_shop_name);
Intrinsics.checkExpressionValueIsNotNull(textView, "textview_shop_name");
Merchant merchant3 = this.merchant;
if (merchant3 == null) {
Intrinsics.throwUninitializedPropertyAccessException("merchant");
}
textView.setText(merchant3.getEstablishmentName());
并且_$_findCachedViewById
方法也被生成为相同的class如下:
private HashMap _$_findViewCache;
public View _$_findCachedViewById(int i) {
if (this._$_findViewCache == null) {
this._$_findViewCache = new HashMap();
}
View view = (View) this._$_findViewCache.get(Integer.valueOf(i));
if (view != null) {
return view;
}
View view2 = getView();
if (view2 == null) {
return null;
}
View findViewById = view2.findViewById(i);
this._$_findViewCache.put(Integer.valueOf(i), findViewById);
return findViewById;
}
因为这是 Android 特有的,我猜这可以在 Kotlin 中完成,也许我可以将其扩展为自定义原始 XML 文件,例如配置文件,并将它们解析为一个对象可能很有趣。
但是,我找不到如何做到这一点。我知道 Kotlin 中的扩展函数,但在这里,一个完整的文件是基于导入综合生成的。还有这个神奇的是,当我反编译应用程序时,没有找到这个 Kotlin import。
我也尝试查看 core-ktx 和 view-ktx 库,但到目前为止还没有成功。知道如何做到这一点吗?
是的,可以做到这一点,您需要做三件事才能完成。
- 写一个Gradle插件
- 编写 Kotlin 编译器插件(API 未记录,所以准备好迎接真正的挑战)
- 编写一个 IntelliJ 插件(用于在合成字段上导航到 XML 文件的 go-to 等功能)
这些合成视图生成是在这条路线上完成的。如果您想尝试制作这样的东西,这里有一些资源。