今天回到家裡用電腦,Mac 在用 Expo 運行 Android 應用程式時,除了跟 Windows 電腦一樣要指定 package 名稱,還需要指定 JAVA_HOME 路徑。這樣 Expo 才知道要用哪個來源的 JVM 進行 Android 應用程式的編譯。
~/development/jim/NotificationProgress | on develop !4 ?2 ------- at 23:21:50
> npx expo run:android --device
✔ Select a device/emulator › 🔌 Pixel_6 (device)
› Using --device Pixel_6
› Building app...
Configuration on demand is an incubating feature.
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration 'classpath'.
> Could not resolve project :gradle-plugin:settings-plugin.
Required by:
unspecified:unspecified:unspecified
> Dependency requires at least JVM runtime version 11. This build uses a Java 8 JVM.
* Try:
> Run this build using a Java 11 or newer JVM.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 740ms
Error: /Users/minjing/development/jim/NotificationProgress/android/gradlew app:assembleDebug -x lint -x test --configure-on-demand --build-cache -PreactNativeDevServerPort=8081 -PreactNativeArchitectures=arm64-v8a,armeabi-v7a exited with non-zero code: 1
Error: /Users/minjing/development/jim/NotificationProgress/android/gradlew app:assembleDebug -x lint -x test --configure-on-demand --build-cache -PreactNativeDevServerPort=8081 -PreactNativeArchitectures=arm64-v8a,armeabi-v7a exited with non-zero code: 1
at ChildProcess.completionListener (/Users/minjing/development/jim/NotificationProgress/node_modules/@expo/spawn-async/build/spawnAsync.js:42:23)
at Object.onceWrapper (node:events:634:26)
at ChildProcess.emit (node:events:519:28)
at maybeClose (node:internal/child_process:1105:16)
at ChildProcess._handle.onexit (node:internal/child_process:305:5)
...
at spawnAsync (/Users/minjing/development/jim/NotificationProgress/node_modules/@expo/spawn-async/build/spawnAsync.js:7:23)
at spawnGradleAsync (/Users/minjing/development/jim/NotificationProgress/node_modules/@expo/cli/build/src/start/platforms/android/gradle.js:96:48)
at assembleAsync (/Users/minjing/development/jim/NotificationProgress/node_modules/@expo/cli/build/src/start/platforms/android/gradle.js:74:18)
at runAndroidAsync (/Users/minjing/development/jim/NotificationProgress/node_modules/@expo/cli/build/src/run/android/runAndroidAsync.js:45:37)
java -version
查看當前的 Java 版本。echo $SHELL
指令查看目前是使用哪一種 shell: Zsh 或是 Bashnano ~/.zshrc
指令開啟 Zsh 設定檔的編輯器,插入下列設定至檔案尾端。export JAVA_HOME=/Library/Java/JavaVirtualMachines/corretto-17.0.12/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
JAVA_HOME 要怎麼找呢?
請看 stackoverflow - How to set JAVA_HOME environment variable on Mac OS X 10.9?
Ctrl+X
、Y
、Enter
存檔。source ~/.zshrc
指令讓你可以立即套用變更而不用重新啟動 terminal。java -version
再一次查看 Java 版本,仍然是較低的版號。sdk list java
查看,原來可用版本有那麼多。================================================================================
Available Java Versions for macOS ARM 64bit
================================================================================
Vendor | Use | Version | Dist | Status | Identifier
--------------------------------------------------------------------------------
Corretto | | 23 | amzn | | 23-amzn
| | 22.0.2 | amzn | | 22.0.2-amzn
| | 21.0.4 | amzn | | 21.0.4-amzn
| | 17.0.12 | amzn | | 17.0.12-amzn
| | 11.0.24 | amzn | | 11.0.24-amzn
| | 8.0.422 | amzn | | 8.0.422-amzn
| >>> | 8.0.372 | amzn | local only | 8.0.372-amzn
sdk install java 17.0.12-amzn
安裝新版本。> sdk install java 17.0.12-amzn
We periodically need to update the local cache. Please run:
$ sdk update
Downloading: java 17.0.12-amzn
In progress...
######################################################################### 100.0%
Repackaging Java 17.0.12-amzn...
Done repackaging...
Cleaning up residual files...
Installing: java 17.0.12-amzn
Done installing!
Do you want java 17.0.12-amzn to be set as default? (Y/n): Y
Setting java 17.0.12-amzn as default.
安裝完會提示要不要設為預設版本,選擇 Y
。
使用 java -version
再一次查看 Java 版本,總算是新版本了。
> java -version
openjdk version "17.0.12" 2024-07-16 LTS
OpenJDK Runtime Environment Corretto-17.0.12.7.1 (build 17.0.12+7-LTS)
OpenJDK 64-Bit Server VM Corretto-17.0.12.7.1 (build 17.0.12+7-LTS, mixed mode, sharing)