Java的与Android 7语言功能 [英] Java 7 language features with Android

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

问题描述

只是想知道是否有人使用新的Java 7的语言功能与Android试过吗? 我知道Android读取字节code,它的Java吐出,并将其以地塞米松。所以,我想我的问题是可以把它理解Java 7中的字节code?

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?

推荐答案

Java 7中的一小部分当然可以与Android使用(注:我只考4.1)。

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

首先,你不能使用Eclipse的ADT因为<一href="https://android.googlesource.com/platform/sdk/+/master/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtConstants.java">it是硬codeD 只有Java编译器1.5和1.6兼容。你可以重新编译ADT,但我觉得有没有简单的方法来做到这一点除了重新编译整个安卓在一起。

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.

但你并不需要使用Eclipse。例如, Android的工作室0.3.2 ,的 IntelliJ IDEA的CE 和其他的javac为主的IDE支持编译到Android 你可以设置符合甚至达到的Java 8:

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:

  • 文件→项目结构→模块→(接模块在第二面板)→语言水平→(选择7.0 - 钻石,ARM,多抓,等)

这仅允许Java 7的语言功能,然后你可以从几乎任何获益,因为改善一半也来自库。你可以使用的功能是那些不依赖于该库:

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:

  • 钻石操作符(&LT;&GT;
  • 字符串开关
  • 多抓(赶上(EXC1 | EXC2 E)
  • 在下划线的文字数量( 1_234_567
  • 在二进制文字( 0b1110111
  • 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:

  • 尝试 -with-resources语句 - 因为它需要非现有的接口java.lang.AutoCloseable(这是可以公开在4.4 +使用)
  • 的@SafeVarargs注释 - 因为java.lang.SafeVarargs不存在
  • 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

...,但:)事实证明,尽管Android的库的目标是1.6,Android的源代码确实包含像<一个接口href="https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/lang/AutoCloseable.java">AutoCloseable而像<传统接口href="https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/io/Closeable.java">Closeable并从AutoCloseable继承(SafeVarargs是真正缺少的,虽然)。我们可以确认通过反射它的存在。他们只是隐藏,因为Javadoc中有 @hide 标签,这引起了的android.jar不包括它们。

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

目前已经为现有问题的 <一个href="http://stackoverflow.com/questions/7888191/how-do-i-build-the-android-sdk-with-hidden-and-internal-apis-available">How做我建立了Android SDK与隐蔽性和内部API可用? 如何让这些方法了。你只需要的替换的现有的android.jar当前平台与我们的自定义之一,那么许多了Java 7的API将变得可用(此过程类似于在Eclipse的参考。检查项目结构→软件开发工具包)。

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

在附加AutoCloseable,(仅)下面的Java 7的库功能也透露:

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

  • 在ConcurrentModificationException的,LinkageError类和AssertionError的异常链构造
  • 在静态.compare()原语的方法:Boolean.compare(),Byte.compare(),Short.compare(),Character.compare(),Integer.compare(),Long.compare()
  • 货币:.getAvailableCurrencies(),.getDisplayName() (不过的没有的.getNumeric code())
  • 位集合:previousSetBit()。 previousClearBit(),.valueOf(),.toLongArray(),.toByteArray()
  • 收藏:.emptyEnumeration(),.emptyIterator() ,.emptyListIterator()
  • AutoCloseable
  • 的Throwable :.addSup pressed() .getSup pressed(),和4个参数的构造函数
  • 性格:.compare(),.isSurrogate() ,.getName(),.highSurrogate(),.lowSurrogate(),.isBmp $ C $口岸系统(),(但是的没有的.isAlphabetic()和.isIdeographic())
  • 系统:(?无证).lineSeparator()
  • <一个href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Modifier.html">java.lang.reflect.Modifier: .classModifiers(),.constructorModifiers(),.fieldModifiers(),.interfaceModifiers(),.methodModifiers()
  • 的NetworkInterface :.getIndex(),.getByIndex()
  • <一个href="http://docs.oracle.com/javase/7/docs/api/java/net/InetSocketAddress.html">InetSocketAddress: .getHostString()
  • 的InetAddress :.getLoopbackAddress()
  • 记录仪:.getGlobal()
  • <一个href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html">ConcurrentLinkedDeque
  • <一个href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/AbstractQueuedSynchronizer.html">AbstractQueuedSynchronizer: .hasQueued predecessors()
  • <一个href="http://docs.oracle.com/javase/7/docs/api/java/util/zip/DeflaterOutputStream.html">DeflaterOutputStream: 3个构造带syncFlush。
  • Deflater :.NO_FLUSH,.SYNC_FLUSH。 FULL_FLUSH,.deflate()有4个参数
  • 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

这基本上是所有的。特别是,NIO 2.0不存在,并且Arrays.asList仍不@SafeVarargs

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

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

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