まずはreact-nativeを最小構成で動かすまでをメモ
$ npm i -g react-native-cli
$ react-native init hello # helloはプロジェクト名
. . .
Done in 33.80s.
Run instructions for iOS:
• cd /home/oreore/dev/test/hello && react-native run-ios
- or -
• Open hello/ios/hello.xcodeproj in Xcode or run "xed -b ios"
• Hit the Run button
Run instructions for Android:
• Have an Android emulator running (quickest way to get started), or a device connected.
• cd /home/oreore/dev/test/hello && react-native run-android
react-native run-androidを実行しろとのこと
$ react-native run-android
. . .
ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
JAVA_HOMEとjavaコマンドがないらしい。AndroidStudioインストール時にopenjdkが入っていたはずなので、せっかくなのでそれを利用する。
javaのpathはAndroidStudioの、File > Project Structureから確認可能。
パスとか入れる。
$ vim .bashrc
JAVA_HOME=/usr/local/android-studio/jre
export PATH="$PATH:/usr/local/android-studio/jre/bin"
$ source .bashrc
java確認
$ java -version
openjdk version "1.8.0_202-release"
いい感じなので、再度run-android実行
$ react-native run-android
. . .
error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> 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 '/home/oreore/dev/test/hi/android/local.properties'.
ANDROID_SDK_ROOTを入れてる
$ vim .bashrc
export ANDROID_SDK_ROOT=/home/oreore/Android/Sdk
$ source .bashrc
run-android再実行
$ react-native run-android
. . .
BUILD SUCCESSFUL in 33s
28 actionable tasks: 28 executed
info Connecting to the development server..
成功してエミュレーターが立ち上がった。
画面は黒いが初期状態なのでこれで成功しているはず。次はreduxとか入れてhello worldしよう。
追記:
黒い画面は正しく起動できていなかった。
理由は不明だがandroid/app/src/main/assets
ディレクトリがなく、indexファイル的なのもなかった。ので、作成する
$ mkdir android/app/src/main/assets
$ react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
$ react-native start
追記2:
start&run-androidしてもエミュレータが動かなかったので、ANDROID_HOME環境変数を入れた。再度initから新規アプリを作成した場合、assetsがなくても起動したので、追記1はなくてもいいのかも。。
android開発環境作りでは、環境変数がすごい大事なことがわかった。
$vim .bashrc
export ANDROID_HOME=/home/oreore/Android/Sdk
メモ:
error SHA-1 for file
はnpm i -g react-native-cli
で直った