最近引入一个第三方,需要使用androidx扩展库,compile后发现与旧的support库无法兼容,于是逐个的把support的库换成了androidx系列,倒腾半天,失败无数次,不过最终还是将它拿下了[/得意]。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iv_head" />
</androidx.constraintlayout.widget.ConstraintLayout>
以上这个布局文件不过是把最外层的布局更换为了androidx的ConstraintLayout,结果在Adapter中inflater的时候,一直报错,android.view.InflateException: Binary XML file line #19: Error inflating class android.view.TextView,看起来这个xml很正常,没毛病啊。尝试将外层布局更换为线性布局,然并卵。经过查看发现项目中有一个adapter也用了constraintlayout和TextView然而那个页面确实正常运行,不闪退。代码比对很多次也没发现问题所在。后来去build.gradle鬼使神差的根据提示把implementation 'androidx.appcompat:appcompat:1.2.0-beta01’由之前的implementation 'androidx.appcompat:appcompat:1.2.0-alpha02’改为beta01,再次运行后,奇迹般的没事儿了!为什么有的页面就行呢?可能是之前版本的一个bug。具体也没研究。自己水平还有待提高啊。突然想起来前几天看到一个程序员大牛在用一个开源的性能分析工具分析app的某个问题时候,始终没找到一个奇怪的问题的原因,于是他把那个开源分析工具代码从头到尾看了一遍竟然发现是这个工具的bug导致的,当时就震惊了!太难以置信了!佩服!
看到build.gradle文件的内容发现这些androidx扩展库有的还停留在alpha和beta阶段,alphabet公司竟然就推给开发者使用了,WTH!就不能稳定再推出androidx吗?本来还能愉快的开发,这一更新都得和开发环境干上半天,半天下去发现时间所剩无几,大好时光都浪费在无意义的事情上面了,不知道ab公司是怎么想的,希望他们可以重视一下android开发者的感受,稳定后再推广,善待android开发人员。
如果需要迁移请参考这篇文章AndroidX迁移,最终迁移成功。
- 导入项目运行出错,提示
The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten.
Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.
在全局搜索(ctrl + shift + f)发现许多文件都在用旧的v4,v7等支持包的类,如果逐个修改的话,工作量实在是太大了,在Studio中点击菜单Refactor -> Migrate to AndroidX即可出现提示,是否全部修改,点击do Refactor就可以全部改掉了。可以根据提示备份backup一下项目到zip文件,更稳妥。