[Capacitor] 系統安裝 Java 21 時,無法編譯 Android app?
今天想用 Capacitor 編譯 Android 版本的 app,
結果出現如下的錯誤,老實說看不太懂:
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':capacitor-android:compileDebugJavaWithJavac'. > Could not resolve all files for configuration ':capacitor-android:androidJdkImage'. > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}. > Execution failed for JdkImageTransform: /Users/testuser/Library/Android/sdk/platforms/android-33/core-for-system-modules.jar. > Error while executing process /Users/testuser/.sdkman/candidates/java/21.0.2-oracle/bin/jlink with arguments {--module-path /Users/testuser/.gradle/caches/transforms-3/d20b91a7c402245d65bf0047643c0671/transformed/output/temp/jmod --add-modules java.base --output /Users/testuser/.gradle/caches/transforms-3/d20b91a7c402245d65bf0047643c0671/transformed/output/jdkImage --disable-plugin system-modules}
跟 ChatGPT 問了一下,感覺可能是 Android 用到的 Java 沒有支援到 21 那麼新…
上網查了一下,目前 Android 支援的 Java 版本如下,
看起來真的沒有支援到 Java 21…
- Android 14 (API 34) = Java 17
- Android 13 (API 33) = Java 11
- Android 12 (API 32) = Java 11
因為我有用 SDKMAN! 來管理不同的 Java 版本,
因此先看一下目前的 Java 版本,果然是 21.0.2:
$ sdk list java ================================================================================ Available Java Versions for macOS ARM 64bit ================================================================================ Vendor | Use | Version | Dist | Status | Identifier -------------------------------------------------------------------------------- Oracle | | 23.0.1 | oracle | | 23.0.1-oracle | | 23 | oracle | | 23-oracle | | 22.0.2 | oracle | | 22.0.2-oracle | | 21.0.5 | oracle | | 21.0.5-oracle | | 21.0.4 | oracle | | 21.0.4-oracle | >>> | 21.0.2 | oracle | local only | 21.0.2-oracle | | 17.0.12 | oracle | | 17.0.12-oracle
使用 SDKMAN! 安裝 Oracle Java 17.0.12 版本,
安裝後它問是否要設成預設值 (default),回答 yes:
sdk install java 17.0.12-oracle
這時再重新跑 Capacitor 編譯 Android 程式,看來就沒問題了:
$ npx cap run android ✔ Copying web assets from www to android/app/src/main/assets/public in 142.36ms ✔ Creating capacitor.config.json in android/app/src/main/assets in 10.53ms [info] Inlining sourcemaps ✔ copy android in 163.82ms ✔ Updating Android plugins in 459.75μs ✔ update android in 27.45ms ✔ Running Gradle build in 70.09s ✔ Deploying app-debug.apk to Pixel_7_Pro_API_35 in 21.00s
(本頁面已被瀏覽過 27 次)