AWS Amplify Android构建失败:ModelConfig注解缺失type/version参数问题
AWS Amplify Android构建失败:ModelConfig注解参数疑问
我在跟着AWS Android Amplify第5模块「API与数据库」的实现指南操作时,项目构建失败,原因是Amplify自动生成的NoteData.java文件里存在未解析符号。查了ModelConfig.java文件后发现,这个注解并没有定义type和version参数,想请教这两个参数是不是必须的?能不能直接删掉?
自定义ModelConfig注解代码
package com.amplifyframework.core.model.annotations; import com.amplifyframework.core.model.Model; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * {@link ModelConfig} annotates any {@link Model} with applicable configuration information. * * The {@link RetentionPolicy#RUNTIME} annotation is added to * retain {@link ModelConfig} at runtime for the reflection capabilities to work * in order to check if this annotation is present for a field of a Model. * * {@link ElementType#TYPE} annotation is added to indicate * {@link ModelConfig} annotation can be used only on types (class, interface, enum). */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface ModelConfig { /** * Specifies the plural version of the model name. * @return the plural version of the name. */ String pluralName() default ""; /** * Specifies an array of authorization rules that should apply to this {@link Model}. * @return list of {@link AuthRule} annotations. */ AuthRule[] authRules() default {}; }
自动生成的NoteData类注解片段
import static com.amplifyframework.core.model.query.predicate.QueryField.field; /** This is an auto generated class representing the NoteData type in your schema. */ @SuppressWarnings("all") @ModelConfig(pluralName = "NoteData", type = Model.Type.USER, version = 1, authRules = { @AuthRule(allow = AuthStrategy.OWNER, ownerField = "owner", identityClaim = "cognito:username", provider = "userPools", operations = { ModelOperation.CREATE, ModelOperation.UPDATE, ModelOperation.DELETE, ModelOperation.READ }) })
解答
- type和version参数不是必须的:从你提供的
ModelConfig注解定义来看,它只包含pluralName和authRules两个参数,type和version是Amplify后续版本新增的注解参数,但你的本地注解类并没有实现这两个参数,所以编译器会抛出未解析符号的错误。 - 可以直接删除这两个参数:把
@ModelConfig里的type = Model.Type.USER, version = 1这段内容删掉,只保留已定义的参数(pluralName和authRules),就能解决构建失败的问题。 - 额外提示:出现这种情况基本是因为你使用的Amplify CLI版本和项目依赖的Amplify SDK版本不匹配。建议检查CLI版本与项目gradle中的Amplify依赖版本,尽量保持一致,避免后续再出现自动生成代码和本地注解不兼容的问题。
内容的提问来源于stack exchange,提问作者nekomaster88
相关产品推荐
相关产品推荐

