AS中Module中没法使用Butterknife.有什么好的替代?

发布于 2022-09-03 09:06:49 字数 1762 浏览 20 评论 0

说是Module中的R文件下的id不是final的。然后Butterknife就不行。。。
估计别的注解类型的也不好使吧。。。于是乎有什么办法么。。

详见:https://github.com/JakeWharton/butterknife/issues/100

以下是作者JakeWharton在14年12月的原话。。。

Yes this is not supported. This is a tradeoff between ease-of-use and what I tolerate as a sane API.

The only way to accomplish something like this is to allow string values like this:

@InjectView(name = "content_frame") protected FrameLayout
contentFrame; The downside's to this are:

The downside's to this are:

  • It's not rename-safe or compile-safe (kind of). If you open the layout XML for this and do an IDE-backed rename operation it will not catch this string and update it. It also means you can (technically) compile the above code if there is no content_frame ID that exists. Now the compilation will eventually fail because the generated code will become invalid.

  • It's hard to know what R class to reference in the generated code, especially when you take into consideration the fact that you can use references to IDs that exist in libraries that you are referencing.

  • It forces us to have defaults for both the value and name properties on each annotation which means you can write @InjectView Foo foo and Butter Knife will have to fail the builder rather than javac.

Because of these facts, I have chosen not to support library projects.

One way that this could potentially be solved is to use a Gradle plugin rather than an annotation processor. I don't have the time to explore something like that for a few months though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

披肩女神 2022-09-10 09:06:49

Android Studio Prettify

这个插件自动生成findViewById等内容,你可以试试。
你说Butterknife不能用,应该是你用错了吧,你仔细看看是不是用错了。

慕烟庭风 2022-09-10 09:06:49

module中好像也没看到别的,还是直接使用findViewById吧

海拔太高太耀眼 2022-09-10 09:06:49

现在基本不用butterknife了,直接封装一个方法在BaseActivity或BaseFragment

//BaseActivity
public <T extends View> T $(@IdRes int resId){
    return (T)super.findViewById(resId);
}
//BaseFragment
public <T extends View> T $(View layoutView, @IdRes int resId){
    return (T)layoutView.findViewById(resId);
}
//使用
TextView tvName=$(R.id.tv_name);
香橙ぽ 2022-09-10 09:06:49

最好使用系统自带的findviewyid来查找id,第三方的工具之间使用会存在冲突

感性 2022-09-10 09:06:49

RoboGuice?或者android annotation?

婴鹅 2022-09-10 09:06:49

图片描述

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文