auto-value-parcel常见编译错误排查清单:类型不支持与CREATOR冲突一次解决
2026/8/18 17:20:57 网站建设 项目流程

auto-value-parcel常见编译错误排查清单:类型不支持与CREATOR冲突一次解决

【免费下载链接】auto-value-parcelAn Android Parcelable extension for Google's AutoValue.项目地址: https://gitcode.com/gh_mirrors/au/auto-value-parcel

在 Android 开发中,auto-value-parcel是让 Google AutoValue 自动生成 Parcelable 实现的利器,但很多新手在接入时会卡在编译报错上,其中「属性类型不支持」和「CREATOR 冲突」出现频率最高。这篇 auto-value-parcel 常见编译错误排查清单,整理了典型报错信息、出错原因和一步到位的修复方法,帮你告别反复试错。

报错前先自查:你的依赖配置正确吗?

auto-value-parcel 常见编译错误里,有相当一部分其实是依赖没配好。确认以下两项都已加入build.gradle

annotationProcessor 'com.ryanharter.auto.value:auto-value-parcel:0.2.9' // 如果需要使用自定义 TypeAdapter,再加这一行 implementation 'com.ryanharter.auto.value:auto-value-parcel-adapter:0.2.9'

注意:只加注解处理器、漏掉auto-value-parcel-adapter,会导致@ParcelAdapter注解无法解析,这也是「类型不支持」报错的隐藏原因之一。

错误一:属性类型不支持(not a supported Parcelable type)

典型报错信息

编译时会出现类似这样的 ERROR:

AutoValue property tea is not a supported Parcelable type.

该错误的校验逻辑位于 AutoValueParcelExtension.java 的validateProperties()方法中。

出错原因

auto-value-parcel 默认只支持与Parcel类原生读写能力对齐的类型清单(定义在 Parcelables.java 的VALID_TYPES中),包括:

类别支持类型
基本类型int、long、float、double、boolean、byte、short、char 及其包装类
常用对象String、CharSequence、Serializable、IBinder
集合容器List、Map、Guava ImmutableCollection / ImmutableMap
Android 类型Bundle、PersistableBundle、SparseArray、SparseBooleanArray、Size、SizeF
数组boolean[]、byte[]、char[]、int[]、long[]、String[]、Parcelable[]、Object[]
其他Parcelable 及其子类、枚举(Enum)

只要你的属性类型不在此列(比如自定义的普通 JavaBean、Date),就会触发「类型不支持」报错。

最快修复方法:Map 键值类型限制

先注意一个特殊规则:Map只能使用String 作为 key、合法的 Parcelable 类型作为 value,否则会报:

Maps can only have String objects for keys and valid Parcelable types for values.

请把Map<Integer, String>Map<String, MyBean>这类写法改掉。

终极解法:用 @ParcelAdapter 自定义序列化

对于Date、自定义类等不支持的属性,最优雅的方案是编写一个 TypeAdapter,接口定义见 TypeAdapter.java:

public class DateTypeAdapter implements TypeAdapter<Date> { public Date fromParcel(Parcel in) { return new Date(in.readLong()); } public void toParcel(Date value, Parcel dest) { dest.writeLong(value.getTime()); } }

然后在属性上用@ParcelAdapter注解(见 ParcelAdapter.java):

@AutoValue public abstract class Foo implements Parcelable { @ParcelAdapter(DateTypeAdapter.class) public abstract Date date(); }

⚠️ 注意:TypeAdapter 类必须提供public 无参构造器,因为生成代码会直接new它来实例化。

错误二:CREATOR 字段冲突(Manual implementation of CREATOR)

典型报错信息

Manual implementation of a static Parcelable.Creator<T> CREATOR field found when processing test.Test. Remove this so auto-value-parcel can automatically generate the implementation for you.

出错原因

很多同学习惯像手写 Parcelable 那样,在类里自己写public static final Parcelable.Creator<Foo> CREATOR,但 auto-value-parcel 会自动生成 CREATOR。检测逻辑在 AutoValueParcelExtension.java 的findCreator()方法中:只要发现类中存在名为CREATOR的 static 字段,就直接报错终止。

一步到位修复

把类里手写的CREATOR字段整段删除,交给插件自动生成。同理,如果你手写了writeToParcel(Parcel, int)方法,也会触发类似的报错(见 findWriteToParcel),一样删掉即可。auto-value-parcel 会为你生成完整的describeContents()writeToParcel()CREATOR

错误三:编译报错但看不出原因?试试 failExplosively

当校验失败时,插件默认只是输出错误信息并跳过生成。如果你希望编译直接失败以快速定位问题,可以在gradle.propertiesbuild.gradle中开启:

android { defaultConfig { javaCompileOptions { annotationProcessorOptions { arguments += ["avparcel.failExplosively": "true"] } } } }

开启后,校验失败会抛出AutoValueParcelException(定义见 AutoValueParcelException.java),错误信息会带完整堆栈,排查起来一目了然。

快速排查路线图

面对 auto-value-parcel 编译错误,按下面顺序走一遍基本都能解决:

  1. 🔍 先确认auto-value-parcelauto-value-parcel-adapter依赖都已添加;
  2. 🧹 删除手写的CREATOR字段和writeToParcel()方法;
  3. 📋 对照上方类型表检查所有属性,Map确保 key 是 String;
  4. 🛠 对自定义类型编写 TypeAdapter,并用@ParcelAdapter标注;
  5. 🚨 仍无法定位时,开启avparcel.failExplosively让编译直接暴露问题。

掌握这份 auto-value-parcel 常见编译错误排查清单后,无论是「类型不支持」还是「CREATOR 冲突」,都能快速定位并一次解决。如果你刚接触 AutoValue + Parcelable 组合,建议先从最简单的@AutoValue类开始验证依赖,再逐步引入自定义类型,这样可以把问题范围缩到最小。

【免费下载链接】auto-value-parcelAn Android Parcelable extension for Google's AutoValue.项目地址: https://gitcode.com/gh_mirrors/au/auto-value-parcel

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询