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升级到最新版本
2万+

被折叠的 条评论
为什么被折叠?



