還在用 android.support?谷歌強推 AndroidX 啦!

2021-02-13 碼個蛋

原文: https://juejin.im/post/5d2b2ee3f265da1b827acb02

AndroidX 是 Google 2018 IO 大會推出的新擴展庫,主要是對 Android支持庫做了重大改進。與支持庫一樣,AndroidX 與 Android 作業系統分開提供,並與各個 Android 版本向後兼容,可以說 AndroidX 就是為了替換 Android 支持庫而設計的。


AndroidX 是 Android 團隊用於在Jetpack中開發、測試、打包和發布庫以及對其進行版本控制的開源項目。[摘自官方]

AndroidX 完全取代了支持庫,不僅提供同等的功能,而且提供了新的庫。

AndroidX 會將原始支持庫 API 軟體包映射到 androidx 命名空間。只有軟體包和 Maven 工件名稱發生了變化;類、方法和欄位名稱沒有改變。

與支持庫不同,AndroidX 軟體包會單獨維護和更新。androidx 軟體包使用嚴格的語義版本控制,從版本 1.0.0 開始,可以單獨更新項目中的 AndroidX 庫。

所有新支持庫的開發工作都將在 AndroidX 庫中進行,這包括維護原始支持庫工件和引入新的 Jetpack 組件。


AndroidX 的變化

1. 常見依賴庫映射

舊編譯工件AndroidX 編譯工件com.android.support.constraint:constraint-layoutandroidx.constraintlayout:constraintlayout:1.1.2com.android.support:appcompat-v7androidx.appcompat:appcompat:1.0.0com.android.support:cardview-v7androidx.cardview:cardview:1.0.0com.android.support:coordinatorlayoutandroidx.coordinatorlayout:coordinatorlayout:1.0.0com.android.support:designcom.google.android.material:material:1.0.0-rc01com.android.support:drawerlayoutandroidx.drawerlayout:drawerlayout:1.0.0com.android.support:gridlayout-v7androidx.gridlayout:gridlayout:1.0.0com.android.support:media2androidx.media2:media2:1.0.0-alpha03com.android.support:multidexandroidx.multidex:multidex:2.0.0com.android.support:percentandroidx.percentlayout:percentlayout:1.0.0com.android.support:recyclerview-v7androidx.recyclerview:recyclerview:1.0.0com.android.support:support-annotationsandroidx.annotation:annotation:1.0.0com.android.support:support-compatandroidx.core:core:1.0.0com.android.support:support-fragmentandroidx.fragment:fragment:1.0.0com.android.support:support-v4androidx.legacy:legacy-support-v4:1.0.0com.android.support:viewpagerandroidx.viewpager:viewpager:1.0.0com.android.support:swiperefreshlayoutandroidx.swiperefreshlayout:swiperefreshlayout:1.0.0

