-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add build command for android (#5891)
* feat(cli): add build command for android * chore: remove console log * chore: mesage if try ios * chore: remove unused * chore: add cap-config file support
- Loading branch information
Showing
6 changed files
with
263 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { join } from 'path'; | ||
|
||
import c from '../colors'; | ||
import { runTask } from '../common'; | ||
import type { Config } from '../definitions'; | ||
import { logSuccess } from '../log'; | ||
import type { BuildCommandOptions } from '../tasks/build'; | ||
import { runCommand } from '../util/subprocess'; | ||
|
||
export async function buildAndroid( | ||
config: Config, | ||
buildOptions: BuildCommandOptions, | ||
): Promise<void> { | ||
const releaseType = buildOptions.androidreleasetype ?? 'AAB'; | ||
const releaseTypeIsAAB = releaseType === 'AAB'; | ||
const arg = releaseTypeIsAAB ? ':app:bundleRelease' : 'assembleRelease'; | ||
const gradleArgs = [arg]; | ||
|
||
if ( | ||
!buildOptions.keystorepath || | ||
!buildOptions.keystorealias || | ||
!buildOptions.keystorealiaspass || | ||
!buildOptions.keystorepass | ||
) { | ||
throw 'Missing options. Please supply all options for android signing. (Keystore Path, Keystore Password, Keystore Key Alias, Keystore Key Password)'; | ||
} | ||
|
||
try { | ||
await runTask('Running Gradle build', async () => | ||
runCommand('./gradlew', gradleArgs, { | ||
cwd: config.android.platformDirAbs, | ||
}), | ||
); | ||
} catch (e) { | ||
if ((e as any).includes('EACCES')) { | ||
throw `gradlew file does not have executable permissions. This can happen if the Android platform was added on a Windows machine. Please run ${c.strong( | ||
`chmod +x ./${config.android.platformDir}/gradlew`, | ||
)} and try again.`; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
const releasePath = join( | ||
config.android.appDirAbs, | ||
'build', | ||
'outputs', | ||
releaseTypeIsAAB ? 'bundle' : 'apk', | ||
'release', | ||
); | ||
|
||
const unsignedReleaseName = `app${ | ||
config.android.flavor ? `-${config.android.flavor}` : '' | ||
}-release${releaseTypeIsAAB ? '' : '-unsigned'}.${releaseType.toLowerCase()}`; | ||
|
||
const signedReleaseName = unsignedReleaseName.replace( | ||
`-release${ | ||
releaseTypeIsAAB ? '' : '-unsigned' | ||
}.${releaseType.toLowerCase()}`, | ||
`-release-signed.${releaseType.toLowerCase()}`, | ||
); | ||
|
||
const signingArgs = [ | ||
'-sigalg', | ||
'SHA1withRSA', | ||
'-digestalg', | ||
'SHA1', | ||
'-keystore', | ||
buildOptions.keystorepath, | ||
'-keypass', | ||
buildOptions.keystorealiaspass, | ||
'-storepass', | ||
buildOptions.keystorepass, | ||
`-signedjar`, | ||
`${join(releasePath, signedReleaseName)}`, | ||
`${join(releasePath, unsignedReleaseName)}`, | ||
buildOptions.keystorealias, | ||
]; | ||
|
||
await runTask('Signing Release', async () => { | ||
await runCommand('jarsigner', signingArgs, { | ||
cwd: config.android.platformDirAbs, | ||
}); | ||
}); | ||
|
||
logSuccess(`Successfully generated ${signedReleaseName} at: ${releasePath}`); | ||
} |
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
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
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
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
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,76 @@ | ||
import { buildAndroid } from '../android/build'; | ||
import { selectPlatforms, promptForPlatform } from '../common'; | ||
import type { Config } from '../definitions'; | ||
import { fatal, isFatal } from '../errors'; | ||
|
||
export interface BuildCommandOptions { | ||
keystorepath?: string; | ||
keystorepass?: string; | ||
keystorealias?: string; | ||
keystorealiaspass?: string; | ||
androidreleasetype?: 'AAB' | 'APK'; | ||
} | ||
|
||
export async function buildCommand( | ||
config: Config, | ||
selectedPlatformName: string, | ||
buildOptions: BuildCommandOptions, | ||
): Promise<void> { | ||
const platforms = await selectPlatforms(config, selectedPlatformName); | ||
let platformName: string; | ||
if (platforms.length === 1) { | ||
platformName = platforms[0]; | ||
} else { | ||
platformName = await promptForPlatform( | ||
platforms.filter(createBuildablePlatformFilter(config)), | ||
`Please choose a platform to build for:`, | ||
); | ||
} | ||
|
||
const buildCommandOptions: BuildCommandOptions = { | ||
keystorepath: | ||
buildOptions.keystorepath || config.android.buildOptions.keystorePath, | ||
keystorepass: | ||
buildOptions.keystorepass || config.android.buildOptions.keystorePassword, | ||
keystorealias: | ||
buildOptions.keystorealias || config.android.buildOptions.keystoreAlias, | ||
keystorealiaspass: | ||
buildOptions.keystorealiaspass || | ||
config.android.buildOptions.keystoreAliasPassword, | ||
androidreleasetype: | ||
buildOptions.androidreleasetype || | ||
config.android.buildOptions.releaseType, | ||
}; | ||
|
||
try { | ||
await build(config, platformName, buildCommandOptions); | ||
} catch (e) { | ||
if (!isFatal(e)) { | ||
fatal((e as any).stack ?? e); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
export async function build( | ||
config: Config, | ||
platformName: string, | ||
buildOptions: BuildCommandOptions, | ||
): Promise<void> { | ||
if (platformName == config.ios.name) { | ||
throw `Platform "${platformName}" is not available in the build command.`; | ||
} else if (platformName === config.android.name) { | ||
await buildAndroid(config, buildOptions); | ||
} else if (platformName === config.web.name) { | ||
throw `Platform "${platformName}" is not available in the build command.`; | ||
} else { | ||
throw `Platform "${platformName}" is not valid.`; | ||
} | ||
} | ||
|
||
function createBuildablePlatformFilter( | ||
config: Config, | ||
): (platform: string) => boolean { | ||
return platform => | ||
platform === config.ios.name || platform === config.android.name; | ||
} |