多场景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
