-
Notifications
You must be signed in to change notification settings - Fork 1k
/
build.ts
162 lines (145 loc) · 4.24 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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 flavor = buildOptions.flavor ?? '';
const arg = releaseTypeIsAAB
? `:app:bundle${flavor}Release`
: `assemble${flavor}Release`;
const gradleArgs = [arg];
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 releaseDir = releaseTypeIsAAB
? flavor !== ''
? `${flavor}Release`
: 'release'
: flavor !== ''
? join(flavor, 'release')
: 'release';
const releasePath = join(
config.android.appDirAbs,
'build',
'outputs',
releaseTypeIsAAB ? 'bundle' : 'apk',
releaseDir,
);
const unsignedReleaseName = `app${flavor !== '' ? `-${flavor}` : ''}-release${
releaseTypeIsAAB ? '' : '-unsigned'
}.${releaseType.toLowerCase()}`;
const signedReleaseName = unsignedReleaseName.replace(
`-release${
releaseTypeIsAAB ? '' : '-unsigned'
}.${releaseType.toLowerCase()}`,
`-release-signed.${releaseType.toLowerCase()}`,
);
if (buildOptions.signingtype == 'jarsigner') {
await signWithJarSigner(
config,
buildOptions,
releasePath,
signedReleaseName,
unsignedReleaseName,
);
} else {
await signWithApkSigner(
config,
buildOptions,
releasePath,
signedReleaseName,
unsignedReleaseName,
);
}
logSuccess(`Successfully generated ${signedReleaseName} at: ${releasePath}`);
}
async function signWithApkSigner(
config: Config,
buildOptions: BuildCommandOptions,
releasePath: string,
signedReleaseName: string,
unsignedReleaseName: string,
) {
if (!buildOptions.keystorepath || !buildOptions.keystorepass) {
throw 'Missing options. Please supply all options for android signing. (Keystore Path, Keystore Password)';
}
const signingArgs = [
'sign',
'--ks',
buildOptions.keystorepath,
'--ks-pass',
`pass:${buildOptions.keystorepass}`,
'--in',
`${join(releasePath, unsignedReleaseName)}`,
'--out',
`${join(releasePath, signedReleaseName)}`,
];
if (buildOptions.keystorealias) {
signingArgs.push('--ks-key-alias', buildOptions.keystorealias);
}
if (buildOptions.keystorealiaspass) {
signingArgs.push('--key-pass', buildOptions.keystorealiaspass);
}
await runTask('Signing Release', async () => {
await runCommand('apksigner', signingArgs, {
cwd: config.android.platformDirAbs,
});
});
}
async function signWithJarSigner(
config: Config,
buildOptions: BuildCommandOptions,
releasePath: string,
signedReleaseName: string,
unsignedReleaseName: string,
) {
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)';
}
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,
});
});
}