In development, if there is a need for serialization and deserialization operations, Serializable or Parcelable will be used. Both have advantages and disadvantages and can be applied to different scenarios.
The advantage of Serializable is that it’s easy to implement. You only need to implement one Serializable interface. You don’t need any extra code. So it’s easier to trigger a GC.
Parcelable requires developers to implement the serialized rules themselves, so it increases the amount of code, and because the rules are fixed, it is much more efficient and less likely to trigger GC.
On Android, I usually recommend using Parcelable, but it requires too much template code to implement. So, is there a way to make it like Serializable, with simple configuration to meet our serialization requirements? Check out Kotlin’s new support for Parcelize.
1. Kotlin’s Parcelize
Parcelize is a new feature Kotlin added in 1.1.4.
If you need to use it, make sure the Android Studio plugin for Kotlin has been upgraded to a version above 1.1.4. The latest version is 1.2.10, which I just upgraded.
An automatic Parcelable implementation generator has been automatically included in the new version of the Kotlin plug-in. In simple terms, just declare the serialized property in the main function and add an @Parcelize annotation, which will automatically create writeToParcel() and createFromParcel() for us. For the developer, just add a @parcelize annotation, and the rest is just like a normal class.
Use Parcelize
2.1 General use of Parcelable
When Kotlin didn’t support @parcelize, the Model class we wrote with Parcelable would look something like this.
2.2 using the @ Parcelize
If you use @parcelize, the template code is automatically generated for you. All you need to do is add an @parcelize annotation.
Do you feel less code?
2.3 Actually generated code
These two classes, when compiled, are virtually identical. After we decompile here, look at the code for UserParcelize().
The @Parcelize annotation actually helps us automatically generate writeToParcel() and createFromParcel(). It’s nothing fancy, but it saves us a lot of code.
3. Early configuration
@parcelize is really handy to use, but before we get there, we need to do some simple configuration.
Kotlin version 3.1
As mentioned earlier, @parcelize requires versions above Kotlin 1.1.4 to be supported, so you need to make sure your version of Kotlin is up to date. Upgrade Kotlin As described above, you can upgrade the Kotlin plug-in directly. Once the upgrade is complete, you can view the currently supported version in Preferences through the Kotlin Compiler (I’m using version 1.2 here).
3.2 Gradle configuration
@parcelize is a lab feature, so you also need to add experimental configuration to Gradle.
3.3 Troubleshooting Lint Errors
Using @parcelize directly you will face a Lint error. Of course, AS has given us hints to solve it.
Simply add @suppressLint (“ParcelCreator”) to ignore it.
Did today’s Kotlin tip help you? Any ideas can be discussed in the comments section.
Today in the background of chengxiang Ink shadow public account, reply “growth”. I will send you some of my collation of learning materials, including: Android decompile, algorithm, design pattern, virtual machine, Linux, Kotlin, Python, crawler, Web project source.
Recommended reading:
- Drawable these common tips, improve development efficiency!
- Android soft keyboard show and hide, so the operation is right
- Android decompiler, jADX advanced skills
- 6 simple tips on how to write Clean Code
- Write your first Dalvik version of HelloWorld by hand!