Skip to content

Instantly share code, notes, and snippets.

@richardleggett
Last active September 11, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardleggett/c87c7d9d40b7dd1748163a553c6124ce to your computer and use it in GitHub Desktop.
Save richardleggett/c87c7d9d40b7dd1748163a553c6124ce to your computer and use it in GitHub Desktop.
Lombok in Android Studio 2.2 with Jack Compiler

Steps

  1. Install the Lombok plugin via Android Studio -> Preferences -> Plugins (Search repositories).
  2. Enable Annotation Processing. With the project open go to: File -> Other Settings -> Default Settings Build, Executio... -> Compiler -> Annotation Processors
  3. Finally to be sure click File -> Invalidate Cache and Restart Android Studio

What does Lombok do?

In this example I just use the @Data annotation to generate getters/setters, toString() and hashCode() - in reality you may want to use the @Getter annotation and others to exact a more fine grained control (or to generate immutable classes with @Value).

buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "0.1"
jackOptions {
enabled true
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
applicationIdSuffix ".dev"
}
staging {
// staging utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
applicationIdSuffix ".staging"
}
production {
minSdkVersion 15
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// Testing framework
testCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'org.mockito:mockito-core:2.2.8'
androidTestCompile 'org.mockito:mockito-core:2.2.8'
// Dagger dependency injection library
provided 'javax.annotation:jsr250-api:1.0'
compile 'com.google.dagger:dagger:2.7'
annotationProcessor "com.google.dagger:dagger-compiler:2.7"
// Support libraries
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'
// Retrofit and OKHTTP
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'
// RxJava
compile 'io.reactivex:rxjava:1.0'
// Jodatime
compile 'joda-time:joda-time:2.9.3'
// Lombok
provided 'org.projectlombok:lombok:1.16.10'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
package com.myapp;
import com.google.gson.annotations.SerializedName;
import org.joda.time.DateTime;
import lombok.Data;
/**
* Base for all entities the API returns
*/
@Data
public abstract class BaseModel {
@SerializedName("id")
String id = null;
@SerializedName("createdAt")
DateTime createdAt = null;
@SerializedName("updatedAt")
DateTime updatedAt = null;
@SerializedName("modelType")
String modelType = null;
}
package com.myapp;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* Some child model class
*/
@Data
@EqualsAndHashCode(callSuper=true)
public class SampleModel extends BaseModel {
@SerializedName("aField")
private String aField = null;
}
package com.myapp.test;
import com.myapp.api.model.SampleModel;
import com.myapp.api.model.BaseModel;
import org.joda.time.DateTime;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
/**
* Tests that Lombok is correctly generating our model class methods
*/
public class LombokDomainModelTest {
@Test
public void sampleModelHasBaseModelFields() {
DateTime now = DateTime.now();
SampleModel model = new SampleModel();
model.setCreatedAt(now);
assertEquals(now, model.getCreatedAt());
}
@Test
public void identicalSampleModelsHaveEquality() {
DateTime now = DateTime.now();
SampleModel model = new SampleModel();
model.setCreatedAt(now);
SampleModel model2 = new SampleModel();
model2.setCreatedAt(now);
assertEquals(model, model2);
}
}
@Toilal
Copy link

Toilal commented Sep 11, 2017

Have you tried using @Builder annotation ? It seems to fail compiling, with Error:(xxx, xxx) Gradle: The method builder() is undefined for the type xxxxxxxxxxxxxxxx

@Toilal
Copy link

Toilal commented Sep 11, 2017

Same for @AllArgsConstructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment