我在kotlin中使用Parcelize
。
每当我在低于21的API级别运行我的应用时,我都会遇到以下例外情况:
W/dalvikvm: VFY: register1 v5 type 17, wanted 5
W/dalvikvm: VFY: register1 v6 type 17, wanted 5
W/dalvikvm: VFY: rejecting opcode 0x76 at 0x0020
W/dalvikvm: VFY: rejected L$Creator;.createFromParcel (Landroid/os/Parcel;)Ljava/lang/Object;
W/dalvikvm: Verifier rejected class L$Creator;
W/dalvikvm: Exception Ljava/lang/VerifyError; thrown while initializing L/data/model/Student;
: Unable to invoke no-args constructor for class data.model.Student. Registering an InstanceCreator with Gson for this type may fix this problem.
I/dalvikvm: Rejecting re-init on previously-failed class L/data/model/Student; v=0x0
I/dalvikvm: Rejecting re-init on previously-failed class L/data/model/Student; v=0x0
W/dalvikvm: VFY: unable to find class referenced in signature (L/data/model/Student;)
我尝试过使用过
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
和apply plugin: "kotlin-noarg"
,但没有运气。
@Parcelize
@Entity(tableName = Constants.STUDENTS_TABLE_NAME)
data class Student(
@PrimaryKey
@ColumnInfo(name = "name")
@SerializedName("name")
val name: String,
@ColumnInfo(name = "phone")
@SerializedName("phone")
val phone: String,
@ColumnInfo(name = "address")
@SerializedName("address")
val address: String,
@ColumnInfo(name = "city")
@SerializedName("city")
val city: String,
@ColumnInfo(name = "sponsored")
@SerializedName("sponsored")
val isSponsored: Boolean,
@ColumnInfo(name = "isFavorite")
var isFavorite: Boolean
) : Parcelable
这是Gson
@Provides
@Singleton
fun provideGson(): Gson {
return GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
}