iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0

今天回到家裡用電腦,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)
  1. 在 terminal 使用 java -version 查看當前的 Java 版本。
  2. echo $SHELL 指令查看目前是使用哪一種 shell: Zsh 或是 Bash
  3. nano ~/.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?

  1. 依序按下 Ctrl+XYEnter 存檔。
  2. source ~/.zshrc 指令讓你可以立即套用變更而不用重新啟動 terminal。
  3. 但是,使用 java -version 再一次查看 Java 版本,仍然是較低的版號。
  4. 這時我們要用 SDKMAN 來管理這些眾多的 Java 版本。
  5. 使用 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
  1. 使用 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.
  1. 安裝完會提示要不要設為預設版本,選擇 Y

  2. 使用 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)

上一篇
[Day 10] Windows - 選擇裝置來運行 React Expo 應用程式
下一篇
[Day 12] Mac 電腦解決 adb 無法安裝問題
系列文
跨平台協同:在 React Native 和 Kotlin 應用中實現無縫交互 -以 Notification 為例30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言