AndroidX 迁移

2019-11-18  本文已影响0人  JustDo23
AndroidX.png

引言:Android Support Library Exit History.

作者:JustDo23

时间:2019年09月18日

官网:https://developer.android.google.cn/jetpack/androidx

01. 简单概览

02. 初步使用

# 是否指定使用 AndroidX
android.useAndroidX=true
# 是否将第三方依赖转换为 AndroidX
android.enableJetifier=true

03. 项目迁移

04. Glide

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'// Glide
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'// Glide
}

如上配置,在 编译时 仍旧会报错,自动生成的文件总是引用 android.support.annotation.CheckResultSupport 注解包内的类。

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"// Kotlin

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'// Glide
    kapt 'com.github.bumptech.glide:compiler:4.9.0'// Glide
}

如上解决,项目原本没有引入 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>

如上结果,迁移 AndroidX 只需要修改 provider 节点下的 android:name 其余配置不变。

06. 迁移检查

# 只查看 release 的依赖关系
$ ./gradlew app:dependencies --configuration releaseCompileClasspath

07. 个人经验

08. 包名替换

问题包名 新版包名
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

09. 拓展阅读

上一篇 下一篇

猜你喜欢

热点阅读