|
| 1 | +import { join } from 'path'; |
| 2 | + |
| 3 | +import c from '../colors'; |
| 4 | +import { runTask } from '../common'; |
| 5 | +import type { Config } from '../definitions'; |
| 6 | +import { logSuccess } from '../log'; |
| 7 | +import type { BuildCommandOptions } from '../tasks/build'; |
| 8 | +import { runCommand } from '../util/subprocess'; |
| 9 | + |
| 10 | +export async function buildAndroid( |
| 11 | + config: Config, |
| 12 | + buildOptions: BuildCommandOptions, |
| 13 | +): Promise<void> { |
| 14 | + const releaseType = buildOptions.androidreleasetype ?? 'AAB'; |
| 15 | + const releaseTypeIsAAB = releaseType === 'AAB'; |
| 16 | + const arg = releaseTypeIsAAB ? ':app:bundleRelease' : 'assembleRelease'; |
| 17 | + const gradleArgs = [arg]; |
| 18 | + |
| 19 | + if ( |
| 20 | + !buildOptions.keystorepath || |
| 21 | + !buildOptions.keystorealias || |
| 22 | + !buildOptions.keystorealiaspass || |
| 23 | + !buildOptions.keystorepass |
| 24 | + ) { |
| 25 | + throw 'Missing options. Please supply all options for android signing. (Keystore Path, Keystore Password, Keystore Key Alias, Keystore Key Password)'; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + await runTask('Running Gradle build', async () => |
| 30 | + runCommand('./gradlew', gradleArgs, { |
| 31 | + cwd: config.android.platformDirAbs, |
| 32 | + }), |
| 33 | + ); |
| 34 | + } catch (e) { |
| 35 | + if ((e as any).includes('EACCES')) { |
| 36 | + 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( |
| 37 | + `chmod +x ./${config.android.platformDir}/gradlew`, |
| 38 | + )} and try again.`; |
| 39 | + } else { |
| 40 | + throw e; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + const releasePath = join( |
| 45 | + config.android.appDirAbs, |
| 46 | + 'build', |
| 47 | + 'outputs', |
| 48 | + releaseTypeIsAAB ? 'bundle' : 'apk', |
| 49 | + 'release', |
| 50 | + ); |
| 51 | + |
| 52 | + const unsignedReleaseName = `app${ |
| 53 | + config.android.flavor ? `-${config.android.flavor}` : '' |
| 54 | + }-release${releaseTypeIsAAB ? '' : '-unsigned'}.${releaseType.toLowerCase()}`; |
| 55 | + |
| 56 | + const signedReleaseName = unsignedReleaseName.replace( |
| 57 | + `-release${ |
| 58 | + releaseTypeIsAAB ? '' : '-unsigned' |
| 59 | + }.${releaseType.toLowerCase()}`, |
| 60 | + `-release-signed.${releaseType.toLowerCase()}`, |
| 61 | + ); |
| 62 | + |
| 63 | + const signingArgs = [ |
| 64 | + '-sigalg', |
| 65 | + 'SHA1withRSA', |
| 66 | + '-digestalg', |
| 67 | + 'SHA1', |
| 68 | + '-keystore', |
| 69 | + buildOptions.keystorepath, |
| 70 | + '-keypass', |
| 71 | + buildOptions.keystorealiaspass, |
| 72 | + '-storepass', |
| 73 | + buildOptions.keystorepass, |
| 74 | + `-signedjar`, |
| 75 | + `${join(releasePath, signedReleaseName)}`, |
| 76 | + `${join(releasePath, unsignedReleaseName)}`, |
| 77 | + buildOptions.keystorealias, |
| 78 | + ]; |
| 79 | + |
| 80 | + await runTask('Signing Release', async () => { |
| 81 | + await runCommand('jarsigner', signingArgs, { |
| 82 | + cwd: config.android.platformDirAbs, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + logSuccess(`Successfully generated ${signedReleaseName} at: ${releasePath}`); |
| 87 | +} |
0 commit comments