uniapp-android-cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +595 -2
- package/bin/uni-pack.js +7 -0
- package/package.json +66 -4
- package/src/cli.js +184 -0
- package/src/commands/build.js +42 -0
- package/src/commands/debug.js +83 -0
- package/src/commands/device.js +303 -0
- package/src/commands/devices.js +23 -0
- package/src/commands/doctor.js +160 -0
- package/src/commands/init.js +277 -0
- package/src/commands/wgt.js +46 -0
- package/src/core/adb.js +234 -0
- package/src/core/build.js +374 -0
- package/src/core/config.js +157 -0
- package/src/core/devicePairing.js +152 -0
- package/src/core/errors.js +36 -0
- package/src/core/fsx.js +172 -0
- package/src/core/gradle.js +75 -0
- package/src/core/hbuilderx.js +258 -0
- package/src/core/logger.js +82 -0
- package/src/core/output.js +74 -0
- package/src/core/patch/assets.js +154 -0
- package/src/core/patch/gradle.js +128 -0
- package/src/core/patch/manifest.js +81 -0
- package/src/core/patch/nativePlugins.js +137 -0
- package/src/core/patch/projectManifest.js +66 -0
- package/src/core/patch/template.js +57 -0
- package/src/core/patch/utsPlugins.js +424 -0
- package/src/core/process.js +71 -0
- package/src/core/project.js +250 -0
- package/src/core/qrcode.js +13 -0
- package/src/core/sdk.js +398 -0
- package/src/core/signing.js +84 -0
- package/src/core/version.js +128 -0
- package/src/core/webBuild.js +69 -0
- package/src/core/wgt.js +76 -0
package/src/core/adb.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { PackError, EXIT } from './errors.js'
|
|
2
|
+
import { run } from './process.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {{ serial: string, state: string, model: string }} Device
|
|
6
|
+
* @typedef {{ name: string, type: 'pairing' | 'connect' | 'other', host: string, port: number, address: string }} MdnsService
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {string} adb
|
|
11
|
+
* @returns {Promise<boolean>}
|
|
12
|
+
*/
|
|
13
|
+
export async function mdnsSupported(adb) {
|
|
14
|
+
const { code, stdout, stderr } = await run(adb, ['mdns', 'check'], { allowFailure: true })
|
|
15
|
+
// 失败时也常带 "mdns"(如 ERROR: mdns daemon unavailable),只认版本行
|
|
16
|
+
return code === 0 && /mdns daemon version/i.test(`${stdout}${stderr}`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 解析 `adb mdns services` 输出,兼容 IPv4 与 [IPv6]:port。
|
|
21
|
+
* @param {string} stdout
|
|
22
|
+
* @returns {MdnsService[]}
|
|
23
|
+
*/
|
|
24
|
+
export function parseMdnsServices(stdout) {
|
|
25
|
+
/** @type {MdnsService[]} */
|
|
26
|
+
const services = []
|
|
27
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
28
|
+
const match = /^(\S+)\s+(\[[^\]]+\]|[^\s:]+):(\d+)\s*$/.exec(line.trim())
|
|
29
|
+
if (!match) {
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
const name = match[1]
|
|
33
|
+
const host = match[2].replace(/^\[|\]$/g, '')
|
|
34
|
+
const type = name.includes('_adb-tls-pairing._tcp')
|
|
35
|
+
? 'pairing'
|
|
36
|
+
: name.includes('_adb-tls-connect._tcp')
|
|
37
|
+
? 'connect'
|
|
38
|
+
: 'other'
|
|
39
|
+
services.push({ name, type, host, port: Number(match[3]), address: `${host}:${match[3]}` })
|
|
40
|
+
}
|
|
41
|
+
return services
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} adb
|
|
46
|
+
* @returns {Promise<MdnsService[]>}
|
|
47
|
+
*/
|
|
48
|
+
export async function listMdnsServices(adb) {
|
|
49
|
+
const { stdout } = await run(adb, ['mdns', 'services'], { allowFailure: true })
|
|
50
|
+
return parseMdnsServices(stdout)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {string} adb
|
|
55
|
+
* @param {string} address
|
|
56
|
+
* @param {string} code
|
|
57
|
+
*/
|
|
58
|
+
export async function pairDevice(adb, address, code) {
|
|
59
|
+
const { code: exitCode, stdout, stderr } = await run(adb, ['pair', address, code], { allowFailure: true })
|
|
60
|
+
const output = `${stdout}\n${stderr}`.trim()
|
|
61
|
+
if (exitCode !== 0 || /failed|error|cannot/i.test(output)) {
|
|
62
|
+
throw new PackError(`配对失败:${output.split(/\r?\n/).slice(-3).join('\n') || 'adb pair 无输出'}`, EXIT.DEVICE, {
|
|
63
|
+
hint: '确认手机与电脑在同一 Wi-Fi、二维码未过期;如有 VPN 先关闭再重试',
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
return output
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} adb
|
|
71
|
+
* @param {string} address
|
|
72
|
+
*/
|
|
73
|
+
export async function connectDevice(adb, address) {
|
|
74
|
+
const { code, stdout, stderr } = await run(adb, ['connect', address], { allowFailure: true })
|
|
75
|
+
const output = `${stdout}\n${stderr}`.trim()
|
|
76
|
+
if (code !== 0 || /failed|cannot/i.test(output)) {
|
|
77
|
+
throw new PackError(`连接失败:${output.split(/\r?\n/).slice(-3).join('\n') || address}`, EXIT.DEVICE, {
|
|
78
|
+
hint: '确认手机无线调试已开启、与电脑同网段,并已配对',
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
return output
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {string} adb
|
|
86
|
+
* @param {string} [address]
|
|
87
|
+
*/
|
|
88
|
+
export async function disconnectDevice(adb, address) {
|
|
89
|
+
const args = address ? ['disconnect', address] : ['disconnect']
|
|
90
|
+
const { stdout, stderr } = await run(adb, args, { allowFailure: true })
|
|
91
|
+
return `${stdout}\n${stderr}`.trim()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @param {string} adb
|
|
96
|
+
* @param {string} serial
|
|
97
|
+
* @param {number} port
|
|
98
|
+
*/
|
|
99
|
+
export async function enableTcpip(adb, serial, port) {
|
|
100
|
+
const { code, stdout, stderr } = await run(adb, ['-s', serial, 'tcpip', String(port)], { allowFailure: true })
|
|
101
|
+
if (code !== 0) {
|
|
102
|
+
throw new PackError(`开启无线调试失败:${(stderr || stdout).trim()}`, EXIT.DEVICE)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {string} adb
|
|
108
|
+
* @param {string} serial
|
|
109
|
+
* @returns {Promise<string>}
|
|
110
|
+
*/
|
|
111
|
+
export async function deviceWifiIp(adb, serial) {
|
|
112
|
+
const { stdout } = await run(adb, ['-s', serial, 'shell', 'ip', '-f', 'inet', 'addr', 'show', 'wlan0'], {
|
|
113
|
+
allowFailure: true,
|
|
114
|
+
})
|
|
115
|
+
const match = /inet\s+(\d+\.\d+\.\d+\.\d+)/.exec(stdout)
|
|
116
|
+
return match?.[1] ?? ''
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @param {string} adb
|
|
121
|
+
* @param {string} address
|
|
122
|
+
* @param {number} timeoutMs
|
|
123
|
+
*/
|
|
124
|
+
export async function waitForDevice(adb, address, timeoutMs) {
|
|
125
|
+
const deadline = Date.now() + timeoutMs
|
|
126
|
+
while (Date.now() < deadline) {
|
|
127
|
+
const devices = await listDevices(adb).catch(() => [])
|
|
128
|
+
const match = devices.find((device) => device.serial === address && device.state === 'device')
|
|
129
|
+
if (match) {
|
|
130
|
+
return match
|
|
131
|
+
}
|
|
132
|
+
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
133
|
+
}
|
|
134
|
+
return null
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {string} adb
|
|
139
|
+
* @returns {Promise<Device[]>}
|
|
140
|
+
*/
|
|
141
|
+
export async function listDevices(adb) {
|
|
142
|
+
const { stdout } = await run(adb, ['devices', '-l'], { allowFailure: false })
|
|
143
|
+
return stdout
|
|
144
|
+
.split(/\r?\n/)
|
|
145
|
+
.slice(1)
|
|
146
|
+
.filter((line) => line.trim().length > 0 && !line.startsWith('*'))
|
|
147
|
+
.map((line) => {
|
|
148
|
+
const [serial, state, ...rest] = line.trim().split(/\s+/)
|
|
149
|
+
const model = rest.find((item) => item.startsWith('model:'))?.slice('model:'.length) ?? ''
|
|
150
|
+
return { serial, state, model }
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {Device[]} devices
|
|
156
|
+
* @param {string} [serial]
|
|
157
|
+
*/
|
|
158
|
+
export function pickDevice(devices, serial) {
|
|
159
|
+
if (serial) {
|
|
160
|
+
const match = devices.find((device) => device.serial === serial)
|
|
161
|
+
if (!match) {
|
|
162
|
+
throw new PackError(`找不到设备:${serial}`, EXIT.DEVICE)
|
|
163
|
+
}
|
|
164
|
+
return match
|
|
165
|
+
}
|
|
166
|
+
const online = devices.filter((device) => device.state === 'device')
|
|
167
|
+
if (online.length === 0) {
|
|
168
|
+
throw new PackError('没有可用设备,请连接设备或启动模拟器后重试', EXIT.DEVICE)
|
|
169
|
+
}
|
|
170
|
+
return online[0]
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @param {string} adb
|
|
175
|
+
* @param {string} apk
|
|
176
|
+
* @param {string} serial
|
|
177
|
+
*/
|
|
178
|
+
export async function installApk(adb, apk, serial) {
|
|
179
|
+
const { code, stdout, stderr } = await run(adb, ['-s', serial, 'install', '-r', apk], { allowFailure: true })
|
|
180
|
+
if (code !== 0 || /Failure/i.test(stdout + stderr)) {
|
|
181
|
+
throw new PackError(`安装失败:${(stdout + stderr).trim().split('\n').slice(-3).join('\n')}`, EXIT.DEVICE)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {string} adb
|
|
187
|
+
* @param {string} serial
|
|
188
|
+
* @param {string} packageName
|
|
189
|
+
*/
|
|
190
|
+
export async function isInstalled(adb, serial, packageName) {
|
|
191
|
+
const { stdout } = await run(adb, ['-s', serial, 'shell', 'pm', 'list', 'packages', packageName], {
|
|
192
|
+
allowFailure: true,
|
|
193
|
+
})
|
|
194
|
+
return stdout.split(/\r?\n/).some((line) => line.trim() === `package:${packageName}`)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @param {string} adb
|
|
199
|
+
* @param {string} packageName
|
|
200
|
+
* @param {string} activity
|
|
201
|
+
* @param {string} serial
|
|
202
|
+
*/
|
|
203
|
+
export async function startApp(adb, packageName, activity, serial) {
|
|
204
|
+
await run(adb, ['-s', serial, 'shell', 'am', 'force-stop', packageName], { allowFailure: true })
|
|
205
|
+
const { code, stdout, stderr } = await run(
|
|
206
|
+
adb,
|
|
207
|
+
['-s', serial, 'shell', 'am', 'start', '-n', `${packageName}/${activity}`],
|
|
208
|
+
{ allowFailure: true },
|
|
209
|
+
)
|
|
210
|
+
if (code !== 0) {
|
|
211
|
+
throw new PackError(`启动应用失败:${(stderr || stdout).trim()}`, EXIT.DEVICE, {
|
|
212
|
+
hint: '基座未安装?先执行 uni-pack debug --install',
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @param {string} adb
|
|
219
|
+
* @param {string} serial
|
|
220
|
+
* @param {number} port
|
|
221
|
+
*/
|
|
222
|
+
export async function reversePort(adb, serial, port) {
|
|
223
|
+
await run(adb, ['-s', serial, 'reverse', `tcp:${port}`, `tcp:${port}`], { allowFailure: false })
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @param {string} adb
|
|
228
|
+
* @param {string} serial
|
|
229
|
+
* @returns {Promise<string>}
|
|
230
|
+
*/
|
|
231
|
+
export async function packagePid(adb, serial, packageName) {
|
|
232
|
+
const { stdout } = await run(adb, ['-s', serial, 'shell', 'pidof', packageName], { allowFailure: true })
|
|
233
|
+
return stdout.trim()
|
|
234
|
+
}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { copyFile, readdir } from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { PackError, EXIT } from './errors.js'
|
|
4
|
+
import { emptyDir, ensureDir, exists } from './fsx.js'
|
|
5
|
+
import { runGradle } from './gradle.js'
|
|
6
|
+
import { resolveHbuilderx } from './hbuilderx.js'
|
|
7
|
+
import { patchAndroidManifest } from './patch/manifest.js'
|
|
8
|
+
import { patchAppGradle, patchRootGradle } from './patch/gradle.js'
|
|
9
|
+
import { applyAssets } from './patch/assets.js'
|
|
10
|
+
import { applyNativePlugins } from './patch/nativePlugins.js'
|
|
11
|
+
import { mergeProjectManifest } from './patch/projectManifest.js'
|
|
12
|
+
import { prepareTemplate } from './patch/template.js'
|
|
13
|
+
import { UTS_AAR, integrateUtsPlugins, scanUtsPlugins } from './patch/utsPlugins.js'
|
|
14
|
+
import {
|
|
15
|
+
detectVueVersion,
|
|
16
|
+
normalizeManifest,
|
|
17
|
+
readProjectManifest,
|
|
18
|
+
readProjectPackage,
|
|
19
|
+
} from './project.js'
|
|
20
|
+
import {
|
|
21
|
+
BASE_AARS,
|
|
22
|
+
DEBUG_AAR,
|
|
23
|
+
MODULE_AARS,
|
|
24
|
+
findSdkForVersion,
|
|
25
|
+
readTemplateDefaults,
|
|
26
|
+
resolveAndroidTools,
|
|
27
|
+
resolveJavaHome,
|
|
28
|
+
resolveSdk,
|
|
29
|
+
} from './sdk.js'
|
|
30
|
+
import { resolveSigning } from './signing.js'
|
|
31
|
+
import { assertVersionMatch, readCompilerVersion, readSdkVersion, versionMismatchError } from './version.js'
|
|
32
|
+
import { runWebBuild } from './webBuild.js'
|
|
33
|
+
import { apkBaseName, findApk, publishApk } from './output.js'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {Object} input
|
|
37
|
+
* @param {any} input.config
|
|
38
|
+
* @param {any} input.project
|
|
39
|
+
* @param {'release' | 'debug'} input.mode
|
|
40
|
+
* @param {any} input.options
|
|
41
|
+
* @param {import('./logger.js').Logger} input.logger
|
|
42
|
+
*/
|
|
43
|
+
export async function buildProject(input) {
|
|
44
|
+
const { config, project, mode, options, logger } = input
|
|
45
|
+
const debug = mode === 'debug'
|
|
46
|
+
|
|
47
|
+
const { manifest, file, type, inputDir } = await readProjectManifest(project.projectDir)
|
|
48
|
+
project.type = type
|
|
49
|
+
project.inputDir = inputDir
|
|
50
|
+
|
|
51
|
+
const resolved = normalizeManifest(manifest, {
|
|
52
|
+
appid: project.appid,
|
|
53
|
+
name: project.name,
|
|
54
|
+
package: project.package || (await readProjectPackage(project.projectDir)),
|
|
55
|
+
versionName: project.versionName,
|
|
56
|
+
versionCode: project.versionCode,
|
|
57
|
+
minSdkVersion: project.minSdkVersion,
|
|
58
|
+
targetSdkVersion: project.targetSdkVersion,
|
|
59
|
+
abiFilters: project.abiFilters,
|
|
60
|
+
})
|
|
61
|
+
const vueVersion = await detectVueVersion(project.projectDir, manifest, type)
|
|
62
|
+
logger.info(`项目 ${resolved.name}(${resolved.appid})v${resolved.versionName}(${resolved.versionCode})`)
|
|
63
|
+
logger.detail(
|
|
64
|
+
`manifest: ${file}(${type === 'hbuilderx' ? 'HBuilderX' : 'CLI'} 项目,Vue ${vueVersion})`,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const hbuilderx = type === 'hbuilderx' ? await resolveHbuilderx(config, { project }) : null
|
|
68
|
+
if (type === 'hbuilderx' && !hbuilderx && !options.skipWeb) {
|
|
69
|
+
throw new PackError('找不到 HBuilderX,无法编译 HBuilderX 项目的源码', EXIT.PREFLIGHT, {
|
|
70
|
+
hint: [
|
|
71
|
+
'任选一种方式指定:',
|
|
72
|
+
' 1) uni-pack init -p <项目> --hbuilderx <HBuilderX 目录>',
|
|
73
|
+
' 2) 在配置 hbuilderx.root 里填写 HBuilderX 安装目录',
|
|
74
|
+
' 3) 设置环境变量 HBUILDERX_HOME',
|
|
75
|
+
].join('\n'),
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const compiler = await readCompilerVersion(project, { hbuilderxRoot: hbuilderx?.root })
|
|
80
|
+
if (compiler.version) {
|
|
81
|
+
logger.info(`编译器版本:${compiler.version}(${compiler.source})`)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let sdk = await resolveSdk(config, { project, version: compiler.version ?? undefined })
|
|
85
|
+
let sdkVersion = await readSdkVersion(sdk.root)
|
|
86
|
+
// 全局 sdk.root 可能是别的项目留下的;项目没单独指定时按编译器版本自动切到匹配的 SDK。
|
|
87
|
+
if (compiler.version && sdkVersion && compiler.version !== sdkVersion && !project.sdkRoot) {
|
|
88
|
+
const matched = await findSdkForVersion(config, compiler.version)
|
|
89
|
+
if (matched) {
|
|
90
|
+
sdk = await resolveSdk(config, { project: { ...project, sdkRoot: matched } })
|
|
91
|
+
sdkVersion = await readSdkVersion(sdk.root)
|
|
92
|
+
logger.info(`按项目版本切换离线 SDK:${sdk.root}`)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
logger.detail(`离线 SDK:${sdk.root}`)
|
|
96
|
+
|
|
97
|
+
const versions = { compiler: compiler.version, sdk: sdkVersion }
|
|
98
|
+
const versionCheck = assertVersionMatch(versions)
|
|
99
|
+
if (!versionCheck.ok && !options.skipVersionCheck) {
|
|
100
|
+
throw versionMismatchError(versions, { type })
|
|
101
|
+
}
|
|
102
|
+
if (versionCheck.ok) {
|
|
103
|
+
logger.info(`版本一致:uni-app ${versions.compiler} / SDK ${versions.sdk}`)
|
|
104
|
+
} else {
|
|
105
|
+
logger.warn(`版本不一致:uni-app ${versions.compiler ?? '未知'} / SDK ${versions.sdk ?? '未知'}(已跳过检查)`)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let resourcesDir = path.resolve(
|
|
109
|
+
project.projectDir,
|
|
110
|
+
String(project.webBuild?.outputDir ?? (type === 'hbuilderx' ? 'unpackage/dist/build/app-plus' : 'dist/build/app')),
|
|
111
|
+
)
|
|
112
|
+
if (options.skipWeb) {
|
|
113
|
+
if (!(await exists(path.join(resourcesDir, 'app-service.js')))) {
|
|
114
|
+
throw new PackError('--skip-web 但前端产物不存在,请先构建', EXIT.WEB_BUILD)
|
|
115
|
+
}
|
|
116
|
+
logger.info(`跳过前端构建,使用 ${resourcesDir}`)
|
|
117
|
+
} else {
|
|
118
|
+
resourcesDir = (await runWebBuild(project, logger, { hbuilderx: hbuilderx ?? undefined, vueVersion })).dir
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const utsPlugins = await scanUtsPlugins({ projectDir: project.projectDir, resourcesDir })
|
|
122
|
+
const hasUts = utsPlugins.some((plugin) => plugin.usable)
|
|
123
|
+
if (utsPlugins.length > 0) {
|
|
124
|
+
logger.info(`UTS 插件:${utsPlugins.map((plugin) => plugin.name).join(', ')}`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const signing = await resolveSigning(project, options)
|
|
128
|
+
const templateDefaults = await readTemplateDefaults(sdk.templateRoot)
|
|
129
|
+
const compileSdkVersion = Number(project.compileSdkVersion ?? templateDefaults.compileSdkVersion ?? 35)
|
|
130
|
+
const buildToolsVersion = firstNonEmpty(
|
|
131
|
+
project.buildToolsVersion,
|
|
132
|
+
config.android?.buildTools,
|
|
133
|
+
templateDefaults.buildToolsVersion,
|
|
134
|
+
)
|
|
135
|
+
const tools = await resolveAndroidTools(config, { preferredBuildTools: buildToolsVersion })
|
|
136
|
+
const javaHome = await resolveJavaHome(config)
|
|
137
|
+
const workRoot = path.join(config.workDir, sanitize(project.name ?? 'project'))
|
|
138
|
+
const gradleRoot = path.join(workRoot, 'gradle')
|
|
139
|
+
|
|
140
|
+
logger.step('准备构建工程')
|
|
141
|
+
await emptyDir(gradleRoot)
|
|
142
|
+
await prepareTemplate(sdk.templateRoot, gradleRoot)
|
|
143
|
+
const aars = await copyAars({
|
|
144
|
+
sdk,
|
|
145
|
+
targetDir: path.join(gradleRoot, 'simpleDemo/libs'),
|
|
146
|
+
debug,
|
|
147
|
+
modules: resolved.modules,
|
|
148
|
+
uts: hasUts,
|
|
149
|
+
vueVersion,
|
|
150
|
+
logger,
|
|
151
|
+
})
|
|
152
|
+
logger.success(`模板与 AAR 就绪(${aars.count} 个)`)
|
|
153
|
+
|
|
154
|
+
logger.step('写入项目配置')
|
|
155
|
+
await patchRootGradle(path.join(gradleRoot, 'build.gradle'), config.android.sdkDir)
|
|
156
|
+
await patchAppGradle(path.join(gradleRoot, 'simpleDemo/build.gradle'), {
|
|
157
|
+
packageName: resolved.packageName,
|
|
158
|
+
versionName: resolved.versionName,
|
|
159
|
+
versionCode: resolved.versionCode,
|
|
160
|
+
minSdkVersion: resolved.minSdkVersion,
|
|
161
|
+
targetSdkVersion: resolved.targetSdkVersion,
|
|
162
|
+
compileSdkVersion,
|
|
163
|
+
buildToolsVersion: tools.buildTools,
|
|
164
|
+
abiFilters: resolved.abiFilters,
|
|
165
|
+
signing,
|
|
166
|
+
})
|
|
167
|
+
const androidManifest = path.join(gradleRoot, 'simpleDemo/src/main/AndroidManifest.xml')
|
|
168
|
+
await patchAndroidManifest(androidManifest, {
|
|
169
|
+
packageName: resolved.packageName,
|
|
170
|
+
appKey: String(project.appKey ?? ''),
|
|
171
|
+
permissions: resolved.permissions,
|
|
172
|
+
features: resolved.features.map((entry) => {
|
|
173
|
+
const [name, required] = entry.split('|')
|
|
174
|
+
return { name, required }
|
|
175
|
+
}),
|
|
176
|
+
debug,
|
|
177
|
+
})
|
|
178
|
+
await mergeProjectManifest({ targetFile: androidManifest, projectDir: project.projectDir, logger })
|
|
179
|
+
await copyFile(signing.keystore, path.join(gradleRoot, 'simpleDemo/aw-pack.jks'))
|
|
180
|
+
await applyAssets({
|
|
181
|
+
moduleDir: path.join(gradleRoot, 'simpleDemo'),
|
|
182
|
+
dataDir: sdk.dataDir,
|
|
183
|
+
resourcesDir,
|
|
184
|
+
appid: resolved.appid,
|
|
185
|
+
appName: resolved.name,
|
|
186
|
+
versionName: resolved.versionName,
|
|
187
|
+
debug,
|
|
188
|
+
projectDir: project.projectDir,
|
|
189
|
+
manifest,
|
|
190
|
+
})
|
|
191
|
+
await applyNativePlugins({
|
|
192
|
+
projectDir: project.projectDir,
|
|
193
|
+
moduleDir: path.join(gradleRoot, 'simpleDemo'),
|
|
194
|
+
libsDir: path.join(gradleRoot, 'simpleDemo/libs'),
|
|
195
|
+
logger,
|
|
196
|
+
})
|
|
197
|
+
await integrateUtsPlugins({
|
|
198
|
+
plugins: utsPlugins,
|
|
199
|
+
gradleRoot,
|
|
200
|
+
mainModule: 'simpleDemo',
|
|
201
|
+
compileSdkVersion,
|
|
202
|
+
logger,
|
|
203
|
+
})
|
|
204
|
+
logger.success('配置写入完成')
|
|
205
|
+
|
|
206
|
+
logger.step(`Gradle ${debug ? 'assembleDebug' : 'assembleRelease'}`)
|
|
207
|
+
await runGradle({
|
|
208
|
+
gradlew: sdk.gradlew,
|
|
209
|
+
cwd: gradleRoot,
|
|
210
|
+
task: debug ? 'assembleDebug' : 'assembleRelease',
|
|
211
|
+
javaHome,
|
|
212
|
+
androidSdkDir: config.android.sdkDir,
|
|
213
|
+
offline: Boolean(options.offline),
|
|
214
|
+
logger,
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
const apkDir = path.join(gradleRoot, 'simpleDemo/build/outputs/apk', mode)
|
|
218
|
+
const apk = await findApk(apkDir, mode)
|
|
219
|
+
await verifySignature(tools.apksigner, apk, logger)
|
|
220
|
+
|
|
221
|
+
const baseName = apkBaseName(project.name ?? resolved.name, resolved.versionName, resolved.versionCode)
|
|
222
|
+
const published = await publishApk({
|
|
223
|
+
apk,
|
|
224
|
+
outputDir: path.resolve(String(options.out ?? project.outputDir)),
|
|
225
|
+
baseName,
|
|
226
|
+
mode,
|
|
227
|
+
workDir: workRoot,
|
|
228
|
+
record: {
|
|
229
|
+
appid: resolved.appid,
|
|
230
|
+
packageName: resolved.packageName,
|
|
231
|
+
versionName: resolved.versionName,
|
|
232
|
+
versionCode: resolved.versionCode,
|
|
233
|
+
projectType: type,
|
|
234
|
+
vueVersion,
|
|
235
|
+
compilerVersion: versions.compiler,
|
|
236
|
+
sdkVersion: versions.sdk,
|
|
237
|
+
hbuilderx: hbuilderx?.root ?? '',
|
|
238
|
+
template: sdk.templateRoot,
|
|
239
|
+
javaHome,
|
|
240
|
+
},
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
logger.success(`APK:${published.target}`)
|
|
244
|
+
logger.info(`最新副本:${published.latest}`)
|
|
245
|
+
return { apk: published.target, latest: published.latest, sha256: published.sha256, resolved, workRoot }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @param {string} apksigner
|
|
250
|
+
* @param {string} apk
|
|
251
|
+
* @param {import('./logger.js').Logger} logger
|
|
252
|
+
*/
|
|
253
|
+
async function verifySignature(apksigner, apk, logger) {
|
|
254
|
+
try {
|
|
255
|
+
const { run } = await import('./process.js')
|
|
256
|
+
const { stdout } = await run(apksigner, ['verify', '--print-certs', apk], { allowFailure: false })
|
|
257
|
+
const sha1 = /SHA-1 digest:\s*([0-9a-f:]+)/i.exec(stdout)?.[1] ?? ''
|
|
258
|
+
const sha256 = /SHA-256 digest:\s*([0-9a-f:]+)/i.exec(stdout)?.[1] ?? ''
|
|
259
|
+
logger.info(`签名 SHA1:${sha1}`)
|
|
260
|
+
logger.detail(`签名 SHA256:${sha256}`)
|
|
261
|
+
} catch (error) {
|
|
262
|
+
throw new PackError('APK 签名校验失败', EXIT.SIGN, { cause: error })
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 模板自带的 libs 是该 SDK 版本的基线;BASE_AARS 里新旧版本都有的再补上,
|
|
268
|
+
* 老 SDK 缺少的 AAR 只提示不阻断。
|
|
269
|
+
* @param {Object} options
|
|
270
|
+
* @param {{ templateRoot: string, libsDir: string }} options.sdk
|
|
271
|
+
* @param {string} options.targetDir
|
|
272
|
+
* @param {boolean} options.debug
|
|
273
|
+
* @param {string[]} options.modules
|
|
274
|
+
* @param {boolean} [options.uts]
|
|
275
|
+
* @param {2 | 3} [options.vueVersion]
|
|
276
|
+
* @param {import('./logger.js').Logger} options.logger
|
|
277
|
+
*/
|
|
278
|
+
async function copyAars(options) {
|
|
279
|
+
const { sdk, targetDir, debug, modules, uts, vueVersion, logger } = options
|
|
280
|
+
await ensureDir(targetDir)
|
|
281
|
+
|
|
282
|
+
/** @type {Map<string, string>} */
|
|
283
|
+
const sources = new Map()
|
|
284
|
+
const templateLibs = path.join(sdk.templateRoot, 'simpleDemo/libs')
|
|
285
|
+
for (const name of await listLibs(templateLibs)) {
|
|
286
|
+
if (name === DEBUG_AAR && !debug) {
|
|
287
|
+
continue
|
|
288
|
+
}
|
|
289
|
+
sources.set(name, path.join(templateLibs, name))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const available = new Set(await listLibs(sdk.libsDir))
|
|
293
|
+
// 模板里的 uniapp-v8-release.aar 可能只带 Vue3 运行时;Vue2 必须用 SDK/libs 的完整版。
|
|
294
|
+
if (vueVersion === 2 && available.has('uniapp-v8-release.aar')) {
|
|
295
|
+
sources.set('uniapp-v8-release.aar', path.join(sdk.libsDir, 'uniapp-v8-release.aar'))
|
|
296
|
+
}
|
|
297
|
+
const missing = []
|
|
298
|
+
for (const name of debug ? [...BASE_AARS, DEBUG_AAR] : BASE_AARS) {
|
|
299
|
+
if (sources.has(name)) {
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
if (available.has(name)) {
|
|
303
|
+
sources.set(name, path.join(sdk.libsDir, name))
|
|
304
|
+
} else if (name !== DEBUG_AAR) {
|
|
305
|
+
missing.push(name)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (missing.length > 0) {
|
|
309
|
+
logger.warn(`当前 SDK 没有这些 AAR,已跳过:${missing.join(', ')}`)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** @type {string[]} */
|
|
313
|
+
const unresolved = []
|
|
314
|
+
for (const module of modules) {
|
|
315
|
+
const names = MODULE_AARS[module]
|
|
316
|
+
if (!names) {
|
|
317
|
+
unresolved.push(module)
|
|
318
|
+
continue
|
|
319
|
+
}
|
|
320
|
+
for (const name of names) {
|
|
321
|
+
if (!available.has(name)) {
|
|
322
|
+
continue
|
|
323
|
+
}
|
|
324
|
+
sources.set(name, path.join(sdk.libsDir, name))
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (unresolved.length > 0) {
|
|
328
|
+
logger.warn(`未识别的模块 AAR:${unresolved.join(', ')}(若运行时报模块缺失,请在配置 modules 里补充)`)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (uts) {
|
|
332
|
+
if (!available.has(UTS_AAR)) {
|
|
333
|
+
throw new PackError(`离线 SDK 缺少 ${UTS_AAR},无法编译 UTS 插件`, EXIT.PREFLIGHT, {
|
|
334
|
+
hint: '请换用包含 utsplugin-release.aar 的 Android 离线 SDK',
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
sources.set(UTS_AAR, path.join(sdk.libsDir, UTS_AAR))
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
for (const [name, source] of sources) {
|
|
341
|
+
await copyFile(source, path.join(targetDir, name))
|
|
342
|
+
}
|
|
343
|
+
return { count: sources.size }
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* @param {string} dir
|
|
348
|
+
* @returns {Promise<string[]>}
|
|
349
|
+
*/
|
|
350
|
+
async function listLibs(dir) {
|
|
351
|
+
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
|
352
|
+
return entries.filter((entry) => entry.isFile() && /\.(aar|jar)$/i.test(entry.name)).map((entry) => entry.name)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* @param {...unknown} values
|
|
357
|
+
* @returns {string}
|
|
358
|
+
*/
|
|
359
|
+
function firstNonEmpty(...values) {
|
|
360
|
+
for (const value of values) {
|
|
361
|
+
const text = String(value ?? '').trim()
|
|
362
|
+
if (text) {
|
|
363
|
+
return text
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return ''
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* @param {string} value
|
|
371
|
+
*/
|
|
372
|
+
function sanitize(value) {
|
|
373
|
+
return value.replace(/[^\w.-]+/g, '-').replace(/^-+|-+$/g, '') || 'project'
|
|
374
|
+
}
|