如何解决Android Studio中Render problem Path.op() not supported错误?
最近我碰到了一个头疼的问题:Android Studio布局预览一直报Render problem: Path.op() not supported,试了强制刷新布局、重启AS、更新到3.5.3版本、临时关闭杀毒软件这些常规操作,都没搞定。更奇怪的是,应用编译运行完全正常,就是预览界面这个报错消不掉。
我的环境配置
- Android Studio 3.5.3
- Android SDK Tools 29.0.2
- Android SDK Platform Android 10.0 (Q)
- Android SDK Platform 29
- Default OpenJDK Platform binary
错误复现步骤
- 创建带空Activity的新Android项目
- 将布局替换为以下代码:
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/parent_view" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@android:color/white"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> <include layout="@layout/toolbar" /> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:id="@+id/nested_scroll_view" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:scrollbars="none" android:scrollingCache="true" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="0dp" app:cardElevation="2dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.viewpager.widget.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="250dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:background="@android:color/darker_gray" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:orientation="vertical" android:paddingBottom="16dp" android:paddingLeft="12dp" android:paddingRight="12dp" android:paddingTop="14dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Title is going here" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="@android:color/white" /> <TextView android:id="@+id/brief" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Location" android:textAppearance="@style/TextAppearance.AppCompat.Small" /> </LinearLayout> <LinearLayout android:id="@+id/layout_dots" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_gravity="end" android:gravity="center" android:orientation="horizontal" /> </RelativeLayout> </RelativeLayout> </androidx.cardview.widget.CardView> <View android:layout_width="0dp" android:layout_height="16dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Description" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textColor="@android:color/darker_gray" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:lineSpacingExtra="4dp" android:text="very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum very_long_lorem_ipsum " android:textAppearance="@style/TextAppearance.AppCompat.Subhead" android:textColor="@android:color/darker_gray"/> </LinearLayout> <View android:layout_width="0dp" android:layout_height="8dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
亲测有效的解决方案
1. 降低预览的API版本
这个错误大多是API 29的预览渲染器存在兼容性bug导致的。在布局编辑器顶部的API Level下拉菜单里,选择API 28或者更低的版本,切换后点击刷新按钮,预览界面的报错基本就能消失。毕竟应用能正常运行,说明代码逻辑没问题,只是预览器的适配问题。
2. 切换到Material Components主题
检查项目的styles.xml文件,如果你当前使用的是Theme.AppCompat.Light.DarkActionBar这类AppCompat主题,换成Theme.MaterialComponents.Light.DarkActionBar试试。因为布局里用到了AppBarLayout、CardView等Material组件,使用Material主题能更好地匹配预览渲染器的要求。
3. 禁用预览的硬件加速
打开Android Studio设置:File > Settings > Build, Execution, Deployment > Build Tools > Layout Editor,找到并勾选Use legacy layout renderer(使用旧版布局渲染器),或者开启Disable hardware acceleration for preview选项。硬件加速预览有时候会触发图形API不支持的问题,用软件渲染就能避开这个坑。
4. 更新Material Components库版本
打开build.gradle(Module: app)文件,检查Material库的依赖版本,如果是比较旧的版本(比如1.0.0),升级到稳定的新版本(比如1.2.1),新版本通常会修复预览时的渲染bug,也能解决这类报错。
内容的提问来源于stack exchange,提问作者Eng. Samer T

