問題
Target APIはAPI LEVEL 31(Android12)を選択してビルドするとエラーが出ました。
An exception has occurred in the compiler (1.8.0-adoptopenjdk). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.UnsharedNameTable$NameImpl)
JDK8とAnroid12の互換性がないので、エラーが出ているみたいです。
# install jdk11
$ brew install openjdk@11
$ export $JAVA_HOME=/usr/local/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home
Unity2018.4のembedded jdkがjdk8なので、Use embedded JDKを外してみました。
結果が変わらない。外してもビルド時が使ってます。
AndroidJavaTools:
java : /Applications/Unity/Hub/Editor/2018.4.22f1/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacOS/bin/java
heapsize : 4096
Android PostProcess task "Detecting Android SDK" took 1487.2207 ms
Unity2018.4のJDKをjdk11へ切り替える
# backup
$ sudo cp -r /Applications/Unity/Hub/Editor/2018.4.22f1/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacOS /Applications/Unity/Hub/Editor/2018.4.22f1/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacO-bak
# change to jdk11
$ sudo rm -rf /Applications/Unity/Hub/Editor/2018.4.22f1/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacOS
$ sudo ln -s /usr/local/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home /Applications/Unity/Hub/Editor/2018.4.22f1/PlaybackEngines/AndroidPlayer/Tools/OpenJDK/MacOS
# check version
$ java -version
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment Homebrew (build 11.0.12+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode)
早速ビルドしてみる、まだエラーが出ました
~/Library/Android/sdk/tools/bin/avdmanager list target -c
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 5 more
Android SDK ToolはJDK8でしか動かないです。最新のAndroid SDK Command-line Tools
へ切り替え
Android SDK Toolの切り替え
Android StudioでAndroid SDK Command-line Toolsをインストール
Android Studio -> Preferences -> Android SDK -> SDK Tools -> Android SDK Command-line Tools
$ ls ~/Library/Android/sdk/cmdline-tools/latest
NOTICE.txt bin lib package.xml source.properties
# backup
$ cp -r ~/Library/Android/sdk/tools/bin ~/Library/Android/sdk/tools/bin-bak
$ cp -r ~/Library/Android/sdk/tools/lib ~/Library/Android/sdk/tools/lib-bak
# copy
$ cp -r ~/Library/Android/sdk/cmdline-tools/latest/lib/* ~/Library/Android/sdk/tools/lib/
$ cp -r ~/Library/Android/sdk/cmdline-tools/latest/bin/* ~/Library/Android/sdk/tools/bin/
# sdk toolのエラーがなくなった
$ ~/Library/Android/sdk/tools/bin/avdmanager list target -c
android-30==============================] 100% Fetch remote repository...
android-31
再度ビルドします。