美文网首页
AndroidX迁移小记

AndroidX迁移小记

作者: binlees | 来源:发表于2020-06-11 14:46 被阅读0次

    AndroidX迁移小记

    为什么要迁移AndroidX?因为Android 现有的软件包,如Android支持库,正在被重构为Androidx。尽管在Google Maven上仍然提供支持库版本27及更低版本,但所有新开发将只包含在Androidx 1.0.0及更高版本中。AndroidX随后将成为主流,越来越多的主流的第三库和Google的原生库将仅仅支持AndroidX版本上的迭代和更新。

    1.如何迁移

    1).环境准备:AndroidX的迁移要求在在AndroidStudio 3.2 或更高版本中执行,要求的targetSdkVersion版本为28(android9.0),classpath 'com.android.tools.build:gradle:3.2.0'要3.2.0或者更高。
    2).修改gradle.properties:修改项目根目录gradle.properties文件,添加配置
    android.useAndroidX=true  表示启用 
    androidxandroid.enableJetifier=true  表示将依赖包迁移到androidx 。如果取值为false,表示不迁移依赖包到androidx,但在使用依赖包中的内容时可能会出现问题,如果项目中没有使用任何三方依赖,可以设置为false。
    
    3).启动一键迁移:在AS中执行Refactor->Migrate to AndroidX 迁移后执行 Flie -> Invalidate Caches /Restart 一下,确保依赖中的缓存文件都已更新。

    2.修改项目代码

    AS中虽然提供了一键迁移插件工具,但是替换效果并不好。因此,在使用一键迁移完成后,还需要手动修改项目中出错的地方。

        /**
        *这里是旧版本的support依赖库
        */
        "support-v4"         : "com.android.support:support-v4:${dependVersion.support}",
        "appcompat-v7"        : "com.android.support:appcompat-v7:${dependVersion.support}",
        "design"              : "com.android.support:design:${dependVersion.support}",
        "recyclerview"        : "com.android.support:recyclerview-v7:${dependVersion.support}",
        "cardview"            : "com.android.support:cardview-v7:${dependVersion.support}",
        "annotations"         : "com.android.support:support-annotations:${dependVersion.support}",
        "constraint-layout"   : "com.android.support.constraint:constraint-layout:1.0.2",
        
        
        /**
        *AndroidX版本对应的依赖库
        */
        "support-v4"          : "androidx.legacy:legacy-support-v4:1.0.0",
        "appcompat-v7"        : "androidx.appcompat:appcompat:1.0.2",
        "design"              : "com.google.android.material:material:1.1.0-alpha09",
        "recyclerview"        : "androidx.recyclerview:recyclerview:1.0.0",
        "cardview"            : "androidx.cardview:cardview:1.0.0",
        "annotations"         : "androidx.annotation:annotation:1.0.0",
        "constraint-layout"   : "androidx.constraintlayout:constraintlayout:1.1.3",
        
    
    Activity/Fragment等Java文件和XML布局文件改动Activity/Fragment/XML(包括涉及到使用support包的工具类等),原来引用support包中的类,在Migrate后并不能完全对应,会有很多错误,所以需要改成对应的androidX中的类引用。如果挨个删掉原有的引用后,再重新导入正确的引用包,工作量会非常繁重。因此,可以使用全局替换: Edit ->Find -> Replace in path 。整理出来常见的对应关系如下:
    android.support.annotation.Nullable                  androidx.annotation.Nullable
    android.support.annotation.NonNull                   androidx.annotation.NonNull;
    androidx.appcompat.widget.GridLayoutManager          androidx.recyclerview.widget.GridLayoutManager
    androidx.appcompat.widget.RecyclerView               androidx.recyclerview.widget.RecyclerView;
    androidx.appcompat.widget.LinearLayoutManager        androidx.recyclerview.widget.LinearLayoutManager
    androidx.appcompat.widget.LinearSmoothScroller       androidx.recyclerview.widget.LinearSmoothScroller
    androidx.appcompat.widget.OrientationHelper          androidx.recyclerview.widget.OrientationHelper
    androidx.appcompat.widget.SnapHelper                 androidx.recyclerview.widget.SnapHelper
    androidx.core.app.FragmentTransaction                androidx.fragment.app.FragmentTransaction
    androidx.core.app.Fragment;                          androidx.fragment.app.Fragment;
    androidx.core.app.FragmentManager                    androidx.fragment.app.FragmentManager;
    androidx.core.app.FragmentStatePagerAdapter          androidx.fragment.app.FragmentStatePagerAdapter
    androidx.core.view.ViewPager                         androidx.viewpager.widget.ViewPager
    androidx.core.app.FragmentActivity                   androidx.fragment.app.FragmentActivity
    android.support.annotation.ColorInt                  androidx.annotation.ColorInt
    android.support.annotation.ColorRes                  androidx.annotation.ColorRes
    android.support.annotation.FloatRange                androidx.annotation.FloatRange
    android.support.annotation.AttrRes                   androidx.annotation.AttrRes
    android.support.design.widget.AppBarLayout           com.google.android.material.appbar.AppBarLayout
    android.support.design.widget.CoordinatorLayout      androidx.coordinatorlayout.widget.CoordinatorLayout
    

    布局XML文件里面,也不会自动替换,需要手动替换

    <androidx.constraintlayout.ConstraintLayout/>             <androidx.constraintlayout.widget.ConstraintLayout/>
    <android.support.design.widget.CollapsingToolbarLayout/>  <com.google.android.material.appbar.CollapsingToolbarLayout/>
    <android.support.v7.widget.Toolbar/>                      <androidx.appcompat.widget.Toolbar/>
    <android.support.v4.view.ViewPager/>                      <androidx.viewpager.widget.ViewPager/>
    <android.support.v7.widget.RecyclerView/>                 <androidx.recyclerview.widget.RecyclerView/>
    

    混淆配置

    修改前的混淆文件是针对support库做的混淆配置,在完成迁移后,需要添加对AndroidX的混淆配置

    -keep class com.google.android.material.** {*;}
    -keep class androidx.** {*;}
    -keep public class * extends androidx.**
    -keep interface androidx.** {*;}
    -dontwarn com.google.android.material.**
    -dontnote com.google.android.material.**
    -dontwarn androidx.**
    

    查漏补缺

    全局替换也可能有少数遗漏或者错误的情况,可以再全局Find in Path ,使用support关键字全局查找,再做替换
    替换完成后,进行Rebuild,如果有错误,编译失败。再进行手动修改,经过几次编译尝试后,最终达到编译成功,
    表示项目代码修改完成。

    其他问题(第三方依赖库有报错)

    • 1、检查第三方依赖包,如果第三方依赖包支持AndroidX的更新,那我们可以直接将第三部直接更新到最新版本即可。
    • 2、如果第三方不支持AndroidX版本,我们需要寻找一个其他支持AndroidX的同样功能的框架,或者直接将第三方依赖clone下来,自己修改成AndroidX的版本。
    参考 还在用android.support?该考虑迁移AndroidX了!AndroidX终极迁移指南Android AndroidX的迁移

    相关文章

      网友评论

          本文标题:AndroidX迁移小记

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