Search code examples
android-studiokotlingetter-setter

Generate Getters and Setters in a Koltin Class in Android Studio


I am trying to generate Getters and Setters in my Kotlin model class without writing the code manually but the options are not showing up in the Generate Menu in Android Studio.

My Model Class (Kotlin)

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

public class EventsRoadSummaryAPINodeModel {

    @SerializedName("road_name")
    @Expose
    var road_name: String? = null

    @SerializedName("locality")
    @Expose
    var locality: String? = null

    @SerializedName("postcode")
    @Expose
    var postcode: String? = null

    @SerializedName("local_government_area")
    @Expose
    var local_government_area: String? = null

    @SerializedName("district")
    @Expose
    var district: String? = null
}

How do I generate getters and setters in a Kotlin class, please?


Solution

  • As per the Kotlin Properties and Fields documentation, each mutable (i.e., var) property has a getter and setter automatically created for it.

    Therefore there's no reason to auto generate getters and setters for Kotlin code.

    If you have annotations that need to be put on specifically the generated getter, for instance, you can use the @get:Expose syntax.