11

I'm trying to migrate some Java library from 'normal' JVM to android and stuck with some Java11 APIs used in the code.

The first thing I already got - Java11 language features seems to work only with Canary build of Android Studio, see answer here

Now I need to get understanding about which APIs can be really used. Here are two use-cases which do not work for me and I can't get if I'm doing something wrong or it never should work:

  1. List.copyOf() - introduced in Java11, method copyOf is not available on android. Methods 'List.of()', introduced with Java 9, work OK.

  2. class java.lang.invoke.LambdaMetafactory - introduced with Java 1.8 - to be used for programmatic creation of lambdas for usage instead for reflection, is not visible on Android.

I see both of them in sources of desugar_jdk_libs here:

So - the question is: how can I identify if some Java API is supposed to be available in 'desugared' android build or no? What really can be expected from 'desugaring'?

Steps to reproduce:

  1. Using Android Studio Canary generate a dummy "Basic Activity" project
  2. Make sure following is provided in build.gradle
android {
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
}
dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
  1. Add following lines somewhere in code
        List<Integer> ints1 = List.of(1, 2, 3);
        Supplier<List<Object>> listSupplier = () -> new ArrayList<>();
        List<Object> alist = listSupplier.get();

        List<Integer> ints2 = List.copyOf(ints1);
        LambdaMetafactory.metafactory(null,null,null,null,null,null);

Last 2 lines fail to compile for me.

PS: final application is supposed to work on Android 10+.

Xtra Coder
  • 3,389
  • 4
  • 40
  • 59
  • 4
    I think it's not possible yet. Mainly, because the work is in progress. In the future, we will have a list of supported APIs and lint rules (like for java 8 https://developer.android.com/studio/write/java8-support-table) – sdex Mar 10 '21 at 06:31
  • My question is, do we still need `coreLibraryDesugaringEnabled` when we have the Java 11 support now?! – Dr.jacky Jan 28 '22 at 11:17
  • Also my question: do we still need coreLibraryDesugaringEnabled when we have the Java 11 support now?! Can anyone clarify? – LXJ Jul 02 '22 at 11:21
  • I assume it's still required, as `desugar_jdk_libs` is still being updated, with the 2.0.0 update being released fairly recently. The Java 11 support added in AGP 7.0.0 was only for language features like private interface methods, var keyword etc. For actual classes which were only added in later Java versions, we still need desugaring. The same was true for Java 8 as you get language features like lambdas even without desugaring. – Adam Burley Jan 10 '23 at 13:36

2 Answers2

3

Contrary to the other answer, desugaring is totally possible.

The dependency to add is

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}

You can find more information at the official Android Java 8 desugaring documentation.

Alex Facciorusso
  • 2,290
  • 3
  • 21
  • 34
  • To add some information, the library seems to have been updated to `1.2.0` now. Another thing you should consider: desugaring will add considerably to the build time. – Alex Facciorusso Jul 13 '22 at 23:00
  • Also possibly relevant: changelog for desugaring https://github.com/google/desugar_jdk_libs/blob/master/CHANGELOG.md You can see that 1.2.0 is required for Android 11 desugaring, and 2.0.0 enables java.nio desugaring with an additional option – Adam Burley Jan 10 '23 at 12:39
1

Desugaring lib is considered unofficial. We can't expect an exact answer. We get the feature when it is ready. Now List.copyOf() method now working with the latest Gradle version.

About the LambdaMetafactory class, It is not included in Android Javadoc. This means we assume we don't have LambdaMetafactory at all. Google stripped down some java API for being lightweight.

In general, We should check android Javadoc first. If android Javadoc has no mention about some API. We can be sure we won't get that feature anytime soon.

hurelhuyag
  • 1,691
  • 1
  • 15
  • 20