Fork me on GitHub

Project Lombok - android instructions

Feature Overview Discuss / Help Contribute Report an issue
Download!
Version: @VERSION@ | changelog

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 on android devices. Also, errors will occur due to the native libraries present in lombok.jar itself. Unfortunately, android does not (yet) understand the concept of a compile-time-only dependency, so you need to mess with your build files to make it work.

The instructions listed below are excerpts from The AndroidAnnotations project cookbook. You may wish to refer to that documentation for complete instructions; lombok is just the equivalent to androidannotations-VERSION.jar; there is no -api aspect.

Eclipse

In eclipse, create a 'lightweight' lombok jar that contains only the annotations by running:
java -jar lombok.jar publicApi
Then, add the lombok-api.jar file created by running this command to your android project instead of the complete lombok.jar, and, as usual, install lombok into eclipse by double-clicking lombok.jar.

Ant

  • Find build.xml in ${ANDROID_SDK_ROOT}/tools/ant/build.xml and copy the -compile target into the paste buffer.
  • Copy this to the build.xml of your own project, right before the <import file="${sdk.dir}/tools/ant/build.xml"> line.
  • Create a compile-libs directory in your own project and copy the complete lombok.jar to it.
  • Now modify the <classpath> entry inside the <javac> task in the -compile target you just copied:
    add <fileset dir="compile-libs" includes="*.jar" /> to it.

Maven

You should be able to just follow the normal lombok with maven instructions.
Note that if you use android, eclipse, and maven together you may have to replace lombok.jar in your eclipse android project's build path (which you can modify in that project's properties page) with lombok-api.jar, as produced in the procedure explained for Eclipse, above.

Gradle & Android Studio

While there might be other ways, this is how I got lombok working for Gradle and Android Studio.

Gradle Setup

  • First of all make sure that the version of your android plugin is >= 0.4.3
  • Then you need android-apt, follow the instructions on thier site
  • Add Lombok to your Apps dependencies-Block
    	provided "org.projectlombok:lombok:1.12.6"
    	apt "org.projectlombok:lombok:1.12.6"
    
  • Now you should be able to compile your App using Lombok Annotations
NOTE: Android does not have the @java.beans.ConstructorProperties therefor you have to use the suppressConstructorProperties property when using @XArgsConstructor Annotations. With Lombok >= 1.12.7 you can use the lombok.config file to enable this property projectwide by adding:

lombok.anyConstructor.suppressConstructorProperties = true

Android Studio

While the Gradle build already works, Android Studio is not able to see the generated methods. To fix this, you have to install the Lombok Plugin for IntelliJ IDEA/Android Studio.
  • Go to File > Settings > Plugins
  • Click on Browse repositories...
  • Search for Lombok Plugin
  • Click on Install plugin
  • Restart Android Studio & Profit!