Getting rid of boilerplate code with Lombok (Android)

Chizoba Ogbonna
Chizoba’s Blog
Published in
3 min readFeb 5, 2018

As a software developer, you would have created several model classes (POJOs), which require several boilerplate code (constructors, getters, setters, toString(), equals() , hashCode()). These boilerplate code usually makes these model classes lengthy, difficult to read, and for any change in a variable, you’d need to make those changes in each of these methods.

These can be gotten rid of by using Lombok.

What is Lombok?

Lombok is a java library that automatically plugs into your editor and build tools, to generate those boilerplate code, keeping your code short, clean, hence, spicing up your java.

Also note that Lombok won’t make your android application any ‘heavier’ because lombok is a compile-time only library. Cool right?

Sneak peak at how it looks

Regular model class without lombok

import java.util.Date;public class User {
private String userName;
private String fullName;
private int age;
private double height;
private Date dateOfBirth;
public User(String userName, String fullName, int age, double height, Date dateOfBirth) {
this.userName = userName;
this.fullName = fullName;
this.age = age;
this.height = height;
this.dateOfBirth = dateOfBirth;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;if (age != user.age)
return false;
if (Double.compare(user.height, height) != 0)
return false;
if (userName != null ? !userName.equals(user.userName) : user.userName != null)
return false;
if (fullName != null ? !fullName.equals(user.fullName) : user.fullName != null)
return false;
return dateOfBirth != null ? dateOfBirth.equals(user.dateOfBirth) : user.dateOfBirth == null;
}@Override
public int hashCode() {
int result;
long temp;
result = userName != null ? userName.hashCode() : 0;
result = 31 * result + (fullName != null ? fullName.hashCode() : 0);
result = 31 * result + age;
temp = Double.doubleToLongBits(height);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (dateOfBirth != null ? dateOfBirth.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", fullName='" + fullName + '\'' +
", age=" + age +
", height=" + height +
", dateOfBirth=" + dateOfBirth +
'}';
}
}

But with Lombok

import java.util.Date;import lombok.Data;@Data
public class User {
private String userName;
private String fullName;
private int age;
private double height;
private Date dateOfBirth;
}

Soooooooooo EASY right?

How to use?

1. Download Lombok IntelliJ Plugin to enable lombok support in your Android Studio.

  • Go to File > Settings > Plugins
  • Click on Browse repositories...
  • Search for Lombok Plugin
  • Click on Install plugin
  • Restart Android Studio

Note: This requires gradle v2.12 or newer

2. Add this to your application dependency block

dependencies {
compileOnly 'org.projectlombok:lombok:1.16.20'
annotationProcessor 'org.projectlombok:lombok:1.16.20'
}

3. Create a file called lombok.config in your project’s root directory, and add this in it

lombok.addGeneratedAnnotation = false

That’s all!!! No more unnecessary lengthy code :)

For more information

Thanks for reading and keep coding smart ;)

--

--