Skip to content

Cordova

Meteor 允许开发者使用 HTML、CSS 和 JavaScript 等 Web 技术构建移动应用,同时还可以访问原生移动功能。此集成与 Apache Cordova 一起进行。

¥Meteor allows developers to build mobile applications using web technologies like HTML, CSS, and JavaScript, while also accessing native mobile capabilities. This integration is made with Apache Cordova.

Cordova 应用在 Web 视图中运行,就像没有 UI 的浏览器一样。不同的浏览器引擎对 Web 标准的实现和支持各不相同。这意味着你的应用使用的 Web 视图会极大地影响其性能和可用功能。(有关跨浏览器和版本支持的功能的详细信息,请查看 caniuse.com。)

¥Cordova apps run in a web view, which is like a browser without the UI. Different browser engines have varying implementations and support for web standards. This means the web view your app uses can greatly affect its performance and available features. (For details on supported features across browsers and versions, check caniuse.com.)

有一个 Meteor Cordova 指南 可用,它为 Meteor Cordova 项目提供高级配置详细信息。在我们更新新文档中的信息时,请随时参考它。

¥There is a Meteor Cordova guide available that offers advanced configuration details for Meteor Cordova projects. Feel free to refer to it while we update the information in the new documentation.

本节将总结为 Meteor Cordova 开发设置环境、管理开发和为商店上传生成原生工件所需的步骤。

¥This section will summarize the steps needed to set up your environment for Meteor Cordova development, manage development, and generate native artifacts for store uploads.

安装前

¥Pre-Installation

在开始之前,请确保你的开发环境满足以下要求:

¥Before you begin, make sure your development environment meets the following requirements:

Android

Java

对于 Android 开发,Cordova 需要 JDK。

¥For Android development, Cordova requires the JDK.

sh
# On Debian/Ubuntu:
sudo apt-get update
sudo apt-get install openjdk-17-jdk

# On Mac OSX
brew install openjdk@17
sudo ln -sfn $(brew --prefix)/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

# using sdkman
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 17
sdk default java 17

java -version  # Verify installation

通过将 JAVA_HOME 环境变量添加到 ~/.bashrc~/.zshrc 来确保已设置该变量:

¥Ensure JAVA_HOME environment variable is set by adding it to ~/.bashrc or ~/.zshrc :

sh
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$JAVA_HOME/bin:$PATH

运行 echo $JAVA_HOME 以检查当前 Java 版本。如果不正确,请通过查找 Java 的安装位置手动设置正确的路径。

¥Run echo $JAVA_HOME to check the current Java version. If it's incorrect, manually set the correct path by finding where Java is installed.

Android SDK

对于 Android 构建,你将需要 Android SDK。你可以通过 Android Studio 安装它。

¥For Android builds, you will need the Android SDK. You can install it via Android Studio.

安装 Android Studio 后,转到 SDK 管理器并安装所需的 SDK 包。最低要求版本为 Android SDK 34。也安装 Android SDK Command-line Tools (latest)

¥Once Android Studio is installed, go to SDK Manager and install the required SDK packages. The minimum required version is Android SDK 34. Install the Android SDK Command-line Tools (latest) as well.

通过将 ANDROID_HOME 环境变量添加到 ~/.bashrc~/.zshrc 来确保已设置该变量:

¥Ensure ANDROID_HOME environment variable is set by adding it to ~/.bashrc or ~/.zshrc :

sh
export ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_SDK_ROOT=${ANDROID_HOME}
export PATH=$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH

Gradle

如果找不到 Gradle,请使用以下命令安装它:

¥If Gradle cannot be found install it with:

sh
# On Mac OSX:
brew install gradle

# On Debian/Ubuntu:
sudo apt-get install gradle

# using sdkman
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install gradle 8.7

gradle --version  # Verify installation

iOS

对于 iOS 开发,你将需要 Xcode(仅限 macOS)。

¥For iOS development, you will need Xcode (macOS only).

从 App Store 安装 Xcode

¥Install Xcode from the App Store.

安装后,请确保已安装命令行工具:

¥After installing, ensure that the command-line tools are installed:

sh
xcode-select --install

下载和安装完成后,你需要接受许可协议。当你第一次打开 Xcode 时,将出现一个对话框,其中包含供你查看和接受的协议。然后,你可以关闭 Xcode。或者在命令行上使用下一个命令。

¥Once the download and installation are finished, you'll need to accept the license agreement. When you open Xcode for the first time, a dialog will appear with the agreement for you to review and accept. You can then close Xcode. Or use the next command on the command line.

sh
sudo xcodebuild -license accept

另外,安装 CocoaPods,它是管理 iOS 项目依赖所必需的:

¥Also, install CocoaPods, which is needed to manage iOS project dependencies:

sh
sudo gem install cocoapods

开发

¥Development

设置好所有先决条件后,你可以快速运行移动项目。

¥Once you have all the prerequisites set up, you can quickly get a mobile project running.

添加平台