更多詳細依賴庫變化,可查閱官方文檔(https://developer.android.com/jetpack/androidx/migrate#artifact_mappings)或下載這些映射的 CSV 格式(https://developer.android.com/topic/libraries/support-library/downloads/androidx-artifact-mapping.csv)文件。

2.常見類映射

支持庫類AndroidX 類android.arch.lifecycle.Lifecycleandroidx.lifecycle.Lifecycleandroid.support.v4.app.Fragmentandroidx.fragment.app.Fragmentandroid.support.v4.app.FragmentActivityandroidx.fragment.app.FragmentActivityandroid.support.v7.app.AppCompatActivityandroidx.appcompat.app.AppCompatActivityandroid.support.v7.app.ActionBarandroidx.appcompat.app.ActionBarandroid.support.v7.widget.RecyclerViewandroidx.recyclerview.widget.RecyclerViewandroid.support.design.card.MaterialCardViewcom.google.android.material.card.MaterialCardViewandroid.support.design.ripple.RippleUtilscom.google.android.material.ripple.RippleUtilsandroid.support.design.widget.CoordinatorLayoutandroidx.coordinatorlayout.widget.CoordinatorLayoutandroid.support.design.widget.NavigationViewcom.google.android.material.navigation.NavigationViewandroid.support.percent.PercentFrameLayoutandroidx.percentlayout.widget.PercentFrameLayout

更多詳細支持類映射變化,可查閱官方文檔(https://developer.android.com/jetpack/androidx/migrate#artifact_mappings)或下載這些映射的CSV 格式(https://developer.android.com/topic/libraries/support-library/downloads/androidx-class-mapping.csv)文件。



下面是 Google 官方描述

Existing packages, such as the Android Support Library, are being refactored into AndroidX.
Although Support Library versions 27 and lower are still available on Google Maven,
all new development will be included in only AndroidX versions 1.0.0 and higher.


1. 更新 Android Studio 與 Gradle 版本

將 Android studio 升級到 3.2 及以上;

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

compileSdkVersion 版本升級到 28 及以上;

buildToolsVersion 版本改為 28.0.2 及以上。

2. 遷移 AndroidX 配置

android.useAndroidX=true
android.enableJetifier=true

配置說明android.useAndroidX=true表示當前項目啟用 androidxandroid.enableJetifier=true表示將依賴包也遷移到androidx

備註:enableJetifier 如果取值為 false, 表示不遷移依賴包到 androidx,但在使用依賴包中的內容時可能會出現問題,當然了,如果你的項目中沒有使用任何三方依賴,那麼,此項可以設置為 false。

3. 修改依賴庫
修改項目 app 目錄下的 build.gradle 依賴庫,具體可以參照 AndroidX 變化中的依賴庫映射。

修改前修改後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.** 包

import android.support.v7.app.AppCompatActivity; → import androidx.appcompat.app.AppCompatActivity;

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


備註:如果你的項目 compileSdkVersion 低於 28,點擊 Refactor to AndroidX... 會提示:



不可以共存。需要將依賴修改為Android Suppor或AndroidX中任一種。

不一定。部分控制項的包名/路徑名轉換的有問題,所以還需要我們手動調整(包括修改xml布局文件和.java/.kt 文件)。

在 AndroidStudio3.2 + androidx 環境下,對錯誤的檢查和處理更為嚴格。如果同一個xml布局文件中存在同名id,
在之前的版本中,我們可以正常編譯和運行,但是,在新的環境下, 必然會報錯,錯誤信息如下:

在遷移到 androidX 之前,我們為自定義控制項編寫自定義屬性時,可以與android已有的屬性重名,
但是,在AndroidX環境下不行了,如果存在重名的情況,必然會報錯——會提示你重複定義(詳細錯
誤信息沒截圖,但翻譯過來就是重複定義了attr/xxx)。

<declare-styleable name="RoundImageView">
    ...
    <!-在遷移到androidx之前,這樣寫雖然不規範,但是能用,不報錯->
    <attr name="textSize" format="Integer" />
    ...
</declare-styleable>

<declare-styleable name="RoundImageView">
    ...
    <!-遷移到androidX之後,必須使用android:xxx 屬性,不能定義android已有的屬性->
    <attr name="android:textSize" />
    ...
</declare-styleable>

Glide 中的註解不兼容 androidX?
遷移到 androidX 之後,Glide 中使用的 android.support.annotation.CheckResult 和 android.support.annotation.NonNull 這兩個註解無法遷移。之前有用戶在 Glide 中提過 issue: https://github.com/bumptech/glide/issues/3185
在上述 issue 中有用戶表示,將 Glide 升級到 4.8.0 之後,可以正常遷移。但是,我這邊並不行。然後,我先升級了 Glide , 又在 gralde 文件中增加了 support.annotation ,這樣才能正常編譯通過。貌似在後續 Glide 5.x 版本中會完成對 androidx 的完全兼容。

規範包名(即文件夾名)?
這裡所說的包名,指的是項目中的文件夾名稱。在之前版本中,我們命名包名時可能會出現大寫字母,雖然這並不符合 Java 命名規範,但起碼能正常編譯和運行。然而,升級到 AndroidStudio3.2 + androidX 環境後,必須嚴格遵守命名規範,否則,可能報錯,從而導致不能正常編譯和運行。

近期文章:

日問題:

大家用Android X沒?

專屬升級社區:《這件事情,我終於想明白了》 

相關焦點

  • Android Support Library 23.2 發布啦!
    上突然流出了一段視頻,關於Android Support Library 23.2的一些令人期待的新feature,一經發出即在圈內引爆,從國外發酵到國內,微博微信上大家也在熱烈討論此事,視頻隨後被設置為private狀態無法閱讀,發布者表示這次是意外洩漏,正式發布應該在本周內,大家可以圍觀這個原討論:https://www.reddit.com/r/androiddev/comments/4790vn/android_support_library
  • 【Android】Android Support Library與androidx的映射.含CSV格式映射下載連結
    :room-testingandroid.arch.persistence:dbandroidx.sqlite:sqliteandroid.arch.persistence:db-frameworkandroidx.sqlite:sqlite-frameworkcom.android.support.constraint:constraint-layoutandroidx.constraintlayout
  • Android Support Library主要庫詳細介紹
    不可能去更新行動裝置中的android.jar吧,因為硬體設備集成的sdk版本是固定的,android.jar也是固定的,所以最好的方式是將新增的API以依賴包的形式集成到需要使用高版本API的應用程式中。  谷歌早已經考慮到了向後兼容的問題,所以推出了一系列脫離於android.jar的依賴包,比如常見的android-support-v4、appcompat-v7等。
  • Android Support Library的前世今生
    App編譯時用的 android sdk(android.jar)不會打包進我們的App中。因為App編碼是使用 android.jar 中的接口就是 android設備裡系統框架層(framework)對外提供的接口。2.
  • 幾行代碼,讓你的 APP 變得花俏—Android Design Support Library 代碼實驗
    來了一個好消息,上周(2015.5.29)在谷歌2015 I/O 大會時,谷歌宣布了一個今年最讓人興奮的支持庫,名叫 Android Design Support Library,在這個單獨的 library 裡提供了一堆有用的材料設計 UI 組件。通過這篇文章,讓我用這個機會向你一個一個描述如何來使用他們。
  • Android Design Support Library使用
    作為Android開發者,谷歌的Material design規範文檔有讀過嗎?
  • Android Design Support Library之TabLayout
    app/build.gradle 中添加下面依賴: compile 'com.android.support:design:25.3.1'<android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width
  • Android Design Support Library 之 NavigationView
    Gradle 中添加下面依賴compile 'com.android.support:design:23.1.0'之前Google也提出了使用DrawerLayout來實現導航抽屜。這次,在support library中,Google提供了NavigationView來實現導航菜單界面。
  • Android 最新 Support V4 包大拆分有用嗎?
    (4) support-media-compatandroid.media 兼容庫,包括 MediaBrowser 和 MediaSession。大小為 248k。(5) support-fragmentfragment 的兼容庫,大小為 136k。2. 子 Module 間依賴關係
  • 探索 Android Design Support Library v28 新增內容
    我們可以像這樣將這個按鈕添加進布局文件中:<android.support.design.button.MaterialButton   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="MATERIAL
  • Android Design Support Library v28 新增組件詳解
    implementation 'com.android.support:design:28.0.0-alpha3'}對應的布局文件:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://
  • 你可能不知道的一個安卓表情庫—Android 官方兼容庫 EmojiCompat Support Library
    谷歌官方推薦,使用方面,內存佔用小。該篇是香辣牛肉麵的投稿,我們都稱他為面哥,一個經驗豐富的老司機。同時,從日本漢字中他獲得了一種能力,用簡單的字符來表達「秘密」和「愛」等抽象概念。compile "com.android.support:support-emoji:26.0.0-beta1"}該組件庫對應的 emojiCompat 組件:<android.support.text.emoji.widget.EmojiTextView   android:layout_width="wrap_content"   android
  • 還在用Android正經布局來寫頁面嗎?
    介紹ConstraintLayout又稱約束布局,是谷歌在2016年開發者大會上推出的,之後在Android Studio上成為了默認布局,該布局能減少布局的層級嵌套,我們都知道,View嵌套的越多,在加載的過程中解析起來就越費時間,該布局幾乎能做到LinearLayout和RelativeLayout嵌套完成的任何布局,下面跟著一波小demo來深入了解谷歌推薦的ConstraintLayout
  • Android Support Library 之 夜間模式
    (1)app/build.gradlecompile 'com.android.support:appcompat-v7:25.3.1'(2)Activity須繼承AppCompatActivity(3)Theme.AppCompat.DayNight <style name="AppTheme
  • 還在用Android自帶的WebView組件?太Out了!
    <com.tencent.smtt.sdk.WebView              android:id="@+id/tbsContent"              android:layout_width="match_parent"              android:layout_height="match_parent
  • Android 遷移到 Androidx
    一個專注 Android 領域的技術平臺公眾號回復 Android 加入我的安卓技術群作者:WangJie0822連結:https://www.jianshu.com/p/773e231daed4聲明:本文已獲WangJie0822授權發表,轉發等請聯繫原作者授權前言 最近 Google 發布了 Android support
  • Android install apk 兼容至 Android 8
    前言以往安裝apk都是很簡單的方法, Intent 裡添加apk的文件就可以調用系統安裝界面.後來隨著谷歌對安全的重視,從Android 7開始以往的方式都不能用,然而到Android 8 又有改動,相信隨著Android 的發展,以後的版本也會有改動,崇尚模塊化開發的我便希望每一個細小的功能,顆粒度最少的功能都可以由一個模塊來負責,然後每個項目需要這個功能時調用這個模塊即可,而這個模塊只需要維護好自己的兼容性問題便解決所有項目的兼容性問題
  • 寫給Android開發的Gradle知識體系
    前言老讀者都知道,我的技術博客從2016年開始就沒寫過不成系列的文章,這些系列文章組成了目前Android領域最全面深入的原創知識體系,更恐怖的是這個體系還在不斷的成長,關於這個知識體系可以點擊 閱讀原文 了解。
  • Android實現炫酷的視覺差效果
    ><android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com
  • Android實現快遞時間軸功能
    4.具體實現dependencies { api 'com.android.support:recyclerview-v7:28.0.0'}<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/