Ubuntu20.04安裝React-Native及編譯APK

Ubuntu20.04安裝React-Native

install react-native development tools in ubuntu

1. 安裝Node.js和Yarn

請參考我的前一篇

2. 安裝React-Native的CLI工具

sudo yarn global add react-native-cli

測試一下是否安裝完整

$ react-native -v

react-native-cli: 2.0.1
react-native: n/a - not inside a React Native project directory

如果你安裝完沒有辦法打react-native看版本

3. 安裝JDK

sudo apt install openjdk-8-jdk

測試一下是否是安裝的jdk

$ java -version

openjdk version "1.8.0_292"

OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~20.04-b10)

OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

$ javac -version

javac 1.8.0_292

如果你是只有裝一套JDK的系統就不需要調整PATH跟JAVA_HOME

4. 設定Android SDK路徑

以我的Android SDK目錄放在/mnt/hdd2/Android/來說,我會在.bashrc下設定

ANDROID_SDK_ROOT=/mnt/hdd2/Android/Sdk

ANDROID_HOME=/mnt/hdd2/Android/Sdk

PATH=$PATH:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools

5. 編譯你的APP

從cmd編譯

先進入你的Project下的android目錄編譯你的APP

  1. 編譯Debug版APP
    ./gradlew assembleDebug
    生成的檔案路徑預設是android/app/build/outputs/apk/debug
  2. 編譯Release版APP
    ./gradlew assembleRelease
    生成的檔案路徑預設是android/app/build/outputs/apk/release

6. Trobuleshooting

6.1 SDK location not found

如遭遇以下錯誤

FAILURE: Build failed with an exception.

* What went wrong:

Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.

> SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at '/mnt/hdd2/..../android/local.properties'.

請依照指示建立一個檔案android/local.properties,以下用簡單的command生成該檔案
echo "sdk.dir=${ANDROID_SDK_ROOT}" >> android/local.properties

6.2 No toolchains found in the NDK toolchains

如遭遇以下錯誤
> Task :app:stripStageDebugSymbols FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:stripStageDebugSymbols'.
> No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi

根據上一個問題的解法,我們可以在android/local.properties內加入ndk.dir
echo "ndk.dir=${ANDROID_SDK_ROOT}/ndk-bundle" >> android/local.properties

留言