注意:錯誤可能會有所不同,但是如果您在過去兩天沒有任何代碼更改的情況下進行 android build 時遇到任何錯誤
我的錯誤 - 無法安裝應用程式。錯誤:命令失敗:./gradlew app:installDebug -PreactNativeDevServerPort=8081
error Failed to install the app. Make sure you have the Android development environment set up:
Error: Command failed: ./gradlew app:installDebug
-PreactNativeDevServerPort=8081
FAILURE: Build failed with an exception.
* Where: Build file '/Users/....../node_modules/react-native-month-year-picker/android/build.gradle' line: 115
* What went wrong: A problem occurred configuring project ':react-native-month-year-picker'.
> Could not resolve all files for configuration ':react-native-month-year-picker:implementation'.
> Could not resolve com.facebook.react:react-native: .
Required by:
project :react-native-month-year-picker
> Cannot choose between the following variants of com.facebook.react:react-native:0.71.0-rc.0:
- debugVariantDefaultRuntimePublication
- releaseVariantDefaultRuntimePublication
All of them match the consumer attributes:
- Variant 'debugVariantDefaultRuntimePublication' capability com.facebook.react:react-native:0.71.0-rc.0:
uj5u.com熱心網友回復:
Android 的構建失敗是由于發布了 React Native 版本0.71.0-rc0。
注意:錯誤可能會有所不同,但如果您在過去兩天沒有任何代碼更改的情況下遇到 android 構建失敗,這將是解決方案
方法一
將此修復添加到您的android -> build.gradle檔案中,如下所示:
buildscript {
// ...
}
allprojects {
repositories {
exclusiveContent {
filter {
includeGroup "com.facebook.react"
}
forRepository {
maven {
url "$rootDir/../node_modules/react-native/android"
}
}
}
// ...
}
}
此修復程式將應用一個exclusiveContent決議規則,該規則將強制 React Native Android 庫的決議,以使用內部的node_modules
方法二
如果您的 gradle 不支持上述功能,請將其添加到您的android -> build.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())
buildscript {
// ...
}
allprojects {
configurations.all {
resolutionStrategy {
force "com.facebook.react:react-native:" REACT_NATIVE_VERSION
}
}
// ...
}
參考:修復和更新自 2022 年 11 月 4 日以來發生的 Android 構建失敗 #35210
uj5u.com熱心網友回復:
這個解決方案對我有用,反應原生版本 63.3 和 gradle 4.2.2
謝謝@Thanhal,我被困了2天。
將此添加到您的 android -> build.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())
buildscript {
// ...
}
allprojects {
configurations.all {
resolutionStrategy {
force "com.facebook.react:react-native:" REACT_NATIVE_VERSION
}
}
// ...
}
uj5u.com熱心網友回復:
添加投票答案以進行一些知識共享。
重申一下,正如@Thanhal 所發布的,解決方案和官方解釋可以在這里找到:Android build failures No matching variant of com.facebook.react:react-native:0.71.0-rc.0 was found。
出現錯誤后我需要回答的最大問題是:
在 package.json 中指定了我的 react-native 版本后,為什么我的專案仍然下載另一個 react-native 版本?
我什npm install --save-exact至曾經確保我得到正確的版本
我收到的錯誤資訊讓我更加困惑:
該類從 ~/.gradle/caches/transforms-3/9a8c596b7e1788d5bad7c80991eefff1/transformed/jetified-kotlin-stdlib-1.6.10.jar!/kotlin/Unit.class e: .../node_modules/expo-modules-加載core/android/src/main/java/expo/modules/adapters/react/permissions/PermissionsService.kt: (351, 32): 類 'kotlin.Unit' 是用不兼容的 Kotlin 版本編譯的。其元資料的二進制版本是 1.6.0,預期版本是 1.4.1。
不知何故,Kotlin 也成為了我的一個問題。
誰/什么要求最新的 react-native?
就我而言,這里的問題與我的專案使用的 react-native 版本無關。這是關于我的圖書館正在使用的東西。
直到0.71.0-rc.0. 大多數庫都將其 build.gradle 配置為參考此目錄。這是通過在庫的build.gradle 中宣告一個自定義存盤庫來完成的:
maven {
url "$rootDir/../node_modules/react-native/android"
}
但在庫的 build.gradle 檔案中,宣告了更多存盤庫,可能如下所示:
repositories {
maven {
url "$rootDir/../node_modules/react-native/android"
}
google()
mavenLocal()
mavenCentral()
}
然后,庫的依賴項宣告如下:
dependencies {
implementation 'com.facebook.react:react-native: '
}
因為“ ”作為 react-native 依賴的版本,Gradle 將從各種宣告的存盤庫中獲取最新的 react-native 版本。
由于過去 react-native 是隨 npm 包一起提供的,Gradle 將始終采用最新的 react-native in node_modules. 但是,既然 react-native 團隊正在將庫發布到包括 MavenCentral 在內的公共存盤庫,Gradle 會尊重“ ”并采用 MavenCentral 上的版本。
為什么我會收到 Kotlin 錯誤?
我的專案使用較舊版本的 react-native,并且從 0.68 版本開始,react-native 開始使用 Kotlin 版本 1.6.10(請參閱更改歷史記錄)。所以是的,react-native 版本的差異也會導致 Kotlin 錯誤。
希望這會有所幫助。
uj5u.com熱心網友回復:
此修復有效:
失敗的原因:Android 的構建失敗是由于將 React Native 版本 0.71.0-rc0 發布到 Maven,因此當 gradle 同步它選擇這個 0.71.0-rc0 版本的 react-native 而不是你當前版本的 react-native。
在不升級 react-native 版本的情況下使其作業,并通過在 build.gradle 中添加它,這可以作業(無論是否啟用 hermes,以及鰭狀肢)
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 {
url "$rootDir/../node_modules/react-native/android"
}
}
}
最后的片段看起來像這樣
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 {
url "$rootDir/../node_modules/react-native/android"
}
}
}
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
在此修復后 gradle clean 和重建。然后就可以react native run android成功了。
此修復程式將應用一個exclusiveContent決議規則,該規則將強制 React Native Android 庫的決議,以使用內部的node_modules
現在,
react native 有一些針對不同版本的補丁版本,If you dont want to put this fix您可以將當前的 react native 版本更新為 react native 補丁版本,如此處所述
https://github.com/facebook/react-native/issues/35210
uj5u.com熱心網友回復:
Facebook 已發布 >=0.63 的錯誤修復版本。您也可以升級而不是應用此修補程式。
https://github.com/facebook/react-native/issues/35210
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/530126.html
上一篇:替換Gradle插件的傳遞依賴
