Markdown版本筆記 | 個人GitHub首頁 | 個人博客 | 個人微信 | 個人郵箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
官方文檔
全部 AndroidX 引用庫
API文檔
androidx-class-mapping.csv
androidx-artifact-mapping.csvhtml
Google 2018 IO 大會推出了擴展庫 AndroidX,AndroidX 是對 android.support.xxx
包的整理後產物。因爲以前的support包過於混亂,因此,google推出了 androidX 用於替換support包。java
其只是將原來的android.*
替換成androidx.*
,只有包名和Maven工件名受到影響,原來的類名、方法名和字段名不會更改。android
因爲在後續版本中,會逐步放棄對 support 的升級和維護,因此,咱們必須儘快遷移到 androidX。git
Jetpack components are part of the AndroidX library. Learn more about components on the Jetpack home page.github
詳細變化內容,能夠下載CSV格式映射文件:androidx-class-mapping.csv微信
Old build artifact | AndroidX 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 |
詳細變化內容,能夠下載CSV格式映射文件:androidx-artifact-mapping.csvapp
Support Library class | AndroidX 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 |
AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.ide
AndroidX是Android團隊用於在Jetpack中開發,測試,打包,發佈和發佈庫的開源項目。佈局
AndroidX is a major improvement to the original Android Support Library. Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility
across Android releases. AndroidX fully replaces the Support Library by providing feature parity and new libraries. In addition AndroidX includes the following features:測試
AndroidX是對原始Android支持庫的重大改進。與支持庫同樣,AndroidX與Android操做系統分開提供,並提供跨Android版本的向後兼容性。 AndroidX經過提供功能奇偶校驗和新庫徹底取代了支持庫。此外,AndroidX還包括如下功能:
All packages in AndroidX live in a consistent namespace starting with the string androidx
. The Support Library packages have been mapped into corresponding androidx.*
packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page.
AndroidX中的全部軟件包都以字符串androidx開頭,位於一致的命名空間中。支持庫包已映射到相應的androidx。*包。有關全部舊類和構建工件的完整映射到新構件,請參閱「包重構」頁面。
Unlike the Support Library, AndroidX packages are separately maintained and updated. The androidx
packages use strict Semantic Versioning starting with version 1.0.0. You can update AndroidX libraries in your project independently.
與支持庫不一樣,AndroidX軟件包是單獨維護和更新的。 androidx軟件包使用嚴格的語義版本控制,從版本1.0.0開始。您能夠單獨更新項目中的AndroidX庫。
All new Support Library development will occur in the AndroidX library. This includes maintenance of the original Support Library artifacts and introduction of new Jetpack components.
全部新的支持庫開發都將在AndroidX庫中進行。這包括維護原始支持庫工件和引入新的Jetpack組件。
If you want to use AndroidX in a new project, you need to set the compile SDK to Android 9.0 (API level 28) or higher and set both of the following Android Gradle plugin flags to true
in your gradle.properties
file.
android.useAndroidX
: When set to true
, the Android plugin uses the appropriate AndroidX library instead of a Support Library. The flag is false
by default if it is not specified.android.enableJetifier
: When set to true
, the Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries. The flag is false
by default if it is not specified.一、修改 gradle.properties
android.useAndroidX=true android.enableJetifier=true
其中:
android.useAndroidX=true
表示當前項目啓用 androidxandroid.enableJetifier=true
表示將依賴包
也遷移到androidx。若是取值爲false,表示不遷移依賴包到androidx,但在使用依賴包中的內容時可能會出現問題,固然了,若是你的項目中沒有使用任何三方依賴,那麼,此項能夠設置爲false二、將AS更新至 AS 3.2 及以上
將 Gradle 插件版本改成 4.6 及以上
將 compileSdkVersion、buildToolsVersion 版本改成 28.0.2及以上
三、在AndroidStudio 3.2 或更高版本中執行以下操做:
Refactor > Migrate to AndroidX
若是你的項目 compileSdkVersion 低於28,點擊Migrate to AndroidX...會提示:
You need to have at least have compileSdk 28 set in your module
build.gradle
to refactor to androidx
在執行該操做時會提醒咱們是否將當前項目打包備份。若是你提早已經作好了備份,能夠忽略;若是沒有備份,則先備份。
參考文章
Android Support 庫和 AndroidX 衝突問題
因爲 Migrate to AndroidX
執行以後,部分控件的包名/路徑名轉換的有問題,因此還須要咱們手動調整(包括修改xml佈局文件和.java/.kt文件)。
如:ViewPager、RecyclerView 等,這些內容在遷移完成以後,包名是 androidx.core.weight.xxxx
,這是一個錯誤的包名,咱們必須手動修改,不然,沒法正常編譯。點擊綠色 Run(運行) 按鈕時會詳細報出哪裏有錯誤。
注意,在 AndroidStudio 的 build 選項卡中一次最多隻會報 50條錯誤!因此,可能在你修完第一批以後,後面還有N個50。此處要保持一個平靜的 💗。
在 AndroidStudio3.2 + androidx 環境下,對錯誤的檢查和處理更爲嚴格。若是同一個xml佈局文件中存在同名id,在以前的版本中,咱們能夠正常編譯和運行,可是,在新的環境下, 必然會報錯,錯誤信息以下:
在上圖的錯誤信息中,咱們以 DecibelBinding 爲例,簡述修復過程。
如上圖,沒法將xxxBinding 構造器中的xxxBinding應用到指定類型
指明瞭出錯的 Binding類 爲 DecibelBinding
按照DataBinding類名的生成規則,咱們能夠知道,DecibelBinding 對應的xml文件名應該是 decibel.xml (若是你在xml中經過 class="xxxBinding" 指定了DataBinding的生成類名,那麼就全局搜索吧)
在肯定了xml以後,咱們還須要知道到底哪裏出了錯誤,那麼,就繼續看圖中的 錯誤:找不到符號 符號:變量 xxx
.這個變量就是控件的id名稱。
DataBinding轉換控件id名的規則是:去除下劃線鏈接符,而後將原下劃線後面的第一個字母大寫。因此,圖中的 fragmentDiscoverGridItemRelativeLayout1 對應的控件id應該是:@+id/fragment_discover_grid_item_relative_layout
,後面之因此有一個1 ,是由於重複了。而後,咱們在對應的xml文件中搜索這個控件名,而後刪除重複便可。
在遷移到 androidX 以前,咱們爲自定義控件編寫自定義屬性時,能夠與android已有的屬性重名,可是,在AndroidX環境下不行了,若是存在重名的狀況, 必然會報錯——會提示你重複定義。
錯誤示例
<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>
關於重名屬性,在 androidX 中不知道哪一個控件中包含了一個 mode 屬性,因此,若是以前你的自定義控件中有 attr/mode,須要手動改爲其餘。
遷移到 androidX 以後,Glide中使用的 android.support.annotation.CheckResult
和 android.support.annotation.NonNull
這兩個註解沒法遷移。以前有用戶在Glide中提過issue: issues/3185
在上述issue中有用戶表示,將Glide升級到 4.8.0 以後,能夠正常遷移。可是,我這邊並不行。而後,我先升級了Glide ,又在 gralde文件中增長了support.annotation ,這樣才能正常編譯經過。貌似在後續Glide 5.x 版本中會完成對 androidx的徹底兼容。
個人臨時解決方案:
//圖片加載——Glide implementation "com.github.bumptech.glide:glide:4.8.0 annotationProcessor "com.github.bumptech.glide:compiler:4.8.0 //這兩行是爲了解決 https://github.com/bumptech/glide/issues/3185 ——Glide 中的註解尚未徹底兼容androidx implementation "com.android.support:support-annotations:28.0.0-alpha3" annotationProcessor "com.android.support:support-annotations:28.0.0-alpha3"
這裏所說的包名,指的是項目中的文件夾名稱。在以前版本中,咱們命名包名時可能會出現大寫字母,雖然這並不符合Java命名規範,但起碼能正常編譯和運行。然而,升級到 AndroidStudio3.2 + androidX 環境後,必須嚴格遵照命名規範,不然,可能報錯,從而致使不能正常編譯和運行。
對於包名的命名規範,好像要求並不是十分嚴格。由於我發現,部分包含大寫字母的報名在編譯時會報錯,部分不報錯。可是,遵照規範總會更好一點!
雖然咱們從gradle中配置了遷移三方庫的參數,可是,因爲三方庫的版本更新問題,也可能會遷移失敗。在三方庫遷移失敗時,若是使用了數據綁定,一般會報以下錯誤:
碰到上述錯誤以後,咱們能夠按下列步驟處理:
遷移過程當中若是爆出一些 android 包自己或者其餘莫名其妙的問題時,先去 xml 佈局文件 或 .java
文件中找一下,是否有繼續引用 xxx.support.xxx
的狀況,若是有,記得替換成 androidx.xxx.xxx
包下的對應控件。( xxx 泛指任意內容)
2019-4-15