问题
I'm having an issue setting the icon for my Electron app in two different ways:
Non-Packaged (Running the app via terminal)
My main.js does specify an 'icon' value, pointing to the icon file, but it does not apply.
Packaged (with electron-packager)
My package.json file specifies the 'icon' key, pointing to the icon file, and I have the .icns (Mac) file in the build directory. I used electron-packager to build the app, but the icon is not applied, the default electron icon is used instead.
Not sure what I'm doing wrong here, everything appears correct.
回答1:
If you mean the icon on the dock, on MAC can should use:
const app = electron.app;
const image = electron.nativeImage.createFromPath(
app.getAppPath() + "/public/YOUR_APP_IMAGE_NAME"
);
app.dock.setIcon(image);
回答2:
There is a good tutorial here:
- https://www.christianengvall.se/electron-app-icons/
Follow the steps but make sure you don't skip anything.
This is also a relevant issue on GitHub:
- https://github.com/electron-userland/electron-builder/issues/289
More links here:
- https://discuss.atom.io/t/changing-electron-app-icon-and-information/18631
回答3:
You can add this script to package.json and it works perfectly fine. Mostly its because of the path issues.
"package-mac": "electron-packager . --overwrite --platform=darwin --arch=x64 --icon=assets/icons/mac/icon.icns --prune=true --out=release-builds",
回答4:
If you come across this issue on Mac OS, it may be the icon cache that is messing things up. It was the case for me. I used the following command to clear it :
sudo rm -rfv /Library/Caches/com.apple.iconservices.store; sudo find /private/var/folders/ \( -name com.apple.dock.iconcache -or -name com.apple.iconservices \) -exec rm -rfv {} \; ; sleep 3;sudo touch /Applications/* ; killall Dock; killall Finder
Then I built the app again and this time it I had the icon I specified with electron-packager
.
来源:https://stackoverflow.com/questions/42894288/electron-cant-get-custom-icon-to-appear