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。你可以直接查看它的 documentation,或者参考 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

  • 带有应用源代码存档

    ¥With an app source code archive

使用预构建的二进制文件

¥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.

注意

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)

如果你尚未使用 Parcel 或 Webpack 等打包程序,则可以将应用打包到 asar 存档中,以提高在 Windows 等平台上读取文件的性能,而不是通过复制所有源文件来发布应用。

¥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 等工具编辑其图标和其他信息。

    ¥Windows: You can rename electron.exe to any name you like, and edit its icon and other information with tools like rcedit.

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

    ¥Linux: You can rename the electron executable to any name you like.

  • 苹果系统:你可以将 Electron.app 重命名为你想要的任何名称,并且还必须重命名以下文件中的 CFBundleDisplayNameCFBundleIdentifierCFBundleName 字段:

    ¥macOS: You can rename Electron.app to any name you want, and you also have to rename the CFBundleDisplayName, CFBundleIdentifier and CFBundleName fields in the following files:

    • Electron.app/Contents/Info.plist

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

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

    ¥You can also rename the helper app to avoid showing Electron Helper in the Activity Monitor, but make sure you have renamed the helper app's executable file's name.

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

    ¥The structure of a renamed app would be like:

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

还可以通过更改产品名称并从源代码构建来重新命名 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.