现有Android项目 集成 React Native (一)

准备工作

1.Node.js (v20.15.1)

https://nodejs.org/zh-cn

2.JDK 17(exe)

https://www.oracle.com/java/technologies/downloads/?er=221886#java17

3.Android Studio (Koala)

https://developer.android.com/studio?gad_source=1&gclid=CjwKCAjw4ri0BhAvEiwA8oo6F66pSQiL0bSisK_pQVEFjrIBuHOsjZ0KN1S5o-BlTrXBzQpDbNePjBoCmBUQAvD_BwE&gclsrc=aw.ds&hl=zh-cn

开始集成

1.Android Studio 创建 空项目 RnApp(项目名)

2. RnApp项目 根目录 命令行执行 npm init

在这里插入图片描述
生成 package.json 文件
在这里插入图片描述

3.RnApp 项目根目录 命令行执行 npm add react-native

在这里插入图片描述
在这里插入图片描述
生成 node_modules 文件和package-lock.json 文件

4.RnApp 项目根目录 命令行执行 npm install @react-native/metro-config

(为了解决 此文件 丢失导致 error no Metro found
PS D:\Android\react\RnApp>))
在这里插入图片描述

5.RnApp 根目录 创建 metro.cofig.js 文件

const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

/**
 * Metro configuration
 * https://reactnative.dev/docs/metro
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

6.RnApp 项目根目录 创建 react-native.config.js 文件

(解决项目启动 找不到默认 android路径 现有项目集成路径变了)

module.exports = {
  project: {
    android: {
      sourceDir: './',
    },
  },
};

7.RnApp 项目根目录 命令行执行 npm run start

(测试 是否可以启动服务 暂时不要run android 因为 app还没配置依赖)
在这里插入图片描述

8.RnApp 项目 setting.gradle.kts 配置阿里云仓库

(解决facebook 依赖包下载失败)

maven {
setUrl("https://maven.aliyun.com/repository/google")
}
maven {
setUrl("https://maven.aliyun.com/repository/public")
}
maven {
setUrl("https://maven.aliyun.com/repository/gradle-plugin")
}

9. RnApp 项目 app/build.gradle.kts 导入远程依赖 sync gradle

//加号是为了自动下载最新0.72的
implementation("com.facebook.react:react-native:+")
//hermes 解决lib so 缺失
implementation("com.facebook.react:hermes-android:0.71.8")

10. RnApp 根目录 创建 index.js

import React from 'react';
import {AppRegistry, StyleSheet, Text, View} from 'react-native';

const HelloWorld = () => {
  return (
    <View style={styles.container}>
      <Text style={styles.hello}>Hello, World</Text>
    </View>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
  hello: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

AppRegistry.registerComponent(
  'RnApp',
  () => HelloWorld,
);

(红框中的 就是 App项目 名字 注意大小写) js代码入口
在这里插入图片描述

11.RnApp项目 创建DemoActivity 继承 Reactivity

class DemoActivity : ReactActivity() {
    /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  override fun getMainComponentName(): String = "RnApp"

  /**
   * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
   * which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
   */
  override fun createReactActivityDelegate(): ReactActivityDelegate =
      DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
}

创建 MyApplication

class MyApplication : Application(), ReactApplication {

    private val mReactNativeHost: ReactNativeHost = object : ReactNativeHost(this) {
        override fun getUseDeveloperSupport(): Boolean {
            return BuildConfig.DEBUG
        }

        override fun getPackages(): List<ReactPackage> {
            return Arrays.asList<ReactPackage>(
                MainReactPackage()
            )
        }
        override fun getJSMainModuleName(): String {
            return "index"
        }
    }

    override fun getReactNativeHost(): ReactNativeHost {
        return mReactNativeHost
    }

    override fun onCreate() {
        super.onCreate()
        SoLoader.init(this, false)
        /*if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
            // If you opted-in for the New Architecture, we load the native entry point for this app.
            load()
        }*/
    }
}

AndroidManifest.xml 声明权限

    <uses-permission android:name="android.permission.INTERNET" />

RnApp 项目 命令行 启动 npx react-native run-android

–over 简单的 现有项目集成 React-Native

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native提供了一个称为Native Modules的机制,允许您在React Native应用程序中使用原生代码。因此,您可以使用Java或Kotlin编写原生Android代码,并将其与React Native应用程序集成。以下是一些步骤: 1.创建一个新的Android项目。 2.在您的React Native项目中创建一个新的Native Module。 3.将您的原生代码添加到Android项目中。 4.编写Java或Kotlin代码来公开原生方法。 5.在React Native Native Module中使用这些方法。 6.构建并运行您的React Native应用程序。 这里是一个简单的例子,说明如何在React Native应用程序中使用原生Android模块: 1.创建一个新的Android项目Android Studio中,选择“File” > “New” > “New Module”。然后选择“Android Library”并按照向导中的说明创建一个新的Android项目。 2.在您的React Native项目中创建一个新的Native Module 在React Native项目的根目录下,运行以下命令: ``` react-native create-library MyNativeModule ``` 此命令将创建一个名为MyNativeModule的新目录。在此目录中,您可以添加一个名为MyNativeModule.java的文件。 3.将您的原生代码添加到Android项目中 将您的原生代码复制到Android项目中的src/main/java目录中。 4.编写Java或Kotlin代码来公开原生方法 在您的Java或Kotlin类中,使用@ReactMethod注释来标记要公开给React Native的原生方法。例如: ``` @ReactMethod public void showToast(String message) { Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_SHORT).show(); } ``` 5.在React Native Native Module中使用这些方法 在您的React Native应用程序中,导入MyNativeModule并调用其方法。例如: ``` import { NativeModules } from 'react-native'; const { MyNativeModule } = NativeModules; MyNativeModule.showToast('Hello, world!'); ``` 6.构建并运行您的React Native应用程序 在React Native应用程序的根目录中,运行以下命令以构建并运行您的应用程序: ``` react-native run-android ``` 这样,您就可以在React Native应用程序中使用原生Android模块了!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值