Querying the mapped value of map before task compileDebugJavaWithJavac has completed is not supported

I exported Unity (2020.3) project to Android project and then imported it into Android Studio, but following error occurred when building using gradle.

Execution failed for task ':unityLibrary:unity-android-resources:compileDebugJavaWithJavac'.

Failed to calculate the value of task ':unityLibrary:unity-android-resources:compileDebugJavaWithJavac' property 'options.generatedSourceOutputDirectory'.
Querying the mapped value of map(java.io.File property(org.gradle.api.file.Directory, fixed(class org.gradle.api.internal.file.DefaultFilePropertyFactory$FixedDirectory, /home/gbstack/Unity/Projects/ads-test/android-export/unityLibrary/unity-android-resources/build/generated/ap_generated_sources/debug/out)) org.gradle.api.internal.file.DefaultFilePropertyFactory$ToFileTransformer@481ca56c) before task ':unityLibrary:unity-android-resources:compileDebugJavaWithJavac' has completed is not supported

The exception detail is following:

Since it's saying that some operation is not supported, so it may be a compatibility problem because of gradle version.

Solution

Changing gradle from 7.1 to version to 6.x will fix this problem. I tried both gradle 6.1 and 6.9, and both works