多场景Androidx适配指南
自多场景v1.3.0开始,正式支持Androidx。切换到Androidx分支,即可集成。
旧版集成的开发者可以按以下步骤执行转化为androidx。
1、AndroidStudio 自动转化
在 AndroidStudio 打开从 Github 下载的多场景 Demo,将项目级别的 build.gradle 文件中的
compileSdkVersion
设置为 28。因为AndroidStudio的自动转化最低要求版本28。另外需要注意:API28开始限制了明文流量网络请求,需要在AndroidManifest打开
// build.gradle(Project)
ext {
compileSdkVersion = 28
minSdkVersion = 16
targetSdkVersion = 28
//...
}
修改Android Gradle Plugin版本至3.3.0或以上。如果原项目已经是3.3.0或以上版本则不需要执行此操作。
File
->Project Structure
->Project
->Android Gradle Plugin Version
修改为3.3.0或以上。依次打开
Refactor
->Migrate to Androidx
,AndroidStudio 会自动帮你转化为 Androidx 的工程。扫描之后会弹出 Refactoring Preview,点击 Do Refactor 就会开始自动转化。
2、修改依赖
点击一下小锤子,Make Project,会发现提示主要提示两个问题:
1、
程序包android.support.annotation不存在
2、
错误: 找不到符号
符号: 类 CheckResult
位置: 类 GlideOptions
这是因为原有的注解在Androidx中不再支持了,且多场景 SDK 中引用的 Glide 4.7.1 不支持Androidx的注解,所以我们要修改部分依赖。
// build.gradle(polyvLiveCommonModul模块)
dependencies {
implementation 'com.google.android.material:material:1.0.0'
api 'com.easefun.polyv:polyvSDKLiveScenes:1.2.0'
//glide
api ("com.github.bumptech.glide:okhttp3-integration:4.7.1"){
// exclude group:'com.github.bumptech.glide',module:'glide'
}
/// ============= 以下为需要修改的依赖 =============
/// 移除4.7.1的依赖
//annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
/// 添加4.10.0的依赖
api 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
/// ============================================
}
修改完成后编译运行即可。
3、可能出现的问题
一般情况下以上两步已经可以将demo转为Androidx的工程,正常运行到手机上了,只需要按照集成文档指引即可集成到项目中。但是由于AndroidStudio版本差异,导致可能转化过程中出现一下异常情况。以下为收集到的高频次异常。
3.1 Program type already present: androidx.annotation.AnyRes
如果出现这个报错,注意检查当前的Android Gradle Plugin版本是否3.3.0及以上。Demo默认使用的AGP为3.2.0,会出现support包和androidx包注解冲突的情况。 修改AGP到3.3.0,File -> Project Structure -> Project -> Android Gradle Plugin Version 改为3.3.0 或以上。尝试重新Build,如果仍报错可以重新执行上面的Migrate to Androidx再试一遍。
3.2 Android 5.x 进入直播间崩溃,其他版本正常
该情况往往是由于Androidx库:"androidx.appcompat:appcompat:1.1.0" 的 bug,在部分 5.0 的手机中会提示以下错误,需要将该库降低为 1.0.2 版本。
android.view.InflateException: Binary XML file line #7: Error inflating class android.webkit.WebView
...
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
3.3 转化后进入直播间就崩溃
部分AndroidStudio在转化完Androidx项目后,运行多场景demo会崩溃报错:
java.lang.IllegalStateException: Fragment already added: **********Fragment{a36637b (08e2d0f4-240d-4d48-9be5-824fe73c6de2) id=0x7f090092 android:switcher:2131296402:0}
at androidx.fragment.app.FragmentManagerImpl.addFragment(FragmentManagerImpl.java:1379)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:399)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
...
这个主要是因为AndroidStudio转化的处理不当造成的,他对某些控件在xml和java文件中转化的包名并不一致,导致了渲染失败造成的。以下是要修改的对照表:
androidx.core.widget.SwipeRefreshLayout
androidx.swiperefreshlayout.widget.SwipeRefreshLayout
androidx.appcompat.widget.RecyclerView
androidx.recyclerview.widget.RecyclerView
androidx.core.view.ViewPager
androidx.viewpager.widget.ViewPager
官方的映射关系表:https://developer.android.com/jetpack/androidx/migrate/artifact-mappings
Last updated
Was this helpful?