多场景Androidx适配指南
Starting from multi-scenario v1.3.0, Androidx is officially supported. Switch to the Androidx branch to integrate.
Developers using the old integration can follow the steps below to migrate to Androidx.
1. Automatic Conversion via AndroidStudio
- Open the multi-scenario Demo downloaded from Github in AndroidStudio, and set
compileSdkVersionin the project-levelbuild.gradlefile to 28. This is because AndroidStudio's automatic conversion requires a minimum version of 28.- Note: API 28 restricts cleartext network traffic. You need to enable it in AndroidManifest:
// build.gradle(Project)
ext {
compileSdkVersion = 28
minSdkVersion = 16
targetSdkVersion = 28
//...
}
- Update the Android Gradle Plugin version to 3.3.0 or higher. If your project already uses 3.3.0 or higher, this step is unnecessary.
File->Project Structure->Project->Android Gradle Plugin VersionChange to 3.3.0 or higher. - Navigate to
Refactor->Migrate to Androidx. AndroidStudio will automatically convert your project to Androidx. After scanning, a Refactoring Preview will appear. Click Do Refactor to start the automatic conversion.
2. Modify Dependencies
Click the hammer icon to Make Project. You will likely see two main issues:
1、
程序包android.support.annotation不存在
2、
错误: 找不到符号
符号: 类 CheckResult
位置: 类 GlideOptions
This is because the original annotations are no longer supported in Androidx, and the Glide 4.7.1 referenced in the multi-scenario SDK does not support Androidx annotations. Therefore, some dependencies need to be modified.
// 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'
/// ============================================
}
After making the changes, compile and run.
3. Potential Issues
Generally, the above two steps are sufficient to convert the demo to an Androidx project and run it on a device. You can then follow the integration documentation to integrate it into your project. However, due to differences in AndroidStudio versions, some anomalies may occur during conversion. Below are frequently encountered issues.
3.1 Program type already present: androidx.annotation.AnyRes
If this error occurs, check whether the current Android Gradle Plugin version is 3.3.0 or higher. The demo uses AGP 3.2.0 by default, which can cause annotation conflicts between the support library and Androidx.
Update AGP to 3.3.0: File -> Project Structure -> Project -> Android Gradle Plugin Version, change to 3.3.0 or higher. Try rebuilding. If the error persists, re-run the Migrate to Androidx process.
3.2 Crash on Android 5.x when entering a live room, but works on other versions
This is often caused by a bug in the Androidx library androidx.appcompat:appcompat:1.1.0. On some Android 5.0 devices, the following error may occur. Downgrade this library to version 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 Crash immediately after entering a live room after conversion
Some AndroidStudio versions may cause the multi-scenario demo to crash after converting to Androidx, with the following error:
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)
...
This is mainly due to improper handling during conversion. The package names for certain controls in XML and Java files may not match, causing rendering failures. Below is a reference table for corrections:
| Incorrect Control | Correct Package Name |
|---|---|
| androidx.core.widget.SwipeRefreshLayout | androidx.swiperefreshlayout.widget.SwipeRefreshLayout |
| androidx.appcompat.widget.RecyclerView | androidx.recyclerview.widget.RecyclerView |
| androidx.core.view.ViewPager | androidx.viewpager.widget.ViewPager |
Official mapping table: https://developer.android.com/jetpack/androidx/migrate/artifact-mappings
