简体   繁体   中英

How do I enable Android Studio/IDEA closure folding for anomynous class in Java 8?

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?

PS 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM