kotlin uses Parcelize annotations to simplify Parcelable writing

kotlin uses Parcelize annotations to simplify Parcelable writing

Parcelize Note

kotlin added support for parcelable at version 1.1.4

The Android extension now includes an automated generator that implements Parcelable.The generator automatically creates the writeToParcel ()/ createFromParcel () method by declaring the serialized properties in the main constructor and adding a @Parcelize comment

Demand Environment

Kotlin version 1.1.4 or higher

Start using

stayBuild.gradleAdd support in

apply plugin: 'kotlin-android-extensions'
androidExtensions {
    experimental = true

Entity Class


data class Student(val id: String, val name: String, val grade: String) : Parcelable

Comparing Java entity classes


public class Student implements Parcelable{
        private String id;
        private String name;
        private String grade;

        // Constructor
        public Student(String id, String name, String grade){
            this.id = id;
            this.name = name;
            this.grade = grade;
       // Getter and setter methods

       // Parcelling part
       public Student(Parcel in){
           String[] data = new String[3];

           // the order needs to be the same as in writeToParcel() method
           this.id = data[0];
           this.name = data[1];
           this.grade = data[2];

       public int describeContents(){
           return 0;

       public void writeToParcel(Parcel dest, int flags) {
           dest.writeStringArray(new String[] {this.id,
       public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
           public Student createFromParcel(Parcel in) {
               return new Student(in); 

           public Student[] newArray(int size) {
               return new Student[size];

About Lint Check Errors

Now Android Studio will pop up this error in the image below. This is a Bug from IntelliJ. You can find the answer in the link below


So you can ignore this warning and compile the project, or you can add the @SuppressLint("ParcelCreator") comment

Links in English


Tags: Android Java

Posted on Wed, 15 Jul 2020 10:24:14 -0400 by mallard