遷移AndroidX!!!

參考鏈接
本次遷移主要參考上面谷歌官方公眾號文章,遷移背景建議點(diǎn)進(jìn)文章進(jìn)行查看,本文主要提及一些遷移過程和其中遇到的坑

重要提醒

遷移開始之前,不管你的項(xiàng)目用的是svn還是git 都確認(rèn)本地代碼無修改并且是分支上最新代碼,建議在電腦上新建個文件夾,把分支上代碼拉下來專門做androidX遷移,這樣到時候既是遷移不成功,把該文件夾刪掉就行了,跟你的小伙伴們商量好遷移的事項(xiàng),像我這一個人的話,就隨便了

如果你的項(xiàng)目不是使用AndroidStudio開發(fā)的本文中的一些步驟可能不太適用,確保AndroidStudio使用3.5+版本

關(guān)于Gradle版本問題

如果你的項(xiàng)目使用的是 美團(tuán)的walle 打包,不要把gradle版本升級到最新的4.0,停留在3.63就行.問題一大堆了

如圖

gradle配置參考

參考示例
項(xiàng)目的gradle文件
dependencies {
        classpath "com.android.tools.build:gradle:3.6.3"
    }
項(xiàng)目的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,將項(xiàng)目中所有的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'

這一步應(yīng)該沒問題,無非就是涉及到一些Api的改動

2,開啟 Jetifier

項(xiàng)目的gradle.properties添加

android.useAndroidX=true
android.enableJetifier=true
修改好之后,確保 clean rebuild run 都沒問題 在進(jìn)行下一步

3,將項(xiàng)目依賴的所有第三方庫全部升級

個人認(rèn)為這一步最重要,當(dāng)然選擇第三方sdk要適用于你的項(xiàng)目,比如okhttp4,rxjava3等底層都換了其實(shí)用rx2最新的版本就行,glide等改動不太大的直接升級最新版本就行

如果檢測到第三方SDK還沒有適配AndroidX,并且是項(xiàng)目必須依賴的,那么就此放棄,以下內(nèi)容不用再看了!!!




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

因?yàn)轫?xiàng)目中使用了 支付寶sdk,微信支付登錄sdk,大廠自然不用說,不存在一些java版本的兼容問題

但是 我們還是用了匯付寶

之前項(xiàng)目中一直使用的是匯付寶3.0的版本,最新的版本是4.0,這次升級sdk時遺漏了匯付寶的sdk升級 導(dǎo)致最終打包時一直報錯,說某個方法不存在之類的,吭哧了兩天 ,從頭開始弄才得以解決

3.2升級Butterknife

butterknife從10.0開始已經(jīng)支持androidx了,我們直接升級到最新的10.2.1版本即可,需要在app下的gradle中配置編譯jdk版本

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

JDK8下載: 官網(wǎng)下載地址,不知道從幾何時下載jdk也需要登錄賬號了,注冊賬號的流程又TM那么復(fù)雜,好在前人已經(jīng)填坑了,直接給出的自己注冊的賬號

1772885836@qq.com 
OracleTest1234

或者
2696671285@qq.com 
密碼:Oracle123

賬號引用鏈接,再次感謝

3.3升級BaseRecyclerViewAdapterHelper

最新的3.0.4支持androidx并且用kotlin重寫,但是一些api改動比較大,
像item中view的點(diǎn)擊事件,加載更多等,一想到項(xiàng)目中那么多列表 我滴個親娘嘞,沒辦法升級總是痛苦的,就是個復(fù)制粘貼的工作,干就得了奧利給!

修改好之后,確保 clean rebuild run 都沒問題 在進(jìn)行下一步

4,使用AndroidStudio 將Support庫轉(zhuǎn)換AndroidX

Migrate to AndroidX


AndroidStudio導(dǎo)航

過程中會提示你備份當(dāng)前工程,備份一下就進(jìn)行下一步,經(jīng)過一段時間后成功了,

需要注意的是,AndroidStudio轉(zhuǎn)換后有時候并沒有把所有的XML布局都能轉(zhuǎn)換成功,有一些布局文件會遺漏

這個時候就需要我們自己手動進(jìn)行替換

4.1庫替換

官方提供庫的映射表,可以用Ctrl+F 在本頁面查找.找到對應(yīng)的庫并且選擇合適的版本

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 進(jìn)行全局搜索替換

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,確認(rèn)問題環(huán)節(jié)

  1. 確保 clean rebuild run 都沒問題
  2. 使用遷移后的代碼打release包,在真機(jī)上運(yùn)行無誤

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容