¥Add platforms

要开发移动应用,你需要为 Cordova 添加平台(iOS 和 Android):

¥To develop a mobile app, you need to add the platforms (iOS and Android) for Cordova:

sh
# Android
meteor add-platform android

# iOS (only works on macOS)
meteor add-platform ios

运行模拟器

¥Run emulator

你现在可以使用 meteor run 命令在开发模式下运行应用:

¥You can now run the application in development mode using the meteor run command:

sh
# Android
meteor run android

# iOS (only works on macOS)
meteor run ios

运行物理设备

¥Run physical device

要在物理设备上运行,请确保设备通过 USB 连接:

¥To run on a physical device, ensure the device is connected via USB:

sh
# Android
meteor run android-device

# iOS (only works on macOS)
meteor run ios-device

打开 IDE

¥Open IDE

使用 Cordova 设置 Meteor 项目后,你可能希望直接使用 Android Studio 或 XCode 运行或调试移动应用。这对于高级调试、自定义配置或访问特定平台工具非常有用

¥Once you have set up your Meteor project with Cordova, you may want to run or debug your mobile app using Android Studio or XCode directly. This can be useful for advanced debugging, custom configurations, or accessing specific platform tools

在 Android Studio 中打开

¥Open in Android Studio

  1. 打开 Android Studio

    ¥Open Android Studio

  2. 单击 "打开现有的 Android Studio 项目"

    ¥Click on "Open an existing Android Studio project"

  3. 导航到你的 Meteor 项目目录:.meteor/local/cordova-build/platforms/android/

    ¥Navigate to your Meteor project directory:
    .meteor/local/cordova-build/platforms/android/

  4. 打开项目

    ¥Open the project

现在你可以使用 Android Studio 管理你的应用,包括连接到物理设备或模拟器、查看代码、使用调试工具等。

¥Now you can manage your app with Android Studio, including connecting to physical devices or emulators, reviewing code, using debugging tools, and more.

在 XCode 中打开

¥Open in XCode

  1. 打开 XCode

    ¥Open XCode

  2. 导航到 Meteor 项目目录:.meteor/local/cordova-build/platforms/ios/

    ¥Navigate to the Meteor project directory:
    .meteor/local/cordova-build/platforms/ios/

  3. 打开项目或 .xcworkspace 文件

    ¥Open the project or the .xcworkspace file

现在,你可以使用 XCode 管理你的应用,包括连接到物理设备或模拟器、查看代码、使用调试工具等。

¥Now you can manage your app with XCode, including connecting to physical devices or emulators, reviewing code, using debugging tools, and more.

生产

¥Production

构建

¥Build

开发完成后,你需要构建实际的移动应用(Android 的 APK/AAB 或 iOS 的 IPA)以分发给用户或上传到应用商店。

¥Once development is complete, you’ll need to build the actual mobile application (APK/AAB for Android or IPA for iOS) to distribute to users or upload to the app stores.

sh
meteor build ../build-output --server=https://your-server-url.com

分发

¥Distribute

使用 Meteor 构建 Cordova 项目后,你可以使用 Android Studio for Android 和 Xcode for iOS 来处理签名和创建最终工件。

¥After building your Cordova project with Meteor, you can use Android Studio for Android and Xcode for iOS to handle signing and creating the final artifacts.

Android

  1. 打开 Android Studio

    ¥Open Android Studio

  2. 单击 "打开现有的 Android Studio 项目"

    ¥Click on "Open an existing Android Studio project"

  3. 导航到你的 Meteor 项目目录:./build-output/android/project

    ¥Navigate to your Meteor project directory:
    ./build-output/android/project

  4. 打开项目

    ¥Open the project

  5. 转到构建 > 生成签名包/APK

    ¥Go to Build > Generate Signed Bundle / APK

  6. 按照提示创建或使用密钥库 配置签名,并构建 APK/ABB。

    ¥Follow the prompts to create or use a keystore, configure signing, and build the APK/ABB.

  7. 使用 Google Play 控制台将 APK/ABB 上传到 Play Store

    ¥Upload the APK/ABB to Play Store using Google Play Console

iOS

  1. 打开 XCode

    ¥Open XCode

  2. 导航到 Meteor 项目目录:../build-output/ios/project

    ¥Navigate to the Meteor project directory:
    ../build-output/ios/project

  3. 打开项目或 .xcworkspace 文件

    ¥Open the project or the .xcworkspace file

  4. 在 Xcode 中配置签名

    ¥Configure Signing in Xcode

  5. 转到产品 > 存档以创建应用的存档

    ¥Go to Product > Archive to create an archive of your app

  6. 在 Organizer 窗口中,单击 Distribute App 并按照提示配置签名并导出 IPA 文件。

    ¥In the Organizer window, click Distribute App and follow the prompts to configure signing and export the IPA file.

  7. 将 IPA 文件上传到 App Store 或通过 TestFlight 分发。

    ¥Upload the IPA file to the App Store or distribute via TestFlight.