fix: manually generate and copy mipmap icons after cap sync

This commit is contained in:
2026-04-18 11:27:31 +08:00
parent e8e1a2bb69
commit 65ad0d8166
2 changed files with 75 additions and 1 deletions

View File

@@ -40,8 +40,82 @@ RUN npm install
COPY . .
RUN npm run build && npx cap add android && npx cap copy android && npx cap sync android
# Generate and copy custom icons to Android res/ AFTER cap sync
RUN node -e "
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
async function gen() {
const src = 'public/icons/icon.png';
// sizes for each mipmap density
const densities = [
{ suffix: 'mdpi', size: 48 },
{ suffix: 'hdpi', size: 72 },
{ suffix: 'xhdpi', size: 96 },
{ suffix: 'xxhdpi', size: 144 },
{ suffix: 'xxxhdpi', size: 192 }
];
const base = '/workspace/android/app/src/main/res';
// Generate regular icons (with white background) - these are used for API < 26
for (const d of densities) {
const dir = base + '/mipmap-' + d.suffix + '-v4';
fs.mkdirSync(dir, { recursive: true });
// ic_launcher and ic_launcher_round: full logo with white bg
const icon = await sharp(src)
.resize(d.size, d.size, { fit: 'contain', background: '#ffffff' })
.png()
.toBuffer();
fs.writeFileSync(dir + '/ic_launcher.png', icon);
fs.writeFileSync(dir + '/ic_launcher_round.png', icon);
// foreground for adaptive icon (transparent bg, scaled 4x)
const fgSize = d.size * 4;
const logo = await sharp(src)
.resize(fgSize, fgSize, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.png()
.toBuffer();
fs.writeFileSync(dir + '/ic_launcher_foreground.png', logo);
}
// Create mipmap-anydpi-v26 (adaptive icon config)
const v26Dir = base + '/mipmap-anydpi-v26';
fs.mkdirSync(v26Dir, { recursive: true });
const adXml = \`<?xml version=\"1.0\" encoding=\"utf-8\"?>
<adaptive-icon xmlns:android=\"http://schemas.android.com/apk/res/android\">
<background android:drawable=\"@color/ic_launcher_background\"/>
<foreground android:drawable=\"@mipmap/ic_launcher_foreground\"/>
</adaptive-icon>\`;
fs.writeFileSync(v26Dir + '/ic_launcher.xml', adXml);
fs.writeFileSync(v26Dir + '/ic_launcher_round.xml', adXml);
// Color for background
const valuesDir = base + '/values';
fs.mkdirSync(valuesDir, { recursive: true });
fs.writeFileSync(valuesDir + '/colors.xml',
'<?xml version=\"1.0\" encoding=\"utf-8\"?><resources><color name=\"ic_launcher_background\">#FFFFFF</color></resources>');
// Create drawable for foreground on API < 26 (use same as mipmap)
const drawDir = base + '/drawable';
fs.mkdirSync(drawDir, { recursive: true });
const fgDraw = await sharp(src)
.resize(48, 48, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
.png()
.toBuffer();
fs.writeFileSync(drawDir + '/ic_launcher_foreground.png', fgDraw);
console.log('Icons copied successfully!');
}
gen().catch(e => { console.error(e); process.exit(1); });
"
# Patch version to 1.0.1
RUN sed -i 's/versionName "1.0.0"/versionName "1.0.1"/' /workspace/android/app/build.gradle
RUN sed -i 's/versionName "1.0"/versionName "1.0.1"/' /workspace/android/app/build.gradle
WORKDIR /workspace/android
RUN chmod +x gradlew