Is it possible to use default interface implementation in Android < API 24? - java

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)

Related

[microsoft-graph-api]Developing for Android in Java Desugaring not working for API 24

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
}
}

Support JAVA-8 Jar in Android 5 - React Native

Solution from Android Documentation is below
android {
defaultConfig {
// Required when setting minSdkVersion to 20 or lower
multiDexEnabled true
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}
This is working fine for Native android app, But this configuration is not working for React-Native app and throwing below error while building the app.
A problem occurred evaluating project ':app'.
Could not find method coreLibraryDesugaringEnabled() for arguments [true] on object of type com.android.build.gradle.internal.CompileOptions.
Half knowledge in always dangerous, and it happens here. As per the documentation above solution worked only for gradle version greater than 4. My gradle version was
classpath("com.android.tools.build:gradle:3.5.3")
I changed it to
classpath "com.android.tools.build:gradle:4.0.1"
Now it's working.

Compile Java 8 Android library to be consumed in Java 7

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.

Error importing java.util.stream.Stream in Android Studio

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.

JDK 1.7 String switch statement is not working

[Fixed]
Thanks to #Visil below (accepted answer). I added the recommended code to build.gradle and waited after syncing gradle. The following popped up shortly after that...
Original Question
I am doing some Android programming with Android Studio 0.5.9, and I wanted to have some String switch statements. I set up everything, but the compiler is complaining that it cannot handle them.
String Switch-statements were introduced in JDK 1.7 so I am confused as to why I cannot do this.
Just to prove I am using JDK 1.7, you can check the image below...
...also, my machine has JDK 1.8 installed...
...what's up with this?
[UPDATE]
File >> Other Settings >> Default Settings
File >> Other Settings >> Default Project Structure
Build.gradle
Please check your gradle source compatibilty configuration:
With Android KitKat (buildToolsVersion 19) you can use the diamond
operator, multi-catch, strings in switches, try with resources, etc.
To do this, add the following to your build file:
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Note that you can use minSdkVersion with a value earlier than 19, for
all language features except try with resources. If you want to use
try with resources, you will need to also use a minSdkVersion of 19.
You also need to make sure that Gradle is using version 1.7 or later
of the JDK. (And version 0.6.1 or later of the Android Gradle plugin.)
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1.7

Categories

Resources