How to automatically generate getters and setters in Android Studio

后端 未结 15 1247
太阳男子
太阳男子 2020-12-02 04:15

Is there a shortcut in Android Studio for automatically generating the getters and setters in a given class?

15条回答
  •  一向
    一向 (楼主)
    2020-12-02 04:58

    This answer deals with your question but is not exactly an answer to it. =) It's an interesting library I found out recently and I want to share with you.


    Project Lombok can generate common methods, such as getters, setters, equals() and hashCode(), toString(), for your classes automatically. It replaces them with annotations reducing boilerplate code. To see a good example of code written using Lombok watch a video on the main page or read this article.

    Android development with Lombok is easy and won't make your android application any 'heavier' because Lombok is a compile-time only library. It is important to configure your Android project properly.

    Another example:

    import lombok.Getter;
    import lombok.Setter;
    
    public class Profile {
    
      @Getter @Setter
      private String username;
    
      @Getter @Setter
      private String password;
    
    }
    

    Android development with Lombok is possible. Lombok should be a compile-time only dependency, as otherwise the entirety of Lombok will end up in your DEX files, wasting precious space. Gradle snippet:

    dependencies {
        compileOnly "org.projectlombok:lombok:1.16.18"
    }
    

    In addition you may want to add the Lombok IntelliJ plugin to support Lombok features in your IDE at development time. Also there is Hrisey library which is based on Lombok. Simply put, it's Lombok + Parcellable support.

提交回复
热议问题