2015-10-20 18:46:37 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-01-02 19:06:37 +00:00
|
|
|
const adb = require('./adb');
|
2015-10-20 18:46:37 +00:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const child_process = require('child_process');
|
|
|
|
const fs = require('fs');
|
2016-12-16 03:38:36 +00:00
|
|
|
const isPackagerRunning = require('../util/isPackagerRunning');
|
2017-01-02 19:06:37 +00:00
|
|
|
const isString = require('lodash/isString');
|
|
|
|
const path = require('path');
|
2016-12-16 03:38:36 +00:00
|
|
|
const Promise = require('promise');
|
2015-10-20 18:46:37 +00:00
|
|
|
|
2016-08-18 00:33:29 +00:00
|
|
|
// Verifies this is an Android project
|
|
|
|
function checkAndroid(root) {
|
|
|
|
return fs.existsSync(path.join(root, 'android/gradlew'));
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:46:37 +00:00
|
|
|
/**
|
2015-10-21 16:37:44 +00:00
|
|
|
* Starts the app on a connected Android emulator or device.
|
2015-10-20 18:46:37 +00:00
|
|
|
*/
|
2016-07-30 15:59:16 +00:00
|
|
|
function runAndroid(argv, config, args) {
|
|
|
|
if (!checkAndroid(args.root)) {
|
2015-10-20 18:46:37 +00:00
|
|
|
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-09 11:25:27 +00:00
|
|
|
if (!args.packager) {
|
|
|
|
return buildAndRun(args);
|
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
return isPackagerRunning().then(result => {
|
2015-10-20 18:46:37 +00:00
|
|
|
if (result === 'running') {
|
2016-07-30 15:59:16 +00:00
|
|
|
console.log(chalk.bold('JS server already running.'));
|
2015-10-20 18:46:37 +00:00
|
|
|
} else if (result === 'unrecognized') {
|
2016-07-30 15:59:16 +00:00
|
|
|
console.warn(chalk.yellow('JS server not recognized, continuing with build...'));
|
2015-10-20 18:46:37 +00:00
|
|
|
} else {
|
|
|
|
// result == 'not_running'
|
2016-07-30 15:59:16 +00:00
|
|
|
console.log(chalk.bold('Starting JS server...'));
|
2015-10-20 18:46:37 +00:00
|
|
|
startServerInNewWindow();
|
|
|
|
}
|
2016-07-30 15:59:16 +00:00
|
|
|
return buildAndRun(args);
|
|
|
|
});
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 13:43:12 +00:00
|
|
|
function getAdbPath() {
|
|
|
|
return process.env.ANDROID_HOME
|
|
|
|
? process.env.ANDROID_HOME + '/platform-tools/adb'
|
|
|
|
: 'adb';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs ADB reverse tcp:8081 tcp:8081 to allow loading the jsbundle from the packager
|
2016-08-16 07:44:32 +00:00
|
|
|
function tryRunAdbReverse(device) {
|
2016-06-23 13:43:12 +00:00
|
|
|
try {
|
|
|
|
const adbPath = getAdbPath();
|
|
|
|
const adbArgs = ['reverse', 'tcp:8081', 'tcp:8081'];
|
|
|
|
|
2016-08-16 07:44:32 +00:00
|
|
|
// If a device is specified then tell adb to use it
|
|
|
|
if (device) {
|
|
|
|
adbArgs.unshift('-s', device);
|
|
|
|
}
|
|
|
|
|
2016-06-23 13:43:12 +00:00
|
|
|
console.log(chalk.bold(
|
|
|
|
`Running ${adbPath} ${adbArgs.join(' ')}`
|
|
|
|
));
|
|
|
|
|
|
|
|
child_process.execFileSync(adbPath, adbArgs, {
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
});
|
2016-08-16 07:44:32 +00:00
|
|
|
} catch (e) {
|
2017-01-02 19:06:37 +00:00
|
|
|
console.log(chalk.yellow(`Could not run adb reverse: ${e.message}`));
|
2016-06-23 13:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:46:37 +00:00
|
|
|
// Builds the app and runs it on a connected emulator / device.
|
2016-07-30 15:59:16 +00:00
|
|
|
function buildAndRun(args) {
|
2016-12-16 03:38:36 +00:00
|
|
|
process.chdir(path.join(args.root, 'android'));
|
|
|
|
const cmd = process.platform.startsWith('win')
|
|
|
|
? 'gradlew.bat'
|
|
|
|
: './gradlew';
|
|
|
|
|
|
|
|
const packageName = fs.readFileSync(
|
|
|
|
'app/src/main/AndroidManifest.xml',
|
|
|
|
'utf8'
|
|
|
|
).match(/package="(.+?)"/)[1];
|
|
|
|
|
|
|
|
const adbPath = getAdbPath();
|
|
|
|
if (args.deviceId) {
|
2017-01-02 19:06:37 +00:00
|
|
|
if (isString(args.deviceId)) {
|
|
|
|
runOnSpecificDevice(args, cmd, packageName, adbPath);
|
|
|
|
} else {
|
|
|
|
console.log(chalk.red('Argument missing for parameter --deviceId'));
|
|
|
|
}
|
2016-12-16 03:38:36 +00:00
|
|
|
} else {
|
|
|
|
runOnAllDevices(args, cmd, packageName, adbPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runOnSpecificDevice(args, gradlew, packageName, adbPath) {
|
|
|
|
let devices = adb.getDevices();
|
|
|
|
if (devices && devices.length > 0) {
|
|
|
|
if (devices.indexOf(args.deviceId) !== -1) {
|
|
|
|
buildApk(gradlew);
|
|
|
|
installAndLaunchOnDevice(args, args.deviceId, packageName, adbPath);
|
|
|
|
} else {
|
|
|
|
console.log('Could not find device with the id: "' + args.deviceId + '".');
|
|
|
|
console.log('Choose one of the following:');
|
|
|
|
console.log(devices);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('No Android devices connected.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildApk(gradlew) {
|
2015-10-20 18:46:37 +00:00
|
|
|
try {
|
2017-01-02 19:06:37 +00:00
|
|
|
console.log(chalk.bold('Building the app...'));
|
2016-06-23 13:43:12 +00:00
|
|
|
|
2016-12-16 03:38:36 +00:00
|
|
|
// using '-x lint' in order to ignore linting errors while building the apk
|
|
|
|
child_process.execFileSync(gradlew, ['build', '-x', 'lint'], {
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2017-01-02 19:06:37 +00:00
|
|
|
console.log(chalk.red('Could not build the app, read the error above for details.\n'));
|
2016-12-16 03:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryInstallAppOnDevice(args, device) {
|
|
|
|
try {
|
|
|
|
const pathToApk = 'app/build/outputs/apk/app-debug.apk';
|
|
|
|
const adbPath = getAdbPath();
|
|
|
|
const adbArgs = ['-s', device, 'install', pathToApk];
|
|
|
|
console.log(chalk.bold(
|
|
|
|
`Installing the app on the device (cd android && adb -s ${device} install ${pathToApk}`
|
|
|
|
));
|
|
|
|
child_process.execFileSync(adbPath, adbArgs, {
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message);
|
|
|
|
console.log(chalk.red(
|
|
|
|
'Could not install the app on the device, read the error above for details.\n'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 12:00:54 +00:00
|
|
|
function tryLaunchAppOnDevice(device, packageName, adbPath, mainActivity) {
|
2016-12-16 03:38:36 +00:00
|
|
|
try {
|
2017-01-09 12:00:54 +00:00
|
|
|
const adbArgs = ['-s', device, 'shell', 'am', 'start', '-n', packageName + '/.' + mainActivity];
|
2016-12-16 03:38:36 +00:00
|
|
|
console.log(chalk.bold(
|
|
|
|
`Starting the app on ${device} (${adbPath} ${adbArgs.join(' ')})...`
|
|
|
|
));
|
|
|
|
child_process.spawnSync(adbPath, adbArgs, {stdio: 'inherit'});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(
|
|
|
|
'adb invocation failed. Do you have adb in your PATH?'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function installAndLaunchOnDevice(args, selectedDevice, packageName, adbPath) {
|
|
|
|
tryRunAdbReverse(selectedDevice);
|
|
|
|
tryInstallAppOnDevice(args, selectedDevice);
|
2017-01-09 12:00:54 +00:00
|
|
|
tryLaunchAppOnDevice(selectedDevice, packageName, adbPath, args.mainActivity);
|
2016-12-16 03:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runOnAllDevices(args, cmd, packageName, adbPath){
|
|
|
|
try {
|
2016-03-05 04:26:31 +00:00
|
|
|
const gradleArgs = [];
|
2016-07-30 15:59:16 +00:00
|
|
|
if (args.variant) {
|
|
|
|
gradleArgs.push('install' +
|
|
|
|
args.variant[0].toUpperCase() + args.variant.slice(1)
|
|
|
|
);
|
|
|
|
} else if (args.flavor) {
|
|
|
|
console.warn(chalk.yellow(
|
|
|
|
'--flavor has been deprecated. Use --variant instead'
|
|
|
|
));
|
|
|
|
gradleArgs.push('install' +
|
|
|
|
args.flavor[0].toUpperCase() + args.flavor.slice(1)
|
|
|
|
);
|
2016-03-05 04:26:31 +00:00
|
|
|
} else {
|
2016-12-16 03:38:36 +00:00
|
|
|
gradleArgs.push('installDebug');
|
2016-03-05 04:26:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 03:38:36 +00:00
|
|
|
if (args.installDebug) {
|
|
|
|
gradleArgs.push(args.installDebug);
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(chalk.bold(
|
2017-02-06 20:21:13 +00:00
|
|
|
`Building and installing the app on the device (cd android && ${cmd} ${gradleArgs.join(' ')})...`
|
2015-10-20 18:46:37 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
child_process.execFileSync(cmd, gradleArgs, {
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(
|
2015-11-03 11:46:08 +00:00
|
|
|
'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' +
|
2016-11-14 02:49:49 +00:00
|
|
|
'set up your Android development environment:\n' +
|
|
|
|
'https://facebook.github.io/react-native/docs/android-setup.html'
|
2015-10-20 18:46:37 +00:00
|
|
|
));
|
|
|
|
// stderr is automatically piped from the gradle process, so the user
|
|
|
|
// should see the error already, there is no need to do
|
|
|
|
// `console.log(e.stderr)`
|
2016-07-30 15:59:16 +00:00
|
|
|
return Promise.reject();
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
2016-03-08 20:07:59 +00:00
|
|
|
const devices = adb.getDevices();
|
|
|
|
if (devices && devices.length > 0) {
|
|
|
|
devices.forEach((device) => {
|
2016-12-16 03:38:36 +00:00
|
|
|
tryRunAdbReverse(device);
|
2017-01-09 12:00:54 +00:00
|
|
|
tryLaunchAppOnDevice(device, packageName, adbPath, args.mainActivity);
|
2016-04-06 16:20:39 +00:00
|
|
|
});
|
2016-03-08 20:07:59 +00:00
|
|
|
} else {
|
2016-12-16 03:38:36 +00:00
|
|
|
try {
|
|
|
|
// If we cannot execute based on adb devices output, fall back to
|
|
|
|
// shell am start
|
|
|
|
const fallbackAdbArgs = [
|
|
|
|
'shell', 'am', 'start', '-n', packageName + '/.MainActivity'
|
|
|
|
];
|
|
|
|
console.log(chalk.bold(
|
|
|
|
`Starting the app (${adbPath} ${fallbackAdbArgs.join(' ')}...`
|
|
|
|
));
|
|
|
|
child_process.spawnSync(adbPath, fallbackAdbArgs, {stdio: 'inherit'});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(
|
|
|
|
'adb invocation failed. Do you have adb in your PATH?'
|
|
|
|
));
|
|
|
|
// stderr is automatically piped from the gradle process, so the user
|
|
|
|
// should see the error already, there is no need to do
|
|
|
|
// `console.log(e.stderr)`
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2016-03-08 20:07:59 +00:00
|
|
|
}
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function startServerInNewWindow() {
|
2016-05-04 09:44:42 +00:00
|
|
|
const yargV = require('yargs').argv;
|
2016-04-29 11:13:30 +00:00
|
|
|
const scriptFile = /^win/.test(process.platform) ?
|
|
|
|
'launchPackager.bat' :
|
|
|
|
'launchPackager.command';
|
2016-05-04 09:44:42 +00:00
|
|
|
const packagerDir = path.resolve(__dirname, '..', '..', 'packager');
|
|
|
|
const launchPackagerScript = path.resolve(packagerDir, scriptFile);
|
|
|
|
const procConfig = {cwd: packagerDir};
|
2015-10-20 18:46:37 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2016-03-05 05:54:47 +00:00
|
|
|
if (yargV.open) {
|
2016-12-16 03:38:36 +00:00
|
|
|
return child_process.spawnSync('open', ['-a', yargV.open, launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
}
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawnSync('open', [launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
|
2015-10-20 18:46:37 +00:00
|
|
|
} else if (process.platform === 'linux') {
|
2016-05-04 09:44:42 +00:00
|
|
|
procConfig.detached = true;
|
2016-03-05 05:54:47 +00:00
|
|
|
if (yargV.open){
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawn(yargV.open,['-e', 'sh', launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
}
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawn('sh', [launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
|
2015-10-22 14:08:51 +00:00
|
|
|
} else if (/^win/.test(process.platform)) {
|
2016-05-04 09:44:42 +00:00
|
|
|
procConfig.detached = true;
|
|
|
|
procConfig.stdio = 'ignore';
|
|
|
|
return child_process.spawn('cmd.exe', ['/C', 'start', launchPackagerScript], procConfig);
|
2015-10-20 18:46:37 +00:00
|
|
|
} else {
|
2016-03-08 20:07:59 +00:00
|
|
|
console.log(chalk.red(`Cannot start the packager. Unknown platform ${process.platform}`));
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
module.exports = {
|
|
|
|
name: 'run-android',
|
|
|
|
description: 'builds your app and starts it on a connected Android emulator or device',
|
|
|
|
func: runAndroid,
|
|
|
|
options: [{
|
2016-12-16 03:38:36 +00:00
|
|
|
command: '--install-debug',
|
|
|
|
}, {
|
2016-07-30 15:59:16 +00:00
|
|
|
command: '--root [string]',
|
2016-12-16 03:38:36 +00:00
|
|
|
description: 'Override the root directory for the android build (which contains the android directory)',
|
2016-07-30 15:59:16 +00:00
|
|
|
default: '',
|
|
|
|
}, {
|
|
|
|
command: '--flavor [string]',
|
|
|
|
description: '--flavor has been deprecated. Use --variant instead',
|
|
|
|
}, {
|
|
|
|
command: '--variant [string]',
|
2017-01-09 12:00:54 +00:00
|
|
|
}, {
|
|
|
|
command: '--main-activity [string]',
|
|
|
|
description: 'Name of the activity to start',
|
|
|
|
default: 'MainActivity',
|
2016-11-23 14:32:58 +00:00
|
|
|
}, {
|
2016-12-16 03:38:36 +00:00
|
|
|
command: '--deviceId [string]',
|
|
|
|
description: 'builds your app and starts it on a specific device/simulator with the ' +
|
|
|
|
'given device id (listed by running "adb devices" on the command line).',
|
2017-01-09 11:25:27 +00:00
|
|
|
}, {
|
|
|
|
command: '--no-packager',
|
|
|
|
description: 'Do not launch packager while building',
|
2016-07-30 15:59:16 +00:00
|
|
|
}],
|
2017-01-02 19:06:37 +00:00
|
|
|
};
|