在现有的原生项目中安装 Expo 模块

了解如何准备你现有的原生项目以安装和使用 Expo 模块和模块 API。


如果你的项目是一个全新的 React Native 应用 — 从一开始就主要使用 React Native 构建,那么请参阅 在现有的 React Native 项目中安装 Expo 模块 而不是本指南。

本指南提供了准备现有原生项目以安装和使用 Expo 模块和模块 API 的步骤。

¥This guide provides the steps to prepare your existing native project to install and use Expo modules and the module API.

先决条件

¥Prerequisites

以下说明可能不适用于所有项目。将 Expo 模块集成到现有项目的支持仍处于试验阶段。如果你遇到问题,在 GitHub 上创建问题

你应该有一个安装并配置了 React Native 的棕地原生项目来渲染根视图。如果你还没有这个,请按照 React Native 文档中的 与现有应用集成 指南进行操作,然后在完成步骤后返回此处。

¥You should have a brownfield native project with React Native installed and configured to render a root view. If you don't have this yet, follow the Integration with Existing Apps guide from the React Native documentation and then come back here once you have followed the steps.

安装 expo

¥Install the expo package

expo 包添加到你的项目中。确保你使用的是 与你项目中的 React Native 版本兼容的 expo 版本。

¥Add the expo package to your project. Ensure you are using a version of the expo package that is compatible with the React Native version in your project.

Terminal
npm install expo

配置原生应用

¥Configuring native apps

Android 应用

¥Android app

1

Add the following to the gradle.properties file in the android directory:

android/gradle.properties
20newArchEnabled=true
21
22hermesEnabled=true

2

Add the following to the setting.gradle file in the android directory:

android/settings.gradle
1apply from: new File(["node", "--print", "require.resolve('expo/package.json')"].execute(null, rootDir).text.trim(), "../scripts/autolinking.gradle");
2useExpoModules()
3

3

Inside the android directory, run the following command:

!!!IG1!!!

Once the above command completes, run:

!!!IG2!!!

4

(Optional) Complete the following steps if you would like to use lifecycle listeners in your app. If you do not set up lifecycle listeners, then additional setup will be required for each module that uses them.

4.1

If you already have a class that extends the Application class you can move to step 3. If you do not have it, we need to create one. Add a file called MainApplication.kt file to your android/app/src/main/java/com/<your-app-package> directory with the following content:

android/app/src/main/java/com/<my-app-package>/MainApplication.kt
1package <my.app.package>
2
3import android.app.Application
4import android.content.res.Configuration
5import com.facebook.soloader.SoLoader
6import expo.modules.ApplicationLifecycleDispatcher
7
8class MainApplication() : Application() {
9 override fun onCreate() {
10 super.onCreate()
11 ApplicationLifecycleDispatcher.onApplicationCreate(this)
12 }
13
14 override fun onConfigurationChanged(newConfig: Configuration) {
15 super.onConfigurationChanged(newConfig)
16 ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig)
17 }
18}

4.2

Register the class in the AndroidManifest.xml file.

android/app/src/main/AndroidManifest.xml
77 <application
88 android:allowBackup="true"
9 android:name=".MainApplication"
910 android:fullBackupContent="@xml/backup_rules"

4.3

If you have your own class extending Application, you can add the following. It includes calls to ApplicationLifecycleDispatcher for handling events at the application startup and during device configuration changes.

android/app/src/main/java/com/<my-app-package>/MainApplication.kt
01 override fun onCreate() {
12 super.onCreate()
3 ApplicationLifecycleDispatcher.onApplicationCreate(this)
24 }
5
6 override fun onConfigurationChanged(newConfig: Configuration) {
7 super.onConfigurationChanged(newConfig)
8 ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig)
9 }

Override onConfigurationChanged if you have not done so already.

iOS app

1

Add the following to your Podfile in the ios directory:

ios/Podfile
1# Expo requires
2require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")
3
17 config = use_native_modules!()
20 # Need to be added inside the target block
21 use_expo_modules!
22
23 config_command = [
24 'node',
25 '--no-warnings',
26 '--eval',
27 'require(require.resolve("expo-modules-autolinking", { paths: [require.resolve("expo/package.json")] }))(process.argv.slice(1))',
28 'react-native-config',
29 '--json',
30 '--platform',
31 'ios'
32 ]
33 config = use_native_modules!(config_command)

2

Open your ios directory in Xcode. From the project navigator, select your project and then select your app target under TARGETS. In Build Settings, using the search bar, search for ENABLE_USER_SCRIPT_SANDBOXING. If it is not already, set its value to No.

3

Run pod install in the ios directory.

!!!IG3!!!

You will need to do this every time you add a dependency that uses native code.

4

(Optional) Complete the following if you would like to use AppDelegate subscribers. If you do not set up AppDelegate subscribers, then additional setup will be required for each module that uses them.

ios/<MyAppProject>/AppDelegate.swift
1import ExpoModulesCore
1class AppDelegate: UIResponder, UIApplicationDelegate {
2class AppDelegate: ExpoAppDelegate {
2 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
3override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
3 return true
4super.application(application, didFinishLaunchingWithOptions: launchOptions)