Skip to content

Commit 2a2d3c6

Browse files
SandroMachadoFacebook Github Bot
authored andcommitted
Add the configuration option to the run-android command
Summary: Currently, to generate a `Release` build in `Android` it is required to get into the `android` directory and run the `react native bundle`with a lot of options and after that run the `gradle` to assemble or install the application with the build type `Release`. This PR improves the process adding that feature to the `React Native CLI`. To generate a release build is only required to use the parameter `--configuration` with the value `Release`. **Examples** To generate a release build: ```sh react-native run-android --configuration release ``` To generate a release build for the product flavors staging: ```sh react-native run-android --configuration release --flavor staging ``` To generate a debug build: ```sh react-native run-android ``` To generate a debug build for the product flavors staging: ```sh react-native run-android --flavor staging ``` This PR also removes the option `--install-debug` from the `react-native run-android` because that is always the default value, Closes #10867 Differential Revision: D4167203 Pulled By: cpojer fbshipit-source-id: c5ac07f81feeeea00ee0e8b059b46ef0d258a1a6
1 parent 39cf29c commit 2a2d3c6

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

docs/RunningOnDevice.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ $ react-native run-android
148148

149149
> If you get a "bridge configuration isn't available" error, see [Using adb reverse](#using-adb-reverse).
150150
151+
> Hint
152+
>
153+
> You can also use the `React Native CLI` to generate and run a `Release` build (e.g. `react-native run-android --configuration Release`).
154+
151155
<block class="mac windows linux android ios" />
152156

153157
## Connecting to the development server

local-cli/runAndroid/runAndroid.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,9 @@ function tryRunAdbReverse(device) {
7777

7878
// Builds the app and runs it on a connected emulator / device.
7979
function buildAndRun(args) {
80-
process.chdir(path.join(args.root, 'android'));
8180
try {
8281
adb.getDevices().map((device) => tryRunAdbReverse(device));
8382

84-
const cmd = process.platform.startsWith('win')
85-
? 'gradlew.bat'
86-
: './gradlew';
87-
8883
const gradleArgs = [];
8984
if (args.variant) {
9085
gradleArgs.push('install' +
@@ -98,13 +93,33 @@ function buildAndRun(args) {
9893
args.flavor[0].toUpperCase() + args.flavor.slice(1)
9994
);
10095
} else {
101-
gradleArgs.push('installDebug');
96+
gradleArgs.push('install');
10297
}
10398

104-
if (args.installDebug) {
105-
gradleArgs.push(args.installDebug);
99+
// Append the build type to the current gradle install configuration. By default it will generate `installDebug`.
100+
gradleArgs[0] = gradleArgs[0] + args.configuration[0].toUpperCase() + args.configuration.slice(1);
101+
102+
// Get the Android project directory.
103+
const androidProjectDir = path.join(args.root, 'android');
104+
105+
if (args.configuration.toUpperCase() === 'RELEASE') {
106+
console.log(chalk.bold(
107+
`Generating the bundle for the release build...`
108+
));
109+
110+
child_process.execSync(`react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output ${androidProjectDir}/app/src/main/assets/index.android.bundle --assets-dest ${androidProjectDir}/app/src/main/res/`, {
111+
stdio: [process.stdin, process.stdout, process.stderr]
112+
});
106113
}
107114

115+
// Change to the Android directory.
116+
process.chdir(androidProjectDir);
117+
118+
// Get the gradle binary for the current platform.
119+
const cmd = process.platform.startsWith('win')
120+
? 'gradlew.bat'
121+
: './gradlew';
122+
108123
console.log(chalk.bold(
109124
`Building and installing the app on the device (cd android && ${cmd} ${gradleArgs.join(' ')}...`
110125
));
@@ -116,9 +131,8 @@ function buildAndRun(args) {
116131
console.log(chalk.red(
117132
'Could not install the app on the device, read the error above for details.\n' +
118133
'Make sure you have an Android emulator running or a device connected and have\n' +
119-
'set up your Android development environment.\n' +
120-
'Go to https://facebook.github.io/react-native/docs/getting-started.html\n' +
121-
'and check the Android tab for setup instructions.'
134+
'set up your Android development environment:\n' +
135+
'https://facebook.github.io/react-native/docs/android-setup.html'
122136
));
123137
// stderr is automatically piped from the gradle process, so the user
124138
// should see the error already, there is no need to do
@@ -206,14 +220,16 @@ module.exports = {
206220
description: 'builds your app and starts it on a connected Android emulator or device',
207221
func: runAndroid,
208222
options: [{
209-
command: '--install-debug',
210-
}, {
211223
command: '--root [string]',
212224
description: 'Override the root directory for the android build (which contains the android directory)',
213225
default: '',
214226
}, {
215227
command: '--flavor [string]',
216228
description: '--flavor has been deprecated. Use --variant instead',
229+
}, {
230+
command: '--configuration [string]',
231+
description: 'You can use `Release` or `Debug`. This creates a build based on the selected configuration. If you want to use the `Release` configuration make sure you have the `signingConfig` configured at `app/build.gradle`.',
232+
default: 'Debug'
217233
}, {
218234
command: '--variant [string]',
219235
}],

0 commit comments

Comments
 (0)