configureAppIcon
从一张源图生成 Android 各密度 mipmap-* 应用图标。
前置依赖
| 参数名 | 类型 | 说明 |
|---|---|---|
deps.existsSync | FileSystem['existsSync'] | 校验源图存在 |
deps.readFileSync / writeFileSync | 读写文件 | 读源图、写各密度 PNG |
deps.mkdirSync | FileSystem['mkdirSync'] | 创建 mipmap 目录 |
deps.join | Path['join'] | 路径拼接 |
deps.sharp | Sharp | 缩放 PNG |
deps.Buffer | BufferConstructor | 二进制缓冲 |
环境要求
- Node.js + sharp peer 依赖
函数签名
typescript
function configureAppIconAndroid(
imagePath: string,
projectPath: string,
moduleDir: string,
deps: ConfigureAppIconAndroidDeps,
): Promise<ConfigureAppIconAndroidResult>参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
imagePath | string | 是 | 源图路径,建议 ≥512px 正方形 PNG |
projectPath | string | 是 | Android 工程根目录 |
moduleDir | string | 是 | 模块名,如 app |
deps | ConfigureAppIconAndroidDeps | 是 | fs / path / sharp |
返回值
| 类型 | 说明 |
|---|---|
ConfigureAppIconAndroidResult | { success, error?, logs[] } |
成功时为 ldpi~xxxhdpi 共 6 档尺寸写入 res/mipmap-{density}/ic_launcher.png(以源码为准)。
工作原理
- 校验
imagePath存在并读入 Buffer。 - 按
ANDROID_ICON_SIZES用 sharp 缩放为各密度尺寸。 - 写入
{projectPath}/{moduleDir}/src/main/res/mipmap-{density}/。 - 失败返回
success: false与 error,不抛异常。