Browse Source

android: run the React packager when running from AS

When running the app from Android Studio the React packager is not automatically
started. In vanilla RN projects this is done by the "react-native run-android"
command, but often times it is desired to run from Android Studio.

This fixes that by starting the packager from Gradle.
master
Saúl Ibarra Corretgé 6 years ago
parent
commit
e32336b96f
2 changed files with 59 additions and 0 deletions
  1. 35
    0
      android/app/build.gradle
  2. 24
    0
      android/scripts/run-packager.sh

+ 35
- 0
android/app/build.gradle View File

@@ -116,6 +116,41 @@ gradle.projectsEvaluated {
116 116
             }
117 117
         }
118 118
     }
119
+
120
+    // Run React packager
121
+    android.applicationVariants.all { variant ->
122
+        def targetName = variant.name.capitalize()
123
+
124
+        def currentRunPackagerTask = tasks.create(
125
+                name: "run${targetName}ReactPackager",
126
+                type: Exec) {
127
+            group = "react"
128
+            description = "Run the React packager."
129
+
130
+            doFirst {
131
+                println "Starting the React packager..."
132
+
133
+                def androidRoot = file("${projectDir}/../")
134
+
135
+                // Set up the call to the script
136
+                workingDir androidRoot
137
+
138
+                // Run the packager
139
+                commandLine("scripts/run-packager.sh")
140
+            }
141
+
142
+            // Set up dev mode
143
+            def devEnabled = !targetName.toLowerCase().contains("release")
144
+
145
+            // Only enable for dev builds
146
+            enabled devEnabled
147
+        }
148
+
149
+        def packageTask = variant.packageApplicationProvider.get()
150
+
151
+        packageTask.dependsOn(currentRunPackagerTask)
152
+    }
153
+
119 154
 }
120 155
 
121 156
 if (googleServicesEnabled) {

+ 24
- 0
android/scripts/run-packager.sh View File

@@ -0,0 +1,24 @@
1
+#!/bin/bash
2
+
3
+# This script is executed bt Gradle to start the React packager for Debug
4
+# targets.
5
+
6
+THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
7
+
8
+export RCT_METRO_PORT="${RCT_METRO_PORT:=8081}"
9
+echo "export RCT_METRO_PORT=${RCT_METRO_PORT}" > "${THIS_DIR}/../../node_modules/react-native/scripts/.packager.env"
10
+
11
+if nc -w 5 -z localhost ${RCT_METRO_PORT} ; then
12
+  if ! curl -s "http://localhost:${RCT_METRO_PORT}/status" | grep -q "packager-status:running" ; then
13
+    echo "Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly"
14
+    exit 2
15
+  fi
16
+else
17
+    CMD="${THIS_DIR}/../../node_modules/react-native/scripts/launchPackager.command"
18
+    if [[ `uname` == "Darwin"  ]]; then
19
+        open -g "${CMD}" || echo "Can't start packager automatically"
20
+    else
21
+        xdg-open "${CMD}" || echo "Can't start packager automatically"
22
+    fi
23
+fi
24
+

Loading…
Cancel
Save