try-with-resources are not supported at this language level - Android
Asked Answered
H

2

28

I have a problem with "try-with-resources are not supported at this language level" in android in the following posted code, I tried to set language to 7 but it stills keeps giving me the same example plus it keeps giving me the option to change to language 7.

public String ReadFile(String fileName) {

    try (BufferedReader br = new BufferedReader(new FileReader(fileName+".txt"))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }

        String everything = sb.toString();
        return everything;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(SaveNLoadRank.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SaveNLoadRank.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "1";
}
Holsinger answered 18/6, 2014 at 16:36 Comment(2)
Read this post: https://mcmap.net/q/36257/-does-android-support-jdk-6-or-7-duplicate, where it's explained that Try-with-resources requires minSdkVersion 19Hokkaido
Possible duplicate of Java 7 language features with AndroidClaribel
P
47

try-with-resources is only supported if your minSdkVersion is set to 19 or higher.

Since I doubt your application support a minimum API version of 19 or higher (in June of 2014), that is likely your problem.

Support for Java 7 language features was added in SDK Tools Revision 22.6 released in March of 2014 (see here). However, try-with-resources is not a feature that is possible to introduce for previous versions of Android, so applications using that feature must run on 19+, thus the minSdkVersion requirement.

UPDATE You can now use try-with-resources with any API.

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.

To start using supported Java 8 language features, update the Android plugin to 3.0.0 (or higher). After that, for each module that uses Java 8 language features (either in its source code or through dependencies), update the Source Compatibility and Target Compatibility to 1.8 in the Project Structure dialog as shown in figure 2 (click File > Project Structure).

https://developer.android.com/studio/write/java8-support.html

Prejudicial answered 18/6, 2014 at 16:39 Comment(5)
@user3272243- Unfortunately you need to have a min of 10. Otherwise your application would be available on devices that do not support try-with-resources.Prejudicial
I was able to fix it making smaller portions of try catchs, but thanks for the help :)Holsinger
+1 for the update - I knew something weird was happening when my external libs used try with resources just fine but Andriod studio was not letting me use it in my app. I do agree with @JemshitIskenderov here though - the API version you choose to support is your decision, not a "problem".Ephebe
I was curious how this support was implemented, so researched a bit and wrote a blogpost that shows how dx/d8 desugaring works.Dercy
FWIW, running Android Studio 3.6, it keeps asking to use try-with-resources while min SDK is set to 14! I had to use 'suppress for statement'. Quite annoying and dangerous because using try-with-resources will crash on any Android lower than 19!Ardie
A
2

It is not supported below API 19, but it reportedly works on 15 and maybe even 14: https://code.google.com/p/android/issues/detail?id=73483

Arni answered 5/10, 2016 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.