---
# System prepended metadata

title: RN For Run at local (Android)

---


# RN For Run at local (Android)

#### Run after first time
1. `yarn install` - Optional, when package.json has changed
2. clean AVD cache: Android Studio AVD simulator -> wipe data ->run
3. clean web source cache: `npm run link & npm start`, until message 'To reload the app press "r"' is displayed, press `Ctrl + C` to terminate program.
4. run app on simulator: `npm run android` **without network**

#### Rebuild project gradle

1. `cd {project-folder}/android && gradlew clean`
2. Android Studio main menu Build > Clean Project
3. `cd ..` `npm run android`

#### Reinstall Android SDK build tools
> 造成原因：內網啟動 RN 到一半卡住，強制停掉程式，斷網或切外網，重啟動皆遇到檔案遺失 gradle build 失敗的問題。
> 
> 問題現象：斷網和外網皆出現 gradle 錯誤訊息 retry 失敗 dl.google.com, jcenter.bintary.com, jitpack.io

##### Sulution 1 - API 28

1. switch external network
2. Android Studio -> SDK Manager -> SDK Tools -> remove build tools -> install build tools
3. invalidate cache and restart
4. Wait for gradle rebuild to complete, `npm run android`

##### Sulution 2 - API 29

1. 刪除專案對應版本的 gradle caches, C:\Users\\{username\}\\.gradle\caches\6.2
2. 檢查並覆蓋檔案 %JAVA_HOME%\jre\lib\security\cacerts

---

### For Run at local (iOS)

1. ( 如果有package.json套件有變動 ) `yarn install`

2. `npm run link`

3. `npm run pod`

4. (選擇性) ios simulator > [device] -> Erase all cotents and setting

5. `npm run ios`

6. on simulator: `command + D` to open simulator menu (debug mode / inspector)

### Build Release APK

#### windows:

1. Keytool (Execute when keystore file does not exist)

Go to the JDK installation path(ex: D:\Program Files\Zulu\zulu-8\bin), and execute

```shell
keytool -genkeypair -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
```
`my-release-key.keystore` and `my-key-alias` can name it yourself.
 
This command generates 'my-release-key.keystore'. Copy this file to 'andorid/app' in project

#### macOS: 

On Mac, if you're not sure where your JDK bin folder is, then perform the following command to find it:

```
$ /usr/libexec/java_home
```

It will output the directory of the JDK, which will look something like this:
```
/Library/Java/JavaVirtualMachines/jdkX.X.X_XXX.jdk/Contents/Home
```

Navigate to that directory by using the command `$ cd /your/jdk/path` and use the keytool command with sudo permission as shown below.

```
$ sudo keytool -genkey -v -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
```

2. Setup parameter 

`android/gradle.properties` add:

```content
MYAPP_UPLOAD_STORE_FILE=my-release-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=xxx
MYAPP_UPLOAD_KEY_PASSWORD=xxx
```

`android/app/build.gradle` add:

```content
signingConfigs {
   release {
      if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
         storeFile file(MYAPP_UPLOAD_STORE_FILE)
         storePassword MYAPP_UPLOAD_STORE_PASSWORD
         keyAlias MYAPP_UPLOAD_KEY_ALIAS
         keyPassword MYAPP_UPLOAD_KEY_PASSWORD
      }
   }
}
buildTypes {
   release {
       ...
       signingConfig signingConfigs.release
   }
}
```

3. build

windows: 
```
cd android && gradlew assembleRelease
```

macOS:
```
cd android && ./gradlew assembleRelease
```

assenbleRelease -> 打包 `apk`出來

路徑 ： android/app/build/outputs/apk/release/app-release.apk

> 新版打包方式 ./gradlew bundleRelease

打包 `aab` 出來 但是直接使用android下載無法安裝，adb也無法直接install (待survey)

路徑 : android/app/build/outputs/bundle/release/app.aab

**打包出來的apk可以直接在電腦上找到該路徑以後，連接實體device(開啟開發者模式) 下指令`adb install XX.apk`即可直接安裝**

請參照 [此](https://reactnative.dev/docs/signed-apk-android)
