Search code examples
androidlombokintellij-lombok-plugin

Lombok val and var not working on Android


I'm integrating Project Lombok in our Android app. I'm running into issues with using val and var. I'm getting Incompatible Types error for a field of type var or val, when I assign a value (of any type) to it.

private final val example = new ArrayList<String>();

private var eg2;
eg2=getRandomString();

Has anyone tried var and val in Android? Any help would be appreciated.

I'm using Lombok version 1.16.12 ,Android Studio 2.3 Beta 1, Android Plugin version 2.2.3, and Gradle 3.2.1.


Solution

  • I think I can see your issue - @val and @var only work for local variables - that is variables within a method or block.

    So the following will work:

    public class ValExample {
      public String example() {
        val example = new ArrayList<String>();
        example.add("Hello, World!");
        val foo = example.get(0);
        return foo.toLowerCase();
      }
    }
    

    But it won't work with class members (this won't build):

    public class BadValExample {
      private val example = new ArrayList<String>();
    }
    

    @var also needs to be manually enabled. To do this add a lombok.config to your project with the contents:

    lombok.var.flagUsage = ALLOW