Java 7 language features with Android
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.
Will my app run on android 4.0 if I use JDK 8
The app will definitely run with JDK 8 run it.
Android Studio 3.0 and later supports all Java 7 language features and a subset of Java 8 language features that vary by platform version.
Android Studio provides built-in support for using certain Java 8 language features and third-party libraries that use them.
Note:
When developing apps for Android, using Java 8 language features is
optional. You can keep your project's source and target compatibility
values set to Java 7, but you still need to compile using JDK 8.
And about your minSDK comment "whats your preferred minSDK? if you start with API 19 you have a rate of 95%"->
minSdk is required to set the minimum api level run environment(android OS version) to the application. If you choose minSdk the minimum possible you can target max number of devices.
Which Android versions run which Java versions?
and so, apparently, Android 4.0 does not support Java 7.
By your definition, Android does not support any version of Java. The java
and javax
classes in the Android SDK do not exactly match any version of Java, whether a numerical version (e.g., 6, 7, 8) or whatever you want to consider Java SE/EE/ME to be.
Is there an overview which clearly shows which Android versions come with which Java version
In terms of language features (e.g., lambda expressions), quoting the documentation:
Android Studio 3.0 and later supports all Java 7 language features and a subset of Java 8 language features that vary by platform version
Here, "Android Studio" is really referring to the build tools that compile your source code and create the Dalvik bytecode. Current tools can support all Java 7 and a few Java 8 features on all versions of Android. Additional Java 8 features are only available on API Level 24+, usually because they rely upon certain classes that were only added to the Android SDK at that point.
But your concern seems to be with classes and methods, in which case there is no simple mapping of any Java version to any Android version.
Moreover, you are using reflection to hack into framework classes, which means your results will not only vary by Android version but by device model, as device manufacturers can and do change the implementation of framework classes.
JDK 7 and Android IDE
Yes, you can use Java 1.7 for the android development.
Starting from build tools 19, Android has full support for Java 1.7. So you can set 1.7 as source and target for compilation. Support for this is from Android studio 0.3.2. Android kitkat has full support of JAVA 1.7 API, but most of language features from 1.7, can be used on the older android versions too.
Edit:
You can also mantain the jdk 7 and configure the Android application to use Jdk 6 during compilation.
Related Topics
Classcastexception When Casting to the Same Class
How to Schedule a Periodic Task in Java
How to Validate an Xml File Against an Xsd File
How to Connect Remote MySQL Database in Android Using Jdbc
Wait Until Firestore Data Is Retrieved to Launch an Activity
Difference Between Using Java.Library.Path and Ld_Library_Path
Execute External Program Through Terminal in Java
Kafka - Broker: Group Coordinator Not Available
Linux Start-Up Script for Java Application
Variable Column Names Using Prepared Statements
How to Pass an Array as Arguments to a Method With Variable Arguments in Java
What Is This Date Format? 2011-08-12T20:17:46.384Z
Iso 8601 String to Date/Time Object in Android
How to Use Intent.Flag_Activity_Clear_Top to Clear the Activity Stack
How to Deserialise a Subclass in Firebase Using Getvalue(Subclass.Class)
Setting/Changing the Ctime or "Change Time" Attribute on a File