Android AGP 4.2.0 + Java 11 compilation fails

Viewed 1378

with the release of Android Studio 4.2 and AGP 4.2.0 we now have an ability to use Java 11 in android projects. I did update everything to point to Java 11, but compilation fails. If I put back JavaVersion.VERSION_1_8, it works.

All the compilation errors are in databinding(? or viewbinding) generated classes and complaining that android classes or packages could not be found. And I tried lot of things already, nothing works, JAVA_HOME points to java11, JDK in studio points to java11 and still:

some_module:compileDebugJavaWithJavac FAILED

../DataBinderMapperImpl.java:3: error: package android.util does not exist
import android.util.SparseArray;
                   ^
../DataBinderMapperImpl.java:4: error: package android.util does not exist
import android.util.SparseIntArray;

.. error: package android.view does not exist

So, what I have

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_11
            targetCompatibility JavaVersion.VERSION_11
        }
        kotlinOptions {
            jvmTarget = "11"
        }

./gradlew -version returns:

------------------------------------------------------------
Gradle 6.8.3
------------------------------------------------------------

Build time:   2021-02-22 16:13:28 UTC
Revision:     9e26b4a9ebb910eaa1b8da8ff8575e514bc61c78

Kotlin:       1.4.20
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.2 (Oracle Corporation 11.0.2+9)
OS:           Mac OS X 10.15.7 x86_64

java -version returns

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

javac -version returns

javac 11.0.2

Will really appreciate any help, thanks!

1 Answers

You should update to Android Studio Arctic Fox | 2020.3.1 to use Java 11, also set your compile options to

sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11

if you are using Kotlin, also

kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() }
Related