Skip to main content

应用封装

要使用 Electron 分发你的应用,你需要对其进行打包和重新命名。为此,你可以使用专用工具或手动方法。

🌐 To distribute your app with Electron, you need to package and rebrand it. To do this, you can either use specialized tooling or manual approaches.

使用工具

🌐 With tooling

有一些工具可以用来打包和分发你的 Electron 应用。我们推荐使用 Electron Forge。你可以直接查看它的 文档,或者参考 Electron 教程的 打包和分发 部分。

🌐 There are a couple tools out there that exist to package and distribute your Electron app. We recommend using Electron Forge. You can check out its documentation directly, or refer to the Packaging and Distribution part of the Electron tutorial.

手工封装

🌐 Manual packaging

如果你更喜欢手动方法,有两种方法来分发你的应用:

🌐 If you prefer the manual approach, there are 2 ways to distribute your application:

  • 使用预构建的二进制文件
  • 带有应用源代码存档

使用预构建的二进制文件

🌐 With prebuilt binaries

要手动分发你的应用,你需要下载 Electron 的预构建二进制文件。接下来,包含你的应用的文件夹应命名为 app 并放置在 Electron 的资源目录中,如以下示例所示。

🌐 To distribute your app manually, you need to download Electron's prebuilt binaries. Next, the folder containing your app should be named app and placed in Electron's resources directory as shown in the following examples.

note

Electron 预编译二进制文件的位置在下面的示例中用 electron/ 表示。

🌐 The location of Electron's prebuilt binaries is indicated with electron/ in the examples below.

macOS
electron/Electron.app/Contents/Resources/app/
├── package.json
├── main.js
└── index.html
Windows and Linux
electron/resources/app
├── package.json
├── main.js
└── index.html

然后在 macOS 上执行 Electron.app,在 Linux 上执行 electron,或在 Windows 上执行 electron.exe,Electron 就会作为你的应用启动。electron 目录将成为你分发给用户的文件夹。

🌐 Then execute Electron.app on macOS, electron on Linux, or electron.exe on Windows, and Electron will start as your app. The electron directory will then be your distribution to deliver to users.

带有应用源代码存档 (asar)

🌐 With an app source code archive (asar)

与其通过复制所有源文件来发布你的应用,你可以将应用打包成 ASAR 压缩包,以提高在 Windows 等平台上读取文件的性能,前提是你尚未使用 Parcel 或 Webpack 等打包工具。

🌐 Instead of shipping your app by copying all of its source files, you can package your app into an asar archive to improve the performance of reading files on platforms like Windows, if you are not already using a bundler such as Parcel or Webpack.

要使用 asar 存档替换 app 文件夹,你需要将存档重命名为 app.asar,并将其放在 Electron 的资源目录下,如下所示,Electron 随后会尝试读取该存档并从中启动。

🌐 To use an asar archive to replace the app folder, you need to rename the archive to app.asar, and put it under Electron's resources directory like below, and Electron will then try to read the archive and start from it.

macOS
electron/Electron.app/Contents/Resources/
└── app.asar
Windows
electron/resources/
└── app.asar

你可以在 electron/asar 仓库 中找到有关如何使用 asar 的更多详细信息。

🌐 You can find more details on how to use asar in the electron/asar repository.

使用下载的二进制文件进行品牌重塑

🌐 Rebranding with downloaded binaries

在将你的应用打包到 Electron 中后,你需要在分发给用户之前对 Electron 进行重新命名。

🌐 After bundling your app into Electron, you will want to rebrand Electron before distributing it to users.

  • Windows: 你可以将 electron.exe 重命名为你喜欢的任何名称,并使用像 rcedit 这样的工具编辑它的图标和其他信息。

  • Linux: 你可以将 electron 可执行文件重命名为你喜欢的任何名称。

  • **macOS:**你可以将 Electron.app 重命名为你想要的任何名称,同时你还需要重命名以下文件中的 CFBundleDisplayNameCFBundleIdentifierCFBundleName 字段:

    • Electron.app/Contents/Info.plist
    • Electron.app/Contents/Frameworks/Electron Helper.app/Contents/Info.plist

    你也可以重命名辅助应用,以避免在活动监视器中显示 Electron Helper,但请确保你已经重命名了辅助应用的可执行文件名称。

    重命名的应用的结构如下:

MyApp.app/Contents
├── Info.plist
├── MacOS/
│ └── MyApp
└── Frameworks/
└── MyApp Helper.app
├── Info.plist
└── MacOS/
└── MyApp Helper
note

通过更改产品名称并从源代码构建,也可以重新为 Electron 进行品牌化。要做到这一点,你需要在 args.gn 文件中设置对应产品名称(electron_product_name = "YourProductName")的构建参数,然后重新构建。

🌐 it is also possible to rebrand Electron by changing the product name and building it from source. To do this you need to set the build argument corresponding to the product name (electron_product_name = "YourProductName") in the args.gn file and rebuild.

请记住,这并不推荐,因为从源代码编译需要搭建环境,这并不简单且耗时较长。

🌐 Keep in mind this is not recommended as setting up the environment to compile from source is not trivial and takes significant time.