This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command to build and run on Android (#3239)
- Loading branch information
Showing
4 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/expo-cli/src/commands/run/buildAndroidClientAsync.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { AndroidConfig } from '@expo/config-plugins'; | ||
import spawnAsync from '@expo/spawn-async'; | ||
import { Android } from '@expo/xdl'; | ||
import ora from 'ora'; | ||
import path from 'path'; | ||
|
||
import Log from '../../log'; | ||
import { prebuildAsync } from '../eject/prebuildAsync'; | ||
|
||
type Options = { | ||
buildVariant: string; | ||
}; | ||
|
||
function capitalize(name: string) { | ||
return name.charAt(0).toUpperCase() + name.slice(1); | ||
} | ||
|
||
function getGradleTask(buildVariant: string): string { | ||
return `install${capitalize(buildVariant)}`; | ||
} | ||
|
||
export default async function buildAndroidClientAsync( | ||
projectRoot: string, | ||
options: Options | ||
): Promise<void> { | ||
const devices = await Android.getAllAvailableDevicesAsync(); | ||
const device = devices.length > 1 ? await Android.promptForDeviceAsync(devices) : devices[0]; | ||
if (!device) { | ||
return; | ||
} | ||
const bootedDevice = await Android.attemptToStartEmulatorOrAssertAsync(device); | ||
if (!bootedDevice) { | ||
return; | ||
} | ||
|
||
Log.log('Building app...'); | ||
|
||
let androidProjectPath; | ||
try { | ||
androidProjectPath = await AndroidConfig.Paths.getProjectPathOrThrowAsync(projectRoot); | ||
} catch { | ||
// If the project doesn't have native code, prebuild it... | ||
await prebuildAsync(projectRoot, { | ||
install: true, | ||
platforms: ['android'], | ||
}); | ||
androidProjectPath = await AndroidConfig.Paths.getProjectPathOrThrowAsync(projectRoot); | ||
} | ||
|
||
const gradlew = path.join( | ||
androidProjectPath, | ||
process.platform === 'win32' ? 'gradlew.bat' : 'gradlew' | ||
); | ||
|
||
await spawnAsync(gradlew, [getGradleTask(options.buildVariant)], { | ||
cwd: androidProjectPath, | ||
stdio: 'inherit', | ||
}); | ||
|
||
const spinner = ora('Starting the development client...').start(); | ||
try { | ||
await Android.openProjectAsync({ | ||
projectRoot, | ||
shouldPrompt: false, | ||
devClient: true, | ||
}); | ||
} catch (error) { | ||
spinner.fail(); | ||
throw error; | ||
} | ||
|
||
spinner.succeed(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Command } from 'commander'; | ||
|
||
import CommandError from '../../CommandError'; | ||
import buildAndroidClientAsync from './buildAndroidClientAsync'; | ||
|
||
type Options = { | ||
platform?: string; | ||
buildVariant?: string; | ||
}; | ||
|
||
async function runAndroidAsync(projectRoot: string, options: Options) { | ||
if (typeof options.buildVariant !== 'string') { | ||
throw new CommandError('--build-variant must be a string'); | ||
} | ||
await buildAndroidClientAsync(projectRoot, { | ||
buildVariant: options.buildVariant, | ||
}); | ||
} | ||
|
||
export default function (program: Command) { | ||
program | ||
.command('run:android [path]') | ||
.helpGroup('experimental') | ||
.description('Build a development client and run it in on a device.') | ||
.option('--build-variant [name]', '(Android) build variant', 'release') | ||
.asyncActionProjectDir(runAndroidAsync); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters