cp : https://blog.csdn.net/qq_17766199/article/details/81433706
1.說明
We hope the division between android.* and androidx.* makes it more obvious which APIs are bundled with the platform, and which are static libraries for app developers that work across different versions of Android.
簡單地說就是新的庫可以在不同的Android版本上使用。比如之前我們如果使用support
為27.1.1的相關依賴庫時。可能需要所有相關的support
庫都為27.1.1。如果其中有bug的話,可能需要所有的都去升級,存在一個綁定關系,而且正式版的發布周期也很長。
通過AndroidX,我們可以看到實時實現的特性和bug修復。升級個別依賴,不需要對使用的所有其他庫進行更新。這就和我們使用Github上的開源庫一樣的,出了問題,我們可以提出bug和意見。作者修復后,發布新版本,我們就可以直接替換使用了。更加的透明便捷。
2.變化
我選取了幾個常用依賴庫,我們可以看看變化:
Old build artifact | AndroidX build artifact |
---|---|
com.android.support:support-compat | androidx.core:core:1.0.0+ |
com.android.support:appcompat-v7 | androidx.appcompat:appcompat:1.0.0+ |
com.android.support:design | com.google.android.material:material:1.0.0+ |
com.android.support:multidex | androidx.multidex:multidex:2.0.0+ |
com.android.support:recyclerview-v7 | androidx.legacy:legacy-support-v4:1.0.0+ |
com.android.support:viewpager | androidx.viewpager:viewpager:1.0.0+ |
com.android.support:support-fragment | androidx.fragment:fragment:1.0.0+ |
當然涉及的不止這些庫,更詳細的變化內容可以查看官方文檔。
我們可以添加appcompat
依賴對比一下:
implementation 'com.android.support:appcompat-v7:28.0.0-beta1' 或 implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
可以看到詳細變化
同時我們看到viewpager
、swiperefreshlayout
、 coordinatorlayout
等一些UI組件被分離了出來,這樣也是便於更好的使用,職責分明,以減輕不使用ProGuard
或 Multidex
的應用程序和測試的壓力。
3.影響
官方博客中有說道,為了給開發者一定遷移的時間,所以28.0.0的穩定版本還是采用android.support
。但是所有后續的功能版本都將采用androidx
。
其實對於我們影響也不是很大,我們可以選擇不使用,畢竟不是強制的。但長遠看來還是有好處的。接受變化,擁抱變化也是我們程序猿需要有的精神,歡迎嘗試。
對於有寫一些開源項目的人,可能會有一些影響。比如你有一個關於RecyclerView的拓展庫,那么你就需要去讓他支持AndroidX
,否則你懂的。
我有去看了一下我們常用的butterknife
、glide
等都已經適配了AndroidX
,不得不說真是很良心。
4.遷移
如果一個一個去替換當然很麻煩了,所以在Android Studio 3.2 Canary中添加了一鍵遷移的功能Refactor -> Migrate to AndroidX
。
首先你的gradle
版本至少為3.2.0以上,以及compileSdkVersion
為28以上。
classpath 'com.android.tools.build:gradle:3.2.0+'
如果你是一個新項目,如果使用AndroidX相關依賴,需要在gradle.properties
文件里添加配置:
android.useAndroidX=true android.enableJetifier=true
如果你想使用AndroidX,但是之前的不遷移,可以這樣配置:
android.useAndroidX=true android.enableJetifier=false
5.參考
當然遷移最好是出了正式版后在嘗試。這里目的也就和標題一樣,了解一下(堅決不做標題黨哈)。