reactnative接入遇到问题

ReactNative接入踩坑

1、接入配置
rn 配置说明
1、node&watchman环境配置
brew install node
brew install watchman
2、yarn 安装
npm install -g yarn
3、rn 库安装
进入项目根目录执行:yarn add react-native
4、rn 自动链接配置

setting.gradle
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

app build.grdle
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

在gradle.kts中如下设置
setting.gradle.kts
apply(file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"))
val applyNativeModules: Closure<Any> = extra.get("applyNativeModulesSettingsGradle") as Closure<Any>
applyNativeModules(settings)

app build.gradle.kts
apply(from = "../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle")
val applyNativeModules: Closure<Any> = extra.get("applyNativeModulesAppBuildGradle") as Closure<Any>
applyNativeModules(project)

问题1、Failed to apply plugin ‘com.facebook.react’.> The value for property ‘languageVersion’ is final and cannot be changed any further.
解决方案:在build.gradle.kts中设置 ext{
set(“react.internal.disableJavaVersionAlignment”,false)
}

解决方案参考:https://github.com/facebook/react-native/issues/41580 使用方案1,JdkConfiguratorUtils中会判断是否对象java版本,不启用对齐即可
input.hasProperty(INTERNAL_DISABLE_JAVA_VERSION_ALIGNMENT

/Users/dxm/react/node_modules/@react-native/gradle-plugin/react-native-gradle-plugin/build/classes/kotlin/main/com/facebook/react/utils

internal object JdkConfiguratorUtils {

  fun configureJavaToolChains(input: Project) {
    // Check at the app level if react.internal.disableJavaVersionAlignment is set.
    if (**input.hasProperty(INTERNAL_DISABLE_JAVA_VERSION_ALIGNMENT**)) {
      return
    }
    input.rootProject.allprojects { project ->
      // Allows every single module to set react.internal.disableJavaVersionAlignment also.
      if (project.hasProperty(INTERNAL_DISABLE_JAVA_VERSION_ALIGNMENT)) {
        return@allprojects
      }
      val action =
          Action<AppliedPlugin> {
            /*project.extensions.getByType(AndroidComponentsExtension::class.java).finalizeDsl { ext
              ->
              ext.compileOptions.sourceCompatibility = JavaVersion.VERSION_17
              ext.compileOptions.targetCompatibility = JavaVersion.VERSION_17
            }*/ -> these lines out
          }
      project.pluginManager.withPlugin("com.android.application", action)
      project.pluginManager.withPlugin("com.android.library", action)
      project.pluginManager.withPlugin("org.jetbrains.kotlin.android") {
        // project.extensions.getByType(KotlinTopLevelExtension::class.java).jvmToolchain(17) -> this line out
      }
      project.pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
       // project.extensions.getByType(KotlinTopLevelExtension::class.java).jvmToolchain(17) -> this line out
      }
    }
  }
}
```(not recommended)
2. Remove these declarations for version update from your gradle file.
3. Use a different Gradle Version that does not throw this error, check here with version does not do that https://docs.gradle.org/current/userguide/upgrading_version_8.html

问题2、Build was configured to prefer settings repositories over project repository but repository ‘maven’ was added by plugin 'com.facebook.react
解决方法:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

问题3、Cannot run program “node” (in directory “/Users/dxm/react/android”): error=0

解决方法:重启电脑ok

其它:
gradle升级到8.7,androidstudio升级到最新版本

### 如何在 React Native 中接入和使用功能或第三方库 #### 接入第三方库的基础流程 为了在 React Native 应用程序中成功集成并使用第三方库,通常需要遵循以下几个方面的工作流: 1. **安装依赖包** 安装所需的 npm 或 yarn 包是第一步操作。例如,在项目根目录运行以下命令可以安装 `react-native-splash-screen` 这样的第三方库: ```bash npm install react-native-splash-screen --save ``` 如果使用的是 Yarn,则执行如下命令: ```bash yarn add react-native-splash-screen ``` 2. **链接本地模块(对于旧版本可能必要)** 对于某些需要原生支持的库,React Native 的早期版本可能需要手动链接这些模块到 Android 和 iOS 平台。尽管现代 React Native 版本已经通过自动链接机制简化了这一过程,但在一些特殊情况下仍需手动配置。可以通过以下命令尝试自动链接: ```bash npx pod-install ios && react-native link react-native-splash-screen ``` 若遇到问题,可参照官方文档或者具体库的 README 文件完成手动设置。 3. **修改原生代码** 当涉及到更复杂的交互时,比如分享、支付等功能,往往还需要调整 Android 和 iOS 原生项目的文件结构以及逻辑处理部分。以微信登录为例[^2],可以在 Android 的 `MainApplication.java` 添加初始化方法: ```java @Override public void onCreate() { super.onCreate(); SoLoader.init(this, false); Config.shareType = "react native"; UMShareAPI.get(this); // 设置平台参数 PlatformConfig.setWeixin("wx083bf496cbc48aec", "750e9075fa521c82274a9d548c399825"); } ``` 4. **编写 JavaScript 层面调用接口** 在前端层面引入已安装好的插件,并按照其 API 文档说明正确调用相应函数即可实现所需的功能特性。例如隐藏启动屏的操作如下所示[^1]: ```javascript import SplashScreen from 'react-native-splash-screen'; function App(): React.ReactElement { useEffect(() => { SplashScreen.hide(); // 调用此方法关闭启动画面 }, []); return ( <View> {/* 主应用界面 */} </View> ); } export default App; ``` 5. **测试验证** 最后一步就是充分测试各个场景下的行为表现是否符合预期目标,确保跨平台一致性良好体验效果达成最佳状态。 #### 注意事项 - 不同类型的 SDK 可能有不同的接入难度和技术要求,请提前评估好技术可行性再做决策。 - 尽量选用那些社区活跃度高且维护良好的开源组件作为首选方案之一[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值