
AndroidX.png
引言:Android Support Library Exit History.
作者:JustDo23
時(shí)間:2019年09月18日
官網(wǎng):https://developer.android.google.cn/jetpack/androidx
01. 簡單概覽
-
AndroidX是JetPack中與操作系統(tǒng)解除捆綁并且向后兼容的開源項(xiàng)目。 -
AndroidX完全取代Support并提供新的功能及特性。 - 所有
Support有關(guān)舊類完整映射到AndroidX中。 -
AndroidX使用嚴(yán)格的語義版本控制并可以進(jìn)行單獨(dú)更新。 - 語義版本控制
02. 初步使用
- 需要設(shè)置
compileSdkVersion為28及以上 - 在
gradle.properties文件中進(jìn)行配置
# 是否指定使用 AndroidX
android.useAndroidX=true
# 是否將第三方依賴轉(zhuǎn)換為 AndroidX
android.enableJetifier=true
- 若
useAndroidX為true則 Android插件會自動使用相應(yīng)的AndroidX而非Support - 若
enableJetifier為true則 Android插件會重寫第三方庫的二進(jìn)制文件,自動遷移現(xiàn)有的第三方庫以使用AndroidX
03. 項(xiàng)目遷移
- 使用
Android Studio 3.2及更高版本 - 菜單欄依次選擇 Refactor > Migrate to AndroidX
- 彈窗提示是否進(jìn)行
項(xiàng)目備份 - 指定備份路徑或者跳過備份
- 自動進(jìn)行
項(xiàng)目掃描并在Find中提示所有引用 - 點(diǎn)擊
Do Refactor進(jìn)行遷移 - 注意:這里并不是結(jié)束,有可能還有很多包名替換是錯(cuò)誤的,需要手動調(diào)整
04. Glide
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'// Glide
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'// Glide
}
如上配置,在 編譯時(shí) 仍舊會報(bào)錯(cuò),自動生成的文件總是引用 android.support.annotation.CheckResult 等 Support 注解包內(nèi)的類。
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"http:// Kotlin
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'// Glide
kapt 'com.github.bumptech.glide:compiler:4.9.0'// Glide
}
如上解決,項(xiàng)目原本沒有引入 Kotlin 在引入之后使用 kapt 替換注解編譯器,問題解決。
05. FileProvider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
如上結(jié)果,遷移 AndroidX 只需要修改 provider 節(jié)點(diǎn)下的 android:name 其余配置不變。
06. 遷移檢查
- 執(zhí)行指令來檢查依賴關(guān)系
# 只查看 release 的依賴關(guān)系
$ ./gradlew app:dependencies --configuration releaseCompileClasspath
- 快捷鍵
Command + Shift + F全局搜索android.support - 運(yùn)行程序隱藏的編譯錯(cuò)誤
07. 個(gè)人經(jīng)驗(yàn)
- 第一個(gè)項(xiàng)目本身引入了
Kotlin在自動遷移之后需要手動修改很多錯(cuò)誤的包名 - 第二個(gè)項(xiàng)目沒有引入過
Kotlin在自動遷移之后包名基本全部正確替換 - 可以嘗試多次進(jìn)行自動遷移操作以達(dá)到正確替換包名的目的
08. 包名替換
- 快捷鍵
Command + Shift + R進(jìn)行全局搜索替換
| 問題包名 | 新版包名 |
|---|---|
| android.support.annotation.NonNull | androidx.annotation.NonNull |
| android.support.annotation.Nullable | androidx.annotation.Nullable |
| android.support.constraint.ConstraintLayout | androidx.constraintlayout.widget.ConstraintLayout |
| android.support.v4.widget.NestedScrollView | androidx.core.widget.NestedScrollView |
| android.support.v7.widget.RecyclerView | androidx.recyclerview.widget.RecyclerView |
| android.support.v7.widget.LinearLayoutManager | androidx.recyclerview.widget.LinearLayoutManager |
| android.support.constraint.Guideline | androidx.constraintlayout.widget.Guideline |
| android.support.v7.widget.CardView | androidx.cardview.widget.CardView |
| androidx.core.view.ViewPager | androidx.viewpager.widget.ViewPager |
| android.support.v4.view.PagerAdapter | androidx.viewpager.widget.PagerAdapter |
| android.support.v4.app.FragmentManager | androidx.fragment.app.FragmentManager |
| android.support.v4.app.FragmentTransaction | androidx.fragment.app.FragmentTransaction |
| android.support.v7.app.AppCompatDialog | androidx.appcompat.app.AppCompatDialog |