使用 RN 更新助手将 React Native 从 0.61.2 升级到 0.68.2 后,gradle 无法构建应用程序。出现此错误
FAILURE:构建失败并出现异常。
java.io.IOException:权限被拒绝
尝试所有react-native-webview版本,但结果是相同的。
当我从 package.json 文件中删除 "react-native-webview": "^8.0.3" 这行然后构建成功,但应用程序出现错误,因为应用程序中使用了此依赖项
低于我使用的依赖版本。
distributionUrl=https://services.gradle.org/distributions/gradle-7.3.3-all.zip
android/build.gralde
buildscript {
ext {
buildToolsVersion = "31.0.0"
minSdkVersion = 21
compileSdkVersion = 31
targetSdkVersion = 31
if (System.properties['os.arch'] == "aarch64") {
// For M1 Users we need to use the NDK 24 which added support for aarch64
ndkVersion = "24.0.8215888"
} else {
// Otherwise we default to the side-by-side NDK version from AGP.
ndkVersion = "21.4.7075529"
}
}
dependencies {
classpath("com.android.tools.build:gradle:7.0.4")
classpath 'com.google.gms:google-services:4.3.8'
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("de.undercouch:gradle-download-task:4.1.2")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
如果您从 2022 年 11 月 4 日起遇到此问题,
修复了react-native >= 0.63且低于0.67的问题
在
android/buld.gradle
文件中,
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
// ...
}
allprojects {
repositories {
+ exclusiveContent {
+ // We get React Native's Android binaries exclusively through npm,
+ // from a local Maven repo inside node_modules/react-native/.
+ // (The use of exclusiveContent prevents looking elsewhere like Maven Central
+ // and potentially getting a wrong version.)
+ filter {
+ includeGroup "com.facebook.react"
+ }
+ forRepository {
+ maven {
+ // NOTE: if you are in a monorepo, you may have "$rootDir/../../../node_modules/react-native/android"
+ url "$rootDir/../node_modules/react-native/android"
+ }
+ }
+ }
// ...
}
}
此修复将应用一项独占内容解析规则,该规则将强制React Native Android 库的解析,以使用node_modules 内的解析规则。
将应用程序更新到 React Native v0.71.0 后,将不再需要此修复。
修复较旧的反应原生(< 0.63)
上述修复仅适用于 gradle 6.2 及更高版本。较旧的 React-Native 使用较旧的 gradle。
您可以通过查看 /android/gradle/wrapper/gradle-wrapper.properties 文件来确定您的 gradle 版本。
如果您使用的是使用 gradle 版本 6.1 或更低版本的旧版 React-Native(例如 0.63 或更早版本),则必须使用不同的解决方法,详细信息如下:#35210(评论)
2022 年 11 月 11 日更新
如果上述解决方案不适合您,请尝试此解决方案。
在
android/buld.gradle
文件中,
def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())
allprojects {
configurations.all {
resolutionStrategy {
// Remove this override in 0.66, as a proper fix is included in react-native itself.
force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
}
}
// ...
}
在
android/buld.gradle
文件中,您必须将 kotlinVersion
指定为“1.6.0”
buildscript {
ext {
//... other fields
kotlinVersion = "1.6.0" //1 <- here is solution
RNNKotlinVersion = kotlinVersion //2 <- here is solution
}
dependencies {
// kotlinVersion you have defined above dot this to koltin version // solution next line
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")//3 <- here is solution
}
}
尝试在 android 文件夹上运行此命令。
sudo ./gradlewcompileDebugKotlin
这对我有用。
如果您不想在项目中使用,请通过卸载 lib 来删除它,而不仅仅是从 package.json 中
npm uninstall react-native-webview
如果您想使用,请将其更新到最新版本:
"react-native-webview": "^11.17.2",
iOS 版:
删除 pod 并重新安装 pod
cd ios && pod install
对于安卓: 删除构建并重建它
希望它有效!
升级你的 React Native 版本就可以了 0.63.x 至 0.63.5 0.65.x 至 0.65.3 0.66.x 至 0.66.5 0.68.x 至 0.68.5 0.69.x 至 0.69.7 0.70.x 至 0.70.5
您可以在 android/build.gradle 文件中尝试以下设置
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 21
compileSdkVersion = 31
targetSdkVersion = 31
kotlin_version = '1.6.10'
}
repositories {
mavenCentral()
google()
jcenter()
maven { url "https://dl.bintray.com/android/android-tools/" }
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
classpath 'com.google.gms:google-services:4.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// Add the Crashlytics Gradle plugin (be sure to add version
// 2.0.0 or later if you built your app with Android Studio 4.1).
// classpath 'com.google.firebase:firebase-crashlytics-gradle:2.3.0'
// classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
}
}
allprojects {
repositories {
exclusiveContent {
// We get React Native's Android binaries exclusively through npm,
// from a local Maven repo inside node_modules/react-native/.
// (The use of exclusiveContent prevents looking elsewhere like Maven Central
// and potentially getting a wrong version.)
filter {
includeGroup "com.facebook.react"
}
forRepository {
maven {
// NOTE: if you are in a monorepo, you may have "$rootDir/../../../node_modules/react-native/android"
url "$rootDir/../node_modules/react-native/android"
}
}
}
mavenCentral()
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../../../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../../../node_modules/jsc-android/dist")
}
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
我使用的是react-native版本0.64.0,然后基于this问题,我升级到0.64.4,现在一切都很好。
这是问题解决方案中有用的部分:
📢 >= 0.63 的补丁我们已经为所有主要版本准备了版本 带修补程序的 React-native 版本:
🛳0.70.5: https://github.com/facebook/react-native/releases/tag/v0.70.5🛳️ 0.69.7:https://github.com/facebook/react-native/releases/tag/v0.69.7🛳0.68.5: https://github.com/facebook/react-native/releases/tag/v0.68.5🛳️ 0.67.5:https://github.com/facebook/react-native/releases/tag/v0.67.5🛳️0.66.5: https://github.com/facebook/react-native/releases/tag/v0.66.5🛳️ 0.65.3:https://github.com/facebook/react-native/releases/tag/v0.65.3🛳️0.64.4: https://github.com/facebook/react-native/releases/tag/v0.64.4🛳️ 0.63.5:https://github.com/facebook/react-native/releases/tag/v0.63.5
通过更新到这些补丁版本,您的 Android 版本应该可以启动 又开始工作了。
为此,请在 package.json 中将react-native 的版本更改为 相关的新补丁(例如,如果您使用的是 0.64.3,请更改为 0.64.4)并且 运行纱线安装。不需要进行其他更改,但您可能会 想要用 cd android && ./gradlew 清理你的 Android 工件 在尝试重新运行您的 Android 应用程序之前先进行清理。