Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are using <strong>Android Studio</strong>, the Java 7 <em>language</em> should be enabled automatically without any patches. Try-with-resource requires API Level 19+, and NIO 2.0 stuff are missing. </p> <p>If you can't use Java 7 features, see <a href="https://stackoverflow.com/a/17309063/224671">@Nuno</a>'s answer on how to edit your <code>build.gradle</code>.</p> <p>The following is for historical interest only.</p> <hr> <p>A small part of Java 7 can certainly be used with Android (note: I have only tested on 4.1). </p> <p>First of all, you could not use Eclipse's ADT because <a href="https://android.googlesource.com/platform/sdk/+/master/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtConstants.java" rel="nofollow noreferrer">it is hard-coded</a> 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. </p> <p>But you don't need to use Eclipse. For instance, <a href="http://tools.android.com/recent/androidstudio032released" rel="nofollow noreferrer"><strong>Android Studio 0.3.2</strong></a>, <a href="http://www.jetbrains.com/idea/" rel="nofollow noreferrer"><strong>IntelliJ IDEA CE</strong></a> and other javac-based IDEs supports compiling to Android <i>and</i> you could set the compliance even up to Java 8 with:</p> <ul> <li>File → Project Structure → Modules → (pick the module at the 2nd pane) → Language level → (choose "7.0 - Diamonds, ARM, multi-catch, etc.")</li> </ul> <p><img src="https://i.stack.imgur.com/1z7iH.png" alt="Enabling Java 7 on IntelliJ"></p> <p>This only allows Java 7 <strong>language features</strong>, 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:</p> <ul> <li>Diamond operator (<code>&lt;&gt;</code>)</li> <li>String switch</li> <li>Multiple-catch (<code>catch (Exc1 | Exc2 e)</code>)</li> <li>Underscore in number literals (<code>1_234_567</code>)</li> <li>Binary literals (<code>0b1110111</code>)</li> </ul> <p>And these features cannot be used <em>yet</em>:</p> <ul> <li>The <code>try</code>-with-resources statement — because it requires the non-existing interface "java.lang.AutoCloseable" (this can be used publicly in 4.4+)</li> <li>The @SafeVarargs annotation — because "java.lang.SafeVarargs" does not exist</li> </ul> <p>... "yet" :) It turns out that, although Android's library is targeting for 1.6, the Android source does contain interfaces like <a href="https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/lang/AutoCloseable.java" rel="nofollow noreferrer">AutoCloseable</a> and traditional interfaces like <a href="https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/io/Closeable.java" rel="nofollow noreferrer">Closeable</a> 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 <code>@hide</code> tag, which caused the "android.jar" not to include them.</p> <p>There is already as existing question<strong> <a href="https://stackoverflow.com/questions/7888191/how-do-i-build-the-android-sdk-with-hidden-and-internal-apis-available">How do I build the Android SDK with hidden and internal APIs available?</a> </strong>on how to get those methods back. You just need to <em>replace</em> 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.)</p> <p>In additional to AutoCloseable, (only) the following Java 7 <strong>library features</strong> are also revealed:</p> <ul> <li>Exception chaining constructors in ConcurrentModificationException, LinkageError and AssertionError</li> <li>The static .compare() methods for primitives: Boolean.compare(), Byte.compare(), Short.compare(), Character.compare(), Integer.compare(), Long.compare().</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html" rel="nofollow noreferrer">Currency</a>: .getAvailableCurrencies(), .getDisplayName() (but <em>without</em> .getNumericCode())</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html" rel="nofollow noreferrer">BitSet</a>: .previousSetBit(), .previousClearBit(), .valueOf(), .toLongArray(), .toByteArray()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html" rel="nofollow noreferrer">Collections</a>: .emptyEnumeration(), .emptyIterator(), .emptyListIterator()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html" rel="nofollow noreferrer">AutoCloseable</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html" rel="nofollow noreferrer">Throwable</a>: .addSuppressed(), .getSuppressed(), and the 4-argument constructor</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html" rel="nofollow noreferrer">Character</a>: .compare(), .isSurrogate(), .getName(), .highSurrogate(), .lowSurrogate(), .isBmpCodePoint() (but <em>without</em> .isAlphabetic() and .isIdeographic())</li> <li>System: .lineSeparator() (undocumented?)</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Modifier.html" rel="nofollow noreferrer">java.lang.reflect.Modifier</a>: .classModifiers(), .constructorModifiers(), .fieldModifiers(), .interfaceModifiers(), .methodModifiers()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/net/NetworkInterface.html" rel="nofollow noreferrer">NetworkInterface</a>: .getIndex(), .getByIndex()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/net/InetSocketAddress.html" rel="nofollow noreferrer">InetSocketAddress</a>: .getHostString()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html" rel="nofollow noreferrer">InetAddress</a>: .getLoopbackAddress()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html" rel="nofollow noreferrer">Logger</a>: .getGlobal()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html" rel="nofollow noreferrer">ConcurrentLinkedDeque</a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/AbstractQueuedSynchronizer.html" rel="nofollow noreferrer">AbstractQueuedSynchronizer</a>: .hasQueuedPredecessors()</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/zip/DeflaterOutputStream.html" rel="nofollow noreferrer">DeflaterOutputStream</a>: the 3 constructors with "syncFlush".</li> <li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html" rel="nofollow noreferrer">Deflater</a>: .NO_FLUSH, .SYNC_FLUSH, .FULL_FLUSH, .deflate() with 4 arguments</li> </ul> <p>That's basically all. In particular, NIO 2.0 does not exist, and Arrays.asList is still not @SafeVarargs.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload