【譯】使用AndroidX代替Android支持庫

原文 Hello World,AndroidX

今天,我們發(fā)布一個(gè)新的Android擴(kuò)展庫(AndroidX)的早期預(yù)覽版,這表示了支持庫的一個(gè)新的時(shí)代。請為這些變化提出你們的反饋。既然這是一個(gè)早期的預(yù)覽版本,我們并不建議放在生產(chǎn)環(huán)境嘗試這些功能,因?yàn)樗€存在一些已知的問題。

7年以來,支持庫框架提供了向后的兼容。多年以來,支持庫已經(jīng)包含了設(shè)備特定的用戶體驗(yàn),調(diào)試,測試和其他工具類。支持庫的普及是非常驚人的;今天大多數(shù)app都采用了支持庫。我們想要加大在這一領(lǐng)域的投入,打下一個(gè)堅(jiān)實(shí)的基礎(chǔ)是具有決定性意義的。

我們很想和你們談一談。我們收到了一致的意見:支持庫的已經(jīng)變得令人困惑和混亂。我們“v7”的包和組件,最低支持的sdk級(jí)別是14。我們想要理清楚不同平臺(tái)相關(guān)聯(lián)的api之間的界限。

出于上述的考慮,say“Hello World”to“AndroidX”。如先前在Android KTX announcement提到的,我們在這個(gè)包里面添加了一些新的功能,并且更新了一些已有的功能。

android. VS androidx.* namespaces

編寫android apps 需要依賴兩種類:

* 一種類似于PackageManager,這個(gè)是和操作系統(tǒng)相關(guān)聯(lián)的,可以提供不同的api,為不同的android版本提供不同的表現(xiàn)。

* 一種類似于AppCompatActivity或者ViewModel,這個(gè)是和操作系統(tǒng)沒有關(guān)聯(lián)的,打包進(jìn)你的apk,這些庫提供了單一的api,盡可能地為不同版本的android提供一致的表現(xiàn)。

很多時(shí)候,非捆綁式的庫是更好地選擇,因?yàn)檫@位不同的android版本提供相同的api。這個(gè)重構(gòu)把非捆綁式的庫-包含所有的支持庫和架構(gòu)組件轉(zhuǎn)移到AndroidX包里,讓所依賴的內(nèi)容更加清楚。

Revised naming for packages and Maven artifacts

我們重新設(shè)計(jì)了包架構(gòu),鼓勵(lì)更小,更聚焦的庫,減小在沒有使用Proguard和Multidex的測試的壓力。Maven groupIds和artifactIds已經(jīng)做了更新,更好的反映庫的內(nèi)容,我們?yōu)椴煌念?,groupId,maven artifact創(chuàng)建相同的包的前綴。

通常,你可以從老的包直接看出和新包映射關(guān)系。

old

android.support.**

android.databinding.**?

android.design.**

android.support.test.**

New

androidx.@

androidx.databinding.@

com.google.android.material.@

(in a future release)androidx.text.@?

架構(gòu)組件庫也轉(zhuǎn)移到了androidx包下面,他們的包名也簡化了,可以直接反映他們集成的核心庫。示例如下:

Old

android.arch.**

android.arch.persistence.rom.**

android.arch.persistence.**

New

androidx.@

androidx.room.@

androidx.sqlite.@

另外,28.0.0-alpha1的Material Components將替代Design Library

點(diǎn)擊AndroidX refactoring map查看完整的 28.0.0-alpha1(android.support)和1.0.0-alpha1(androidx)的對(duì)應(yīng)關(guān)系。請注意,在alpha階段,還只有較少的映射關(guān)系地修改。

Per-artifact strict semantic versioning

Starting with the AndroidX refactor, library versions have been reset from 28.0.0 to 1.0.0. Future updates will be versioned on a per-library basis, following strict semantic versioning rules where the major version indicates binary compatibility. This means, for example, that a feature may be added to RecyclerView and used in your app without requiring an update to every other library used by your app. This also means that libraries depending on androidx may provide reasonable guarantees about binary compatibility with future releases of AndroidX -- that a dependency on a 1.5.0 revision will still work when run against 1.7.0 but will likely not work against 2.0.

Migration from 28.0.0-alpha1

遷移你的app從android.support到androidx-包依賴包含兩個(gè)主要的部分:

源代碼重構(gòu)和依賴翻譯。如圖

源碼重構(gòu)會(huì)更新你的java代碼,xml資源,gradle配置引用新的類和Maven artifacts。這個(gè)功能可以在[Android Studio Canary 14](https://developer.android.com/studio/preview/)中使用,并且app的target api 需要是Android P。

如果你的依賴的庫引用了舊的Support library,Android Studio將會(huì)更新該庫的引用androidx,而不是通過dependency translation。Dependency translation 在Android Gradle Plugin 3.2.0-aplha14中被自動(dòng)應(yīng)用,它會(huì)重寫字節(jié)碼和jar資源,aar依賴(還有傳遞性依賴)去引用新的androidx-包類和artifacts。我們也會(huì)提供單獨(dú)的翻譯工具作為jar。

What's next?

We understand this is a big change for existing projects and codebases. Our intention is to provide a strong foundation that sets Android library projects up for more sustainable growth, better modularity, and smaller code size.

We hope that these changes also make it easier for developers to discover features and implement high-quality apps in less time; however, we understand that migration takes time and may not fit into everyone's production schedule. For this reason, we will continue to provide parallel updates to an android.support-packaged set of libraries for the duration of the P preview SDK timeframe. These updates will continue the 28.0.0 versioning scheme that began with 28.0.0-alpha1 in March 2018, and they will continue to be source-compatible with existing projects that depend on the android.support package.

28.0.0的穩(wěn)定版本將作為android.support的最后版本,后續(xù)所有新的功能都只會(huì)被加入androidx-packaged artifacts。

We'd love to hear from you as we iterate on this exciting future. Send us feedback by posting comments below, and please file any bugs you run into on AOSP.

We look forward to a new era of Android libraries!

Verify Android App Links (Deep Links和Android App Links的區(qū)別)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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