Android:你好,androidX.再見,android.support
1、AndroidX簡介
ofollow,noindex">點選檢視Android文件中對androidx的簡介
按照官方文件說明 androidx 是對 android.support.xxx 包的整理後產物。由於之前的support包過於混亂,所以,google推出了 androidX。
由於在後續版本中,會逐步放棄對support 的升級和維護,所以,我們必須遷移到 androidX.對此,官方描述如下:
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.
2、遷移步驟
2.1 修改gradle.properties
android.useAndroidX=true android.enableJetifier=true
其中:
android.useAndroidX=true android.enableJetifier=true
2.2 如何遷移
在AndroidStudio 3.2 或更高版本(目前最新正式版為3.2,其他更高版為alpha版)中執行如下操作:
-
Refactor > Migrate to AndroidX
image
在執行該操作時會提醒我們是否將當前專案打包備份。如果你提前已經做好了備份,可以忽略;如果沒有備份,則先備份。
3 遷移後續
3.1 手動修改錯誤包名
由於 Migrate to AndroidX
執行之後,部分控制元件的包名/路徑名轉換的有問題,所以還需要我們手動調整(包括修改xml佈局檔案和.java/.kt 檔案)。
如:ViewPager,RecyclerView 等,這些內容在遷移完成之後,包名是 androidx.core.weight.xxxx
,這是一個錯誤的包名,我們必須手動修改,否則,無法正常編譯——點選綠色 Run(執行) 按鈕時會詳細報出哪裡有錯誤。
此處需要注意,在 AndroidStudio 的 build 選項卡中一次最多隻會報 50條錯誤!!所以,可能在你修完第一批之後,後面還有N個50。此處要保持一個平靜的:heartpulse:。
3.2 修復DataBinding中的錯誤
在 AndroidStudio3.2 + androidx 環境下,對錯誤的檢查和處理更為嚴格。如果同一個xml佈局檔案中存在同名id,在之前的版本中,我們可以正常編譯和執行,但是,在新的環境下, 必然會報錯,錯誤資訊如下:

image
在上圖的錯誤資訊中,我們以 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檔案中搜索這個控制元件名,然後刪除重複即可。
3.3 去除 attr.xml 中重複的屬性名稱
在遷移到 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>
3.4 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的完全相容。
我的臨時解決方案:
//圖片載入——Glide implementation "com.github.bumptech.glide:glide:4.8.0 annotationProcessor "com.github.bumptech.glide:compiler:4.8.0 //CnPeng 2018/9/26 下午8:38 這兩行是為了解決 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"