I am using Java JDK version 1.8.0_60-b27 for both Netbeans and Android Studio. I could not import java.util.function.Consumer and java.util.stream.Stream packages in my Android Studio project. But I can import the same in my Netbeans Project. What is the reason?
In Google I/O 2016, they announced that Android now supports some features of Java 8, but not all the features.
Features like :
- Default and static interface methods
- Lambda expressions (also available on API level 23 and lower)
- Repeatable annotations
- Method References (also available on API level 23 and lower)
- Type Annotations (also available on API level 23 and lower)
Additionally, the following Java 8 language APIs are also available:
Reflection and language-related APIs:
java.lang.FunctionalInterface java.lang.annotation.Repeatable java.lang.reflect.Method.isDefault()
and Reflection APIs associated with repeatable annotations, such as AnnotatedElement.getAnnotationsByType(Class) Utility APIs: java.util.function java.util.stream
All what you should do is to use the new jack compiler, to do this you have just to add this to your gradle file build.gradle "module:app":
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Refrences
https://developer.android.com/guide/platform/j8-jack.html https://www.youtube.com/watch?v=B08iLAtS3AQ&feature=youtu.be&t=28m14s
Android only supports Java 7. Because of this you cannot use the Java Stream API introduced with Java 8. There exist some backports for this features, like
https://github.com/aNNiMON/Lightweight-Stream-API for Streams
or
https://github.com/orfjackal/retrolambda for Lambda-Expressions.
Related
I am trying to update the microsoft-graph-api to 3.3.0 and everything works in the Emulator, but the real device is an Android 7.0 (API 24) and I get:
com.android.tools.r8.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
Execution failed for task ':app:mergeExtDexDebug'.
Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
Failed to transform azure-core-1.15.0.jar (com.azure:azure-core:1.15.0) to match attributes {artifactType=android-dex, dexing-enable-desugaring=true, dexing-incremental-transform=false, dexing-is-debuggable=true, dexing-min-sdk=24, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
> Execution failed for DexingNoClasspathTransform: /Users/pmartinez/.gradle/caches/transforms-2/files-2.1/8cfc8780442b4f1e0ebead042ade0764/jetified-azure-core-1.15.0.jar.
> Error while dexing.
The Readme from 3.3.0 says that developers trying to use it in <26 API could make it work using desugarizing, this is in my Gradle.app
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
dependencies {
// To get features from Newer APIs
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
implementation "com.microsoft.identity.client:msal:2.+"
implementation 'com.microsoft.graph:microsoft-graph:3.3.0'
// Uncomment the line below if you are building an android application
implementation 'com.google.guava:guava:29.0-android'
}
Any ideas on what I could do to keep the graph 3 and/or whats the microsoft-graph 2 minimum Android API?
You can try minifying the apk. It would remove unnecessary code and that method call may be unnecessary, even though graph api includes it. Here is an example:
buildTypes
{
release
{
minifyEnabled true
}
debug
{
minifyEnabled true
}
}
according to MoPub Docs I should add this to my gradle file, but my question is what do I pick for sourceCompatibility and target compatibility, it seems like target Compatibility has to be set to 1_8 or else it will not build. But what is the difference if I leave sourceCompatibility to 1_7 vs 1_8 is there a way to know which one my project uses, sourceCompatibility builds the app fine with either one set I would just like to know what is the difference between the 2 and if I actually do need to set it to 1 of them instead of the other also I've read that gradle uses the java version which is on our machine and mine is 1.8.0 so why do I have to explicitly declare VERSION_1_8 ?
on java docs it states "Generates class files that target a specified release of the virtual machine. Class files will run on the specified target and on later releases, but not on earlier releases of the JVM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), 1.7 (also 7), and 1.8 (also 8)." but java 1.7&1.8 not available on android 4.1 and it still runs on android 4.1 when I set java version to 1.7 or 1.8 how is this possible ?
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_8
} }
Let's say I am making an Android library (to be distributed as a .aar) and I want to use Java 8 language features in my internal library code. Basic stuff like lambdas, etc.
In my experience, if I then assemble and publish this library any developer that relies up on it will get errors in build unless they add this to their build.gradle:
android {
// ...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Is there any way to compile my library so that consumers don't have to do this? Assume that the public API surface of the library does not contain any Java 8 specific features.
Android Studio 2.3.3, Java 8
I create Android app for Android 4.0+
In my app/build.gradle:
...
minSdkVersion 15
targetSdkVersion 26
I want to use default interface implementation (from Java 8).
So I create the tnext class:
public interface DefaultCallback {
public default void onResponse(Call<T> var1, Response<T> var2) {
}
}
but I get compile error:
Default method required API level 24 (current min is 15)
So the question is:
Can I use deafult interface implementation on Android < API 24?
Depending on what your minSdk version is you may need to add the following to your app's or module's build.gradle file:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
See https://developer.android.com/studio/write/java8-support.html#supported_features for more details
No.
Default methods require API level 24.
From https://developer.android.com/guide/platform/j8-jack.html#supported-features:
Android does not support all Java 8 language features. However, the
following features are available when developing apps targeting
Android 7.0 (API level 24):
Default and static interface methods
Lambda expressions (also
available on API level 23 and lower)
Repeatable annotations
Method References (also available on API level 23 and lower)
Type Annotations
(also available on API level 23 and lower)
I am new to using Realm database for Android.
I modified my gradle files to include the Jack toolchain so that I could use Java 8 language features. I also modified the gradle files to install the Realm plugin. When I synced the project gradle files, I received the following error: Error:Could not find property 'options' on task ':app:compileDebugJavaWithJack'. The two modifications work fine on their own, but for some reason I cannot have both at the same time.
I would very much appreciate help on this matter.
It is not possible to use Jack compiler with Realm at the moment, because Jack does not support bytecode manipulation (Javassist / Transform API).
In order to use lambdas, it's easier for you to use Retrolambda instead for the time being.
buildscript {
//...
dependencies {
classpath "io.realm:realm-gradle-plugin:1.1.0"
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
}
And
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Unfortunately Jack compiler and Realm can't play together right now. Please follow that topic. That one is also useful.