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

こ雲淡風輕ζ 提交于 2019-12-03 09:38:53
Tanis.7x

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!