auto-value-parcel常见编译错误排查清单:类型不支持与CREATOR冲突一次解决 auto-value-parcel常见编译错误排查清单类型不支持与CREATOR冲突一次解决【免费下载链接】auto-value-parcelAn Android Parcelable extension for Googles 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.gradleannotationProcessor 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典型报错信息编译时会出现类似这样的 ERRORAutoValue 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 / ImmutableMapAndroid 类型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.请把MapInteger, String、MapString, MyBean这类写法改掉。终极解法用 ParcelAdapter 自定义序列化对于Date、自定义类等不支持的属性最优雅的方案是编写一个 TypeAdapter接口定义见 TypeAdapter.javapublic class DateTypeAdapter implements TypeAdapterDate { public Date fromParcel(Parcel in) { return new Date(in.readLong()); } public void toParcel(Date value, Parcel dest) { dest.writeLong(value.getTime()); } }然后在属性上用ParcelAdapter注解见 ParcelAdapter.javaAutoValue 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.CreatorT 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.CreatorFoo CREATOR但 auto-value-parcel 会自动生成 CREATOR。检测逻辑在 AutoValueParcelExtension.java 的findCreator()方法中只要发现类中存在名为CREATOR的 static 字段就直接报错终止。一步到位修复把类里手写的CREATOR字段整段删除交给插件自动生成。同理如果你手写了writeToParcel(Parcel, int)方法也会触发类似的报错见 findWriteToParcel一样删掉即可。auto-value-parcel 会为你生成完整的describeContents()、writeToParcel()和CREATOR。错误三编译报错但看不出原因试试 failExplosively当校验失败时插件默认只是输出错误信息并跳过生成。如果你希望编译直接失败以快速定位问题可以在gradle.properties或build.gradle中开启android { defaultConfig { javaCompileOptions { annotationProcessorOptions { arguments [avparcel.failExplosively: true] } } } }开启后校验失败会抛出AutoValueParcelException定义见 AutoValueParcelException.java错误信息会带完整堆栈排查起来一目了然。快速排查路线图面对 auto-value-parcel 编译错误按下面顺序走一遍基本都能解决 先确认auto-value-parcel与auto-value-parcel-adapter依赖都已添加 删除手写的CREATOR字段和writeToParcel()方法 对照上方类型表检查所有属性Map确保 key 是 String 对自定义类型编写 TypeAdapter并用ParcelAdapter标注 仍无法定位时开启avparcel.failExplosively让编译直接暴露问题。掌握这份 auto-value-parcel 常见编译错误排查清单后无论是「类型不支持」还是「CREATOR 冲突」都能快速定位并一次解决。如果你刚接触 AutoValue Parcelable 组合建议先从最简单的AutoValue类开始验证依赖再逐步引入自定义类型这样可以把问题范围缩到最小。【免费下载链接】auto-value-parcelAn Android Parcelable extension for Googles AutoValue.项目地址: https://gitcode.com/gh_mirrors/au/auto-value-parcel创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考