迁移AndroidX!!!

作者: 从心开始的我 | 来源:发表于2020-07-15 19:57 被阅读0次

    参考链接
    本次迁移主要参考上面谷歌官方公众号文章,迁移背景建议点进文章进行查看,本文主要提及一些迁移过程和其中遇到的坑

    重要提醒

    迁移开始之前,不管你的项目用的是svn还是git 都确认本地代码无修改并且是分支上最新代码,建议在电脑上新建个文件夹,把分支上代码拉下来专门做androidX迁移,这样到时候既是迁移不成功,把该文件夹删掉就行了,跟你的小伙伴们商量好迁移的事项,像我这一个人的话,就随便了

    如果你的项目不是使用AndroidStudio开发的本文中的一些步骤可能不太适用,确保AndroidStudio使用3.5+版本

    关于Gradle版本问题

    如果你的项目使用的是 美团的walle 打包,不要把gradle版本升级到最新的4.0,停留在3.63就行.问题一大堆了

    如图

    gradle配置参考

    参考示例
    项目的gradle文件
    dependencies {
            classpath "com.android.tools.build:gradle:3.6.3"
        }
    
    项目的gradle文件夹
    gradle文件夹
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
    

    迁移步骤

    迁移步骤示意图

    1,将项目中所有的Support库升级到28

    参考版本和示例

        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:support-core-ui:28.0.0'
        implementation 'com.android.support:support-core-utils:28.0.0'
        implementation 'com.android.support:support-fragment:28.0.0'
        implementation 'com.android.support:support-vector-drawable:28.0.0'
        implementation 'com.android.support:animated-vector-drawable:28.0.0'
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support:support-v4:28.0.0'
        implementation 'com.android.support:design:28.0.0'
        implementation 'com.android.support:recyclerview-v7:28.0.0'
        implementation 'com.android.support:cardview-v7:28.0.0'
    

    这一步应该没问题,无非就是涉及到一些Api的改动

    2,开启 Jetifier

    项目的gradle.properties添加
    
    android.useAndroidX=true
    android.enableJetifier=true
    
    修改好之后,确保 clean rebuild run 都没问题 在进行下一步

    3,将项目依赖的所有第三方库全部升级

    个人认为这一步最重要,当然选择第三方sdk要适用于你的项目,比如okhttp4,rxjava3等底层都换了其实用rx2最新的版本就行,glide等改动不太大的直接升级最新版本就行

    如果检测到第三方SDK还没有适配AndroidX,并且是项目必须依赖的,那么就此放弃,以下内容不用再看了!!!




    ---3.1前方巨坑,坑了我两天

    因为项目中使用了 支付宝sdk,微信支付登录sdk,大厂自然不用说,不存在一些java版本的兼容问题

    但是 我们还是用了汇付宝

    之前项目中一直使用的是汇付宝3.0的版本,最新的版本是4.0,这次升级sdk时遗漏了汇付宝的sdk升级 导致最终打包时一直报错,说某个方法不存在之类的,吭哧了两天 ,从头开始弄才得以解决

    3.2升级Butterknife

    butterknife从10.0开始已经支持androidx了,我们直接升级到最新的10.2.1版本即可,需要在app下的gradle中配置编译jdk版本

    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    

    JDK8下载: 官网下载地址,不知道从几何时下载jdk也需要登录账号了,注册账号的流程又TM那么复杂,好在前人已经填坑了,直接给出的自己注册的账号

    1772885836@qq.com 
    OracleTest1234
    
    或者
    2696671285@qq.com 
    密码:Oracle123
    

    账号引用链接,再次感谢

    3.3升级BaseRecyclerViewAdapterHelper

    最新的3.0.4支持androidx并且用kotlin重写,但是一些api改动比较大,
    像item中view的点击事件,加载更多等,一想到项目中那么多列表 我滴个亲娘嘞,没办法升级总是痛苦的,就是个复制粘贴的工作,干就得了奥利给!

    修改好之后,确保 clean rebuild run 都没问题 在进行下一步

    4,使用AndroidStudio 将Support库转换AndroidX

    Migrate to AndroidX


    AndroidStudio导航

    过程中会提示你备份当前工程,备份一下就进行下一步,经过一段时间后成功了,

    需要注意的是,AndroidStudio转换后有时候并没有把所有的XML布局都能转换成功,有一些布局文件会遗漏

    这个时候就需要我们自己手动进行替换

    4.1库替换

    官方提供库的映射表,可以用Ctrl+F 在本页面查找.找到对应的库并且选择合适的版本

    Old build artifact AndroidX build artifact
    android.arch.lifecycle:common-java8 androidx.lifecycle:lifecycle-common-java8
    android.arch.lifecycle:compiler androidx.lifecycle:lifecycle-compiler
    android.arch.lifecycle:extensions androidx.lifecycle:lifecycle-extensions
    android.arch.lifecycle:livedata androidx.lifecycle:lifecycle-livedata
    android.arch.lifecycle:livedata-core androidx.lifecycle:lifecycle-livedata-core
    android.arch.lifecycle:reactivestreams androidx.lifecycle:lifecycle-reactivestreams
    android.arch.lifecycle:runtime androidx.lifecycle:lifecycle-runtime
    android.arch.lifecycle:viewmodel androidx.lifecycle:lifecycle-viewmodel
    android.arch.paging:common androidx.paging:paging-common
    android.arch.paging:runtime androidx.paging:paging-runtime
    android.arch.paging:rxjava2 androidx.paging:paging-rxjava2
    android.arch.persistence.room:common androidx.room:room-common
    android.arch.persistence.room:compiler androidx.room:room-compiler
    android.arch.persistence.room:guava androidx.room:room-guava
    android.arch.persistence.room:migration androidx.room:room-migration
    android.arch.persistence.room:runtime androidx.room:room-runtime
    android.arch.persistence.room:rxjava2 androidx.room:room-rxjava2
    android.arch.persistence.room:testing androidx.room:room-testing
    android.arch.persistence:db androidx.sqlite:sqlite
    android.arch.persistence:db-framework androidx.sqlite:sqlite-framework
    com.android.support.constraint:constraint-layout androidx.constraintlayout:constraintlayout
    com.android.support.constraint:constraint-layout-solver androidx.constraintlayout:constraintlayout-solver
    com.android.support.test.espresso.idling:idling-concurrent androidx.test.espresso.idling:idling-concurrent
    com.android.support.test.espresso.idling:idling-net androidx.test.espresso.idling:idling-net
    com.android.support.test.espresso:espresso-accessibility androidx.test.espresso:espresso-accessibility
    com.android.support.test.espresso:espresso-contrib androidx.test.espresso:espresso-contrib
    com.android.support.test.espresso:espresso-core androidx.test.espresso:espresso-core
    com.android.support.test.espresso:espresso-idling-resource androidx.test.espresso:espresso-idling-resource
    com.android.support.test.espresso:espresso-intents androidx.test.espresso:espresso-intents
    com.android.support.test.espresso:espresso-remote androidx.test.espresso:espresso-remote
    com.android.support.test.espresso:espresso-web androidx.test.espresso:espresso-web
    com.android.support.test.janktesthelper:janktesthelper androidx.test.jank:janktesthelper
    com.android.support.test.services:test-services androidx.test:test-services
    com.android.support.test.uiautomator:uiautomator androidx.test.uiautomator:uiautomator
    com.android.support.test:monitor androidx.test:monitor
    com.android.support.test:orchestrator androidx.test:orchestrator
    com.android.support.test:rules androidx.test:rules
    com.android.support.test:runner androidx.test:runner
    com.android.support:animated-vector-drawable androidx.vectordrawable:vectordrawable-animated
    com.android.support:appcompat-v7 androidx.appcompat:appcompat
    com.android.support:asynclayoutinflater androidx.asynclayoutinflater:asynclayoutinflater
    com.android.support:car androidx.car:car
    com.android.support:cardview-v7 androidx.cardview:cardview
    com.android.support:collections androidx.collection:collection
    com.android.support:coordinatorlayout androidx.coordinatorlayout:coordinatorlayout
    com.android.support:cursoradapter androidx.cursoradapter:cursoradapter
    com.android.support:customtabs androidx.browser:browser
    com.android.support:customview androidx.customview:customview
    com.android.support:design com.google.android.material:material
    com.android.support:documentfile androidx.documentfile:documentfile
    com.android.support:drawerlayout androidx.drawerlayout:drawerlayout
    com.android.support:exifinterface androidx.exifinterface:exifinterface
    com.android.support:gridlayout-v7 androidx.gridlayout:gridlayout
    com.android.support:heifwriter androidx.heifwriter:heifwriter
    com.android.support:interpolator androidx.interpolator:interpolator
    com.android.support:leanback-v17 androidx.leanback:leanback
    com.android.support:loader androidx.loader:loader
    com.android.support:localbroadcastmanager androidx.localbroadcastmanager:localbroadcastmanager
    com.android.support:media2 androidx.media2:media2
    com.android.support:media2-exoplayer androidx.media2:media2-exoplayer
    com.android.support:mediarouter-v7 androidx.mediarouter:mediarouter
    com.android.support:multidex androidx.multidex:multidex
    com.android.support:multidex-instrumentation androidx.multidex:multidex-instrumentation
    com.android.support:palette-v7 androidx.palette:palette
    com.android.support:percent androidx.percentlayout:percentlayout
    com.android.support:preference-leanback-v17 androidx.leanback:leanback-preference
    com.android.support:preference-v14 androidx.legacy:legacy-preference-v14
    com.android.support:preference-v7 androidx.preference:preference
    com.android.support:print androidx.print:print
    com.android.support:recommendation androidx.recommendation:recommendation
    com.android.support:recyclerview-selection androidx.recyclerview:recyclerview-selection
    com.android.support:recyclerview-v7 androidx.recyclerview:recyclerview
    com.android.support:slices-builders androidx.slice:slice-builders
    com.android.support:slices-core androidx.slice:slice-core
    com.android.support:slices-view androidx.slice:slice-view
    com.android.support:slidingpanelayout androidx.slidingpanelayout:slidingpanelayout
    com.android.support:support-annotations androidx.annotation:annotation
    com.android.support:support-compat androidx.core:core
    com.android.support:support-content androidx.contentpager:contentpager
    com.android.support:support-core-ui androidx.legacy:legacy-support-core-ui
    com.android.support:support-core-utils androidx.legacy:legacy-support-core-utils
    com.android.support:support-dynamic-animation androidx.dynamicanimation:dynamicanimation
    com.android.support:support-emoji androidx.emoji:emoji
    com.android.support:support-emoji-appcompat androidx.emoji:emoji-appcompat
    com.android.support:support-emoji-bundled androidx.emoji:emoji-bundled
    com.android.support:support-fragment androidx.fragment:fragment
    com.android.support:support-media-compat androidx.media:media
    com.android.support:support-tv-provider androidx.tvprovider:tvprovider
    com.android.support:support-v13 androidx.legacy:legacy-support-v13
    com.android.support:support-v4 androidx.legacy:legacy-support-v4
    com.android.support:support-vector-drawable androidx.vectordrawable:vectordrawable
    com.android.support:swiperefreshlayout androidx.swiperefreshlayout:swiperefreshlayout
    com.android.support:textclassifier androidx.textclassifier:textclassifier
    com.android.support:transition androidx.transition:transition
    com.android.support:versionedparcelable androidx.versionedparcelable:versionedparcelable
    com.android.support:viewpager androidx.viewpager:viewpager
    com.android.support:wear androidx.wear:wear
    com.android.support:webkit androidx.webkit:webkit

    4.2包名替换

    快捷键 Ctrl + Shift + R 进行全局搜索替换

    Old build artifact AndroidX build artifact
    android.support.annotation.NonNull androidx.annotation.NonNull
    android.support.annotation.Nullable androidx.annotation.Nullable
    android.support.constraint.ConstraintLayout androidx.constraintlayout.widget.ConstraintLayout
    android.support.v4.widget.NestedScrollView androidx.core.widget.NestedScrollView
    android.support.v7.widget.RecyclerView androidx.recyclerview.widget.RecyclerView
    android.support.v7.widget.LinearLayoutManager androidx.recyclerview.widget.LinearLayoutManager
    android.support.v7.widget.DefaultItemAnimator androidx.recyclerview.widget.DefaultItemAnimator
    android.support.v7.widget.GridLayoutManager androidx.recyclerview.widget.GridLayoutManager
    android.support.constraint.Guideline androidx.constraintlayout.widget.Guideline
    android.support.v7.widget.CardView androidx.cardview.widget.CardView
    android.support.v4.view.ViewPager androidx.viewpager.widget.ViewPager
    android.support.v4.view.PagerAdapter androidx.viewpager.widget.PagerAdapter
    android.support.v4.app.FragmentManager androidx.fragment.app.FragmentManager
    android.support.v4.app.FragmentTransaction androidx.fragment.app.FragmentTransaction
    android.support.v7.app.AppCompatDialog androidx.appcompat.app.AppCompatDialog
    android.support.design.widget.CoordinatorLayout androidx.coordinatorlayout.widget.CoordinatorLayout
    android.support.design.widget.AppBarLayout com.google.android.material.appbar.AppBarLayout
    android.support.design.widget.CollapsingToolbarLayout com.google.android.material.appbar.CollapsingToolbarLayout
    android.support.v4.widget.Space Space

    本次替换用到了这些,如果有新的 再往里面添加吧


    到这一步基本上就完成迁移了

    5,确认问题环节

    1. 确保 clean rebuild run 都没问题
    2. 使用迁移后的代码打release包,在真机上运行无误

    下班吧 迁移完成了 GO! GO! GO!

    相关文章

      网友评论

        本文标题:迁移AndroidX!!!

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