5

I really like Android Studio's code folding for anonymous class:

new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        doSomething();
        return null;
    }
}.execute();

into

(AsyncTask) (params) -> {
    doSomething()
}.execute();

This folding option is under "closures" in the settings. I switched to Java 8 (using retrolambda) since I wanted to use lambda expressions, however now Android Studio doesn't fold anonymous class. Is there a way to re-enable it?

P.S. I know I can stay at Java 7 and Android Studio would fold single-method anonymous class into lambda expression, but I want to be able to type lambda expressions directly, while still have multi-method anonymous class folded.

Khen Tek
  • 51
  • 2
  • 1
    When you move your cursor on the anonymous class and press `Ctrl + .`, it should fold it. – ehehhh Dec 07 '15 at 21:32
  • That folds it into new AsyncTask() {...}.execute(); Which doesn't show what is being overridden. – Khen Tek Dec 07 '15 at 22:27
  • The code folding should only work for functional interfaces (interfaces with one method), I think that's why it doesn't work with `AsyncTask`, which is an abstract class. I might be wrong though. – ehehhh Dec 07 '15 at 22:31
  • It does work with folding anonymous class extending an abstract class as I showed above. However it seems to be disabled with folding functional interface when Java 8 is used. P.S. Hell I would actually want it to fold into (AsyncTask) doInBackground(params) -> ...., but it is good enough I can see what is in the method – Khen Tek Dec 07 '15 at 23:23

1 Answers1

0

First you need to set JDK location to java 8 in under project structure -> SDK Location

Right now Android Studio 2.1 does not support lambda notation for java 8 but there is workaround for that.

  1. You need to enable Jack compiler to support this feature
  2. Add android { jackOptions { enabled true } } in build.gradle file
  3. Also it would require Android Sdk Build-tools 24 or later. To download that open standalone SDK manager under sdk manager and download