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 https://github.com/facebook/react-native/pull/10867

Differential Revision: D4167203

Pulled By: cpojer

fbshipit-source-id: c5ac07f81feeeea00ee0e8b059b46ef0d258a1a6
This commit is contained in:
Sandro Machado 2016-11-13 18:49:49 -08:00 committed by Facebook Github Bot
parent 39cf29c0f7
commit 2a2d3c6e5a
2 changed files with 33 additions and 13 deletions

View File

@ -148,6 +148,10 @@ $ react-native run-android
> If you get a "bridge configuration isn't available" error, see [Using adb reverse](#using-adb-reverse).
> Hint
>
> You can also use the `React Native CLI` to generate and run a `Release` build (e.g. `react-native run-android --configuration Release`).
<block class="mac windows linux android ios" />
## Connecting to the development server

View File

@ -77,14 +77,9 @@ function tryRunAdbReverse(device) {
// Builds the app and runs it on a connected emulator / device.
function buildAndRun(args) {
process.chdir(path.join(args.root, 'android'));
try {
adb.getDevices().map((device) => tryRunAdbReverse(device));
const cmd = process.platform.startsWith('win')
? 'gradlew.bat'
: './gradlew';
const gradleArgs = [];
if (args.variant) {
gradleArgs.push('install' +
@ -98,13 +93,33 @@ function buildAndRun(args) {
args.flavor[0].toUpperCase() + args.flavor.slice(1)
);
} else {
gradleArgs.push('installDebug');
gradleArgs.push('install');
}
if (args.installDebug) {
gradleArgs.push(args.installDebug);
// Append the build type to the current gradle install configuration. By default it will generate `installDebug`.
gradleArgs[0] = gradleArgs[0] + args.configuration[0].toUpperCase() + args.configuration.slice(1);
// Get the Android project directory.
const androidProjectDir = path.join(args.root, 'android');
if (args.configuration.toUpperCase() === 'RELEASE') {
console.log(chalk.bold(
`Generating the bundle for the release build...`
));
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/`, {
stdio: [process.stdin, process.stdout, process.stderr]
});
}
// Change to the Android directory.
process.chdir(androidProjectDir);
// Get the gradle binary for the current platform.
const cmd = process.platform.startsWith('win')
? 'gradlew.bat'
: './gradlew';
console.log(chalk.bold(
`Building and installing the app on the device (cd android && ${cmd} ${gradleArgs.join(' ')}...`
));
@ -116,9 +131,8 @@ function buildAndRun(args) {
console.log(chalk.red(
'Could not install the app on the device, read the error above for details.\n' +
'Make sure you have an Android emulator running or a device connected and have\n' +
'set up your Android development environment.\n' +
'Go to https://facebook.github.io/react-native/docs/getting-started.html\n' +
'and check the Android tab for setup instructions.'
'set up your Android development environment:\n' +
'https://facebook.github.io/react-native/docs/android-setup.html'
));
// stderr is automatically piped from the gradle process, so the user
// should see the error already, there is no need to do
@ -206,14 +220,16 @@ module.exports = {
description: 'builds your app and starts it on a connected Android emulator or device',
func: runAndroid,
options: [{
command: '--install-debug',
}, {
command: '--root [string]',
description: 'Override the root directory for the android build (which contains the android directory)',
default: '',
}, {
command: '--flavor [string]',
description: '--flavor has been deprecated. Use --variant instead',
}, {
command: '--configuration [string]',
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`.',
default: 'Debug'
}, {
command: '--variant [string]',
}],