Androidx的使用

作者: 奔跑吧李博 | 来源:发表于2020-05-27 14:15 被阅读0次

    为什么要有Androidx库?

    也许是google官方也对这个早期形成的演化产物不满意,于是对这些API的架构进行了一次重新的划分,推出了AndroidX。Androidx 是对 android.support.xxx包和其他Android支持库整理后的产物。与support库一样,每个AndroidX库都与Android系统版本分开提供,并提供跨Android版本的向后兼容性。

    对开发者造成什么影响?

    对于有写一些开源项目的人,会有一些影响。一些项目会找不到原有库,一些项目会往Androidx上迁移。Android团队官方的态度也很明确,未来都会为AndroidX为主,Android Support Library已经不再建议使用,并会慢慢停止维护,所以陆续会有众多的Android开发者将项目迁移到Androidx库上。

    怎么使用Androidx?

    1.现如今新创建的Android项目已经是强制勾选使用androidx库了。
    2.原有项目迁移到Androidx。

    使用Android studio一键迁移的功能,Refactor -> Migrate to AndroidX


    步骤一

    为避免迁移之后出现不好挽回的错误导致不能编译,所以可以在迁移之前将项目备份。


    步骤二

    一键迁移Androidx库之后,在项目gradle.properties中会增加如下代码:

    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    

    使用后有什么变化?

    会将原有部分库自动替换为Androidx下对应的库,部分主流使用的包替换一览:

    原包名 新包名
    android.arch.lifecycle:livedata androidx.lifecycle:lifecycle-livedata
    android.arch.lifecycle:viewmodel androidx.lifecycle:lifecycle-viewmodel
    com.android.support.constraint:constraint-layout androidx.constraintlayout:constraintlayout
    com.android.support:appcompat-v7 androidx.appcompat:appcompat
    com.android.support:cardview-v7 androidx.cardview:cardview
    com.android.support:design com.google.android.material:material
    com.android.support:interpolator androidx.interpolator:interpolator
    com.android.support:recyclerview-v7 androidx.recyclerview:recyclerview
    com.android.support:support-annotations androidx.annotation:annotation
    com.android.support:support-fragment androidx.fragment:fragment
    com.android.support:support-media-compat androidx.media:media
    com.android.support:swiperefreshlayout androidx.swiperefreshlayout:swiperefreshlayout
    com.android.support:transition androidx.transition:transition
    com.android.support:viewpager androidx.viewpager:viewpager
    com.android.support:webkit androidx.webkit:webkit

    也许你感觉刚开始接触Androidx时不怎么习惯,可是使用某个类是自动引包的,并不需要你去记忆新的包路径是什么,那应该不会对你的编程水平造成影响吧?

    相关文章

      网友评论

        本文标题:Androidx的使用

        本文链接:https://www.haomeiwen.com/subject/xksxoctx.html