老项目适配androidx,出现了一些问题,汇总一下。
问题1:ButterKnife.apply
爆红,找不到该方法。
解决:由于近期一直使用的是databinding
,所以对ButterKnife
没有太关注,直到适配androidx。适配使用了ButterKnife
最新版10.2.1
,发现ButterKnife.apply
找不到。查看JakeWharton
大神的更新日志,只发现了Is Setter/Action removed in 10.0.0? #1428 Issues
,并没有找到新旧版本的差异。 查看源码发现:
JakeWharton
大佬将Set , Action
单独出来了,通过ViewCollections
调用。
旧代码:
ButterKnife.apply(new View[]{llYears, llDataProductSubmit, tvSoftwareSubmit}, new ButterKnife.Setter<View, Boolean>() {
@Override
public void set(@NonNull View view, Boolean value, int index) {
if (index == 2) {
view.setVisibility(value ? View.GONE : View.VISIBLE);
} else {
view.setVisibility(value ? View.VISIBLE : View.GONE);
}
}
}, mDataProductBean != null);
新代码:
ViewCollections.set(new View[]{llYears, llDataProductSubmit, tvSoftwareSubmit}, new Setter<View, Boolean>() {
@Override
public void set(@NonNull View view, @Nullable Boolean value, int index) {
if (index == 2) {
view.setVisibility(value ? View.GONE : View.VISIBLE);
} else {
view.setVisibility(value ? View.VISIBLE : View.GONE);
}
}
}, mDataProductBean != null);
问题2:一把辛酸泪,,一个androidlib 库的R2文件中有2个相同的变量
全局搜索CompoundButton_android_button
始终没找到,甚至一度怀疑是不是依赖包冲突了。直到后来:
罪魁祸首:
<declare-styleable name="CompoundButton">
<attr name="android:button" />
</declare-styleable>
这么简单的问题,搞了好久。血一般的教训,撸代码,还是要细心啊~~