0
Android Studio 3.0

classpath 'com.android.tools.build:gradle:3.0.0'

In official documentaion https://developer.android.com/studio/write/java8-support.html#supported_features

I read the next:

"In addition to the Java 8 language features and APIs above, Android Studio 3.0 and later extends support for try-with-resources to all Android API levels."

OK. And here my snippet:

public static boolean writeToDir(File path, String fileName, InputStream inputStream) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(new File(path, fileName))) {
            byte[] bytes = IOUtils.toByteArray(inputStream);
            fileOutputStream.write(bytes);
            return true;
        } catch (Exception e) {
            return false;
        } 
}

And I get compile error:

Try-with-resources requires API level 19 (current min is 15)

Why?

In officail documentation write:

"... try-with-resources to ALL Android API levels"

Alex
  • 633
  • 1
  • 7
  • 18
  • Maybe limited to "Java 8 language features and APIs above" and FileOutputStream (a child class of java.io.OutputStream) is not a part of them, I guess, and AutoCloseable is from API19. – Toris Nov 15 '17 at 18:29
  • from https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html. So java.io.FileOutputStream implements AutoCloseable – Alex Nov 15 '17 at 18:43
  • java.util.stream is listed in Java 8 Language API, but java.io.OutputStream is not. AutoCloseable is for "requires API level 19" part. – Toris Nov 15 '17 at 18:49
  • See the answer in here. https://stackoverflow.com/questions/40408628/try-with-resources-requires-api-level-19-okhttp/49615729#49615729 – Simon Apr 02 '18 at 18:12

0 Answers0