Android AndroidX的遷移

1. AndroidX變化

1)常用依賴庫對比:

Old? build artifactAndroidX? build artifact

com.android.support:appcompat-v7:28.0.2 ======>androidx.appcompat:appcompat:1.0.0

com.android.support:design:28.0.2======>com.google.android.material:material:1.0.0

com.android.support:support-v4:28.0.2======>androidx.legacy:legacy-support-v4:1.0.0

com.android.support:recyclerview-v7:28.0.2======>androidx.recyclerview:recyclerview:1.0.0

com.android.support.constraint:constraint-layout:1.1.2======>androidx.constraintlayout:constraintlayout:1.1.2

更多詳細(xì)變化內(nèi)容,可以下載CSV格式映射文件;

2)常用支持庫類對比:

Support Library classAndroidX class

android.support.v4.app.Fragment======>androidx.fragment.app.Fragment

android.support.v4.app.FragmentActivity======>androidx.fragment.app.FragmentActivity

android.support.v7.app.AppCompatActivity======>androidx.appcompat.app.AppCompatActivity

android.support.v7.app.ActionBar======>androidx.appcompat.app.ActionBar

android.support.v7.widget.RecyclerView======>androidx.recyclerview.widget.RecyclerView

更多詳細(xì)變化內(nèi)容,可以下載CSV格式映射文件。

2. AndroidX配置

1)更新升級插件

將AS更新至AS 3.2及以上;

Gradle 插件版本改為4.6及以上;

項(xiàng)目下gradle/wrapper/gradle-wrapper.propertie文件中的distributionUrl改為:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

compileSdkVersion 版本升級到28及以上;

buildToolsVersion 版本改為28.0.2及以上。

插件更新提示

2)開啟遷移AndroidX

在項(xiàng)目的gradle.properties文件里添加如下配置:

android.useAndroidX=trueandroid.enableJetifier=true

?表示項(xiàng)目啟用 AndroidX 并遷移到 AndroidX。

3)替換依賴庫

修改項(xiàng)目app目錄下的build.gradle依賴庫:

implementation 'com.android.support:appcompat-v7:28.0.2' → implementation 'androidx.appcompat:appcompat:1.0.0'implementation 'com.android.support:design:28.0.2'? → implementation 'com.google.android.material:material:1.0.0'implementation 'com.android.support.constraint:constraint-layout:1.1.2' → implementation 'androidx.constraintlayout:constraintlayout:1.1.2'...

4)修改支持庫類

將原來import的android.**包刪除,重新import新的androidx.**包;

importandroid.support.v7.app.AppCompatActivity;→importandroidx.appcompat.app.AppCompatActivity;

5)一鍵遷移AndroidX庫

AS 3.2 及以上版本提供了更加方便快捷的方法一鍵遷移到 AndroidX。選擇菜單上的ReFactor —— Migrate to AndroidX...即可。(如果遷移失敗,就需要重復(fù)上面1,2,3,4步手動去修改遷移)

AndroidX 遷移

注意:如果你的項(xiàng)目compileSdkVersion 低于28,點(diǎn)擊Refactor to AndroidX...會提示:

You need to have at least have compileSdk28set in your module build.gradle to refactor to androidx

提示讓你使用不低于28的sdk,升級最新到SDK,然后點(diǎn)擊Migrate to AndroidX...,AS就會自動將項(xiàng)目重構(gòu)并使用AndroidX庫。

3. AndroidX遷移問題

《Android Support庫和AndroidX沖突問題》

4. AndroidX影響

??雖然說目前對我們沒有多大影響,我們可以不使用仍然使用舊版本的支持庫,畢竟沒有強(qiáng)制,但長遠(yuǎn)來看還是有好處的。AndroidX重新設(shè)計(jì)了包結(jié)構(gòu),旨在鼓勵庫的小型化,支持庫和架構(gòu)組件包的名字也都簡化了;而且也是減輕Android生態(tài)系統(tǒng)碎片化的有效方式。

作者:翻譯不了的聲響

鏈接:http://m.itdecent.cn/p/7dc111353328

來源:簡書

著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

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