[英]Getting error while running `react-native run-android`
在運行我的本機代碼react-native run-android
出現錯誤。 它工作正常。 但是在從 git 和npm ci
提取新的 pull 之后,在我運行之后然后得到這個錯誤。
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
> The minCompileSdk (30) specified in a
dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties)
is greater than this module's compileSdkVersion (android-29).
Dependency: androidx.core:core:1.7.0-alpha01.
AAR metadata file: C:\Users\gauraab\.gradle\caches\transforms-2\files-2.1\9e02d64f5889006a671d0a7165c73e72\core-1.7.0-alpha01\META-INF\com\android\build\gradle\aar-metadata.properties.
正如一些人提到的,問題是 RN 構建會自動“升級”到 androidx.core:core:1.7.0-alpha01,這取決於 SDK 版本 30。
修復只是通過 build.gradle 中的 androidXCore 指定 android 核心版本
buildscript {
ext {
buildToolsVersion = "29.0.3"
minSdkVersion = 23
compileSdkVersion = 29
targetSdkVersion = 29
ndkVersion = "20.1.5948944"
kotlin_version = "1.5.0"
androidXCore = "1.5.0"
}
弄清楚這一點很痛苦。 我搜索了 gradle 文件,這些文件會像這樣自動升級包
find . -name '*.gradle' -exec grep -H "\.+" {} \;
並在node_modules/@react-native-community/netinfo/android/build.gradle
找到以下代碼段
def androidXCore = getExtOrInitialValue('androidXCore', null)
if (supportLibVersion && androidXVersion == null && androidXCore == null) {
implementation "com.android.support:appcompat-v7:$supportLibVersion"
} else {
def defaultAndroidXVersion = "1.+"
if (androidXCore == null) {
androidXCore = androidXVersion == null ? defaultAndroidXVersion : androidXVersion
}
implementation "androidx.core:core:$androidXCore"
}
}
我有同樣的問題。 就是今天。 當我嘗試在本地運行時,我得到了完全相同的錯誤。 為了讓它再次運行,我將文件 android > build.gradle 中的compileSdkVersion
和targetSdkVersion
字段從 29 更新到 30。然后,它可以再次運行。 如果這個答案適合您,您可以采用這種方式。 但是,就我個人而言,我正在尋找不改變compileSdkVersion
和targetSdkVersion
值的解決方案
更新:只需更改compileSdkVersion
顯然他們今天剛剛打破了這個。
您可以通過將以下行添加到您的應用程序級 build.gradle 文件(作為兄弟的android { }
塊上方)來修復它:
configurations.all {
resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
}
最后,Gradle 構建成功完成。 參考 https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
這個問題正是我的猜測。 因此,今天對 android 依賴項的次要版本/補丁版本的一些更新導致了這一切。
為了解決這個問題,對於build.gradle
文件中的依賴build.gradle
,如果您已將其指定為每次構建時采用最新的次要/補丁版本,請使其采用完全穩定的版本。
例如,我的 appcompact 依賴是,
implementation "androidx.appcompat:appcompat:1.+
這意味着它可以更新到 1.2.x 或 1.3.x 等。當此類版本更新發布時。 我將其更改為依賴於如下所示的確切穩定版本,
implementation "androidx.appcompat:appcompat:1.3.0"
希望這可以解決每個人的問題。
對我來說,這個問題是由react-native-netinfo
的舊version 4.0.0
,它被配置為自動獲取最新發布的androidx.core
包......所以在這種情況下,我意識到了androidx.core.core-1.7.0-alpha01
已在問題開始發生時發布。
所以為了解決這個問題,我將我的react-native-netinfo
包從4.0.0
到6.0.0
,這個問題已經為我解決了。
我只是將 compileSdkVersion=29 更改為 compileSdkVersion=30 並且我的問題解決了。 它的工作
觀察到相同的核心庫依賴會導致所有的 Gradle 構建問題。
此外, clean project
或Invalidate Caches/Restart...
選項在這里也無濟於事。
影響依賴:
'androidx.core:core-ktx:1.7.0-alpha01'
這個導致問題的依賴項,為此我在build.gradle
文件中使它穩定,然后添加了一個新行來解決依賴項沖突:
configurations.all {
resolutionStrategy {
force 'androidx.core:core-ktx:1.6.0'
}
}
使用 complie 版本的簡單解決方法
compileSdkVersion=29
更改為compileSdkVersion=30
並同步。如果您不願意更改編譯版本,那么
configurations.all {
resolutionStrategy {
force 'androidx.core:core-ktx:1.6.0'
}
}
加在上面的代碼build.gradle
內android
和應用此
implementation 'androidx.core:core-ktx:1.7.0-alpha01'
試着在你的頂部添加此build.gradle
在底部buildscript
。 這將強制任何特定版本。
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'androidx.core'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.5.0"
}
if (details.requested.group == 'androidx.core-ktx'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.5.0"
}
}
}
}
更新版本號以匹配官方模板 build.gradle 文件中顯示的版本號對我有用: https : //github.com/facebook/react-native/blob/master/template/android/build.gradle
buildToolsVersion = "30.0.2"
compileSdkVersion = 30
targetSdkVersion = 30
ndkVersion = "21.4.7075529"
您可以在此處找到有關此問題的信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.