Android 的 Java 7 语言特性 [英] Java 7 language features with Android

查看:31
本文介绍了Android 的 Java 7 语言特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否有人尝试过在 Android 上使用新的 Java 7 语言功能?我知道 Android 会读取 Java 吐出的字节码并将其转换为 dex.所以我想我的问题是它可以理解 Java 7 的字节码吗?

解决方案

如果您使用的是 Android Studio,Java 7 语言 应该会自动启用,无需任何补丁.Try-with-resource 需要 API Level 19+,并且缺少 NIO 2.0 的东西.

如果您不能使用 Java 7 功能,请参阅 和传统界面,如 Closeable 确实从 AutoCloseable 继承(尽管确实缺少 SafeVarargs).我们可以通过反射来确认它的存在.它们被隐藏只是因为 Javadoc 具有 @hide 标记,这导致android.jar"不包含它们.

已经存在问题 如何使用可用的隐藏和内部 API 构建 Android SDK? 关于如何恢复这些方法.您只需要替换当前平台的现有android.jar"引用为我们自定义的引用,然后许多 Java 7 API 将可用(该过程与 Eclipse 中的类似.检查项目结构 → SDK.)

除了 AutoCloseable 之外,(仅)还显示了以下 Java 7 库功能:

  • ConcurrentModificationException、LinkageError 和 AssertionError 中的异常链构造函数
  • 用于基元的静态 .compare() 方法:Boolean.compare()、Byte.compare()、Short.compare()、Character.compare()、Integer.compare()、Long.compare().
  • 货币:.getAvailableCurrencies(), .getDisplayName() (但没有 .getNumericCode())
  • BitSet:.previousSetBit(), .previousClearBit(), .valueOf(), .toLongArray(), .toByteArray()
  • 集合:.emptyEnumeration(), .emptyIterator(), .emptyListIterator()
  • AutoCloseable
  • Throwable:.addSuppressed()、.getSuppressed() 和 4 参数构造函数
  • 字符:.compare(), .isSurrogate(), .getName(), .highSurrogate(), .lowSurrogate(), .isBmpCodePoint()(但没有 .isAlphabetic()和.isIdeographic())
  • 系统:.lineSeparator()(未记录?)
  • java.lang.reflect.Modifier: .classModifiers(), .constructorModifiers(), .fieldModifiers(), .interfaceModifiers(), .methodModifiers()
  • 网络接口:.getIndex(), .getByIndex()
  • InetSocketAddress: .getHostString()
  • InetAddress:.getLoopbackAddress()
  • 记录器:.getGlobal()
  • ConcurrentLinkedDeque
  • AbstractQueuedSynchronizer:.hasQueuedPredecessors()
  • DeflaterOutputStream:3带有syncFlush"的构造函数.
  • Deflater:.NO_FLUSH, .SYNC_FLUSH, .FULL_FLUSH, .deflate() 有 4 个参数

基本上就是这样.特别是,NIO 2.0 不存在,Arrays.asList 仍然不是@SafeVarargs.

Just wondering if anyone has tried using new Java 7 language features with Android? I know that Android reads the bytecode that Java spits out and turns it to dex. So I guess my question is can it understand the bytecode of Java 7?

解决方案

If you are using Android Studio, the Java 7 language should be enabled automatically without any patches. Try-with-resource requires API Level 19+, and NIO 2.0 stuff are missing.

If you can't use Java 7 features, see @Nuno's answer on how to edit your build.gradle.

The following is for historical interest only.


A small part of Java 7 can certainly be used with Android (note: I have only tested on 4.1).

First of all, you could not use Eclipse's ADT because it is hard-coded that only Java compiler 1.5 and 1.6 are compliant. You could recompile ADT but I find there is no simple way to do that aside from recompiling the whole Android together.

But you don't need to use Eclipse. For instance, Android Studio 0.3.2, IntelliJ IDEA CE and other javac-based IDEs supports compiling to Android and you could set the compliance even up to Java 8 with:

  • File → Project Structure → Modules → (pick the module at the 2nd pane) → Language level → (choose "7.0 - Diamonds, ARM, multi-catch, etc.")

This only allows Java 7 language features, and you can hardly benefit from anything since a half of improvement also comes from the library. Features you could use are those which do not depend on the library:

  • Diamond operator (<>)
  • String switch
  • Multiple-catch (catch (Exc1 | Exc2 e))
  • Underscore in number literals (1_234_567)
  • Binary literals (0b1110111)

And these features cannot be used yet:

  • The try-with-resources statement — because it requires the non-existing interface "java.lang.AutoCloseable" (this can be used publicly in 4.4+)
  • The @SafeVarargs annotation — because "java.lang.SafeVarargs" does not exist

... "yet" :) It turns out that, although Android's library is targeting for 1.6, the Android source does contain interfaces like AutoCloseable and traditional interfaces like Closeable does inherit from AutoCloseable (SafeVarargs is really missing, though). We could confirm its existence via reflection. They are hidden simply because the Javadoc has the @hide tag, which caused the "android.jar" not to include them.

There is already as existing question How do I build the Android SDK with hidden and internal APIs available? on how to get those methods back. You just need to replace the existing "android.jar" reference of the current Platform with our customized one, then many of the Java 7 APIs will become available (the procedure is similar to that in Eclipse. Check Project Structure → SDKs.)

In additional to AutoCloseable, (only) the following Java 7 library features are also revealed:

  • Exception chaining constructors in ConcurrentModificationException, LinkageError and AssertionError
  • The static .compare() methods for primitives: Boolean.compare(), Byte.compare(), Short.compare(), Character.compare(), Integer.compare(), Long.compare().
  • Currency: .getAvailableCurrencies(), .getDisplayName() (but without .getNumericCode())
  • BitSet: .previousSetBit(), .previousClearBit(), .valueOf(), .toLongArray(), .toByteArray()
  • Collections: .emptyEnumeration(), .emptyIterator(), .emptyListIterator()
  • AutoCloseable
  • Throwable: .addSuppressed(), .getSuppressed(), and the 4-argument constructor
  • Character: .compare(), .isSurrogate(), .getName(), .highSurrogate(), .lowSurrogate(), .isBmpCodePoint() (but without .isAlphabetic() and .isIdeographic())
  • System: .lineSeparator() (undocumented?)
  • java.lang.reflect.Modifier: .classModifiers(), .constructorModifiers(), .fieldModifiers(), .interfaceModifiers(), .methodModifiers()
  • NetworkInterface: .getIndex(), .getByIndex()
  • InetSocketAddress: .getHostString()
  • InetAddress: .getLoopbackAddress()
  • Logger: .getGlobal()
  • ConcurrentLinkedDeque
  • AbstractQueuedSynchronizer: .hasQueuedPredecessors()
  • DeflaterOutputStream: the 3 constructors with "syncFlush".
  • Deflater: .NO_FLUSH, .SYNC_FLUSH, .FULL_FLUSH, .deflate() with 4 arguments

That's basically all. In particular, NIO 2.0 does not exist, and Arrays.asList is still not @SafeVarargs.

这篇关于Android 的 Java 7 语言特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