使用NX Expo命令运行React Native项目时Android依赖解析失败
解决NX Expo运行React Native Android项目的Java版本依赖冲突问题
问题核心
用NX Expo命令启动React Native Android项目时触发依赖解析失败,核心矛盾是:
- Gradle插件
com.android.tools.build:gradle:7.4.0和react-native-gradle-plugin要求Java 11,但NX命令的运行环境用的是Java 8,导致版本不兼容。 - Android Studio能正常运行项目,说明项目本身的Java配置(build.gradle里已指定
compileOptions为Java 11)没问题,问题出在NX命令未匹配正确的JDK版本。
错误信息
> Could not determine the dependencies of null. > Could not resolve all task dependencies for configuration ':classpath'. > Could not resolve com.android.tools.build:gradle:7.4.0. Required by: project : > No matching variant of com.android.tools.build:gradle:7.4.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but: - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.0 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.0 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') > Could not resolve com.facebook.react:react-native-gradle-plugin. Required by: project : > No matching variant of project :gradle-plugin was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but: - Variant 'apiElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'mainSourceElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a component, and its dependencies declared externally: - Incompatible because this component declares a component of category 'verification' and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Doesn't say anything about its usage (required a runtime) - Variant 'runtimeElements' capability com.facebook.react:react-native-gradle-plugin:unspecified declares a runtime of a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Variant 'testResultsElementsForTest' capability com.facebook.react:react-native-gradle-plugin:unspecified: - Incompatible because this component declares a component of category 'verification' and the consumer needed a library - Other compatible attributes: - Doesn't say anything about how its dependencies are found (required its dependencies declared externally) - Doesn't say anything about its target Java version (required compatibility with Java 8) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.5') - Doesn't say anything about its usage (required a runtime)
项目build.gradle配置
apply plugin: "com.android.application" apply plugin: "com.facebook.react" android { ndkVersion rootProject.ext.ndkVersion compileSdkVersion rootProject.ext.compileSdkVersion namespace 'org.sample.svg' defaultConfig { // detox-plugin-default-config testBuildType System.getProperty('testBuildType', 'debug') testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' applicationId 'org.sample.svg' minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0.0" buildConfigField("boolean", "REACT_NATIVE_UNSTABLE_USE_RUNTIME_SCHEDULER_ALWAYS", (findProperty("reactNative.unstable_useRuntimeSchedulerAlways") ?: true).toString()) } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } }
解决步骤
统一命令行JDK版本
- 检查当前命令行Java版本:执行
java -version和./gradlew --version,确认是否为Java 11。 - 临时设置环境变量:运行NX命令前,执行
export JAVA_HOME=/path/to/your/jdk11(Linux/macOS)或set JAVA_HOME=C:\path\to\jdk11(Windows)。 - 永久配置项目级JDK:在项目根目录的
gradle.properties文件中添加org.gradle.java.home=/path/to/your/jdk11,确保Gradle始终使用Java 11。
- 检查当前命令行Java版本:执行
验证Gradle版本兼容性
- 检查项目根目录
build.gradle中的com.android.tools.build:gradle版本(当前为7.4.0),确认与gradle/wrapper/gradle-wrapper.properties中distributionUrl对应的Gradle版本(当前为7.5)匹配,两者兼容无需调整。
- 检查项目根目录
清理缓存并重新构建
- 清理Gradle缓存:执行
./gradlew clean。 - 清理NX缓存:执行
nx reset。 - 重新运行NX Expo命令启动Android项目。
- 清理Gradle缓存:执行
检查NX配置
- 查看
nx.json或workspace.json中Android任务的配置,确保未强制指定Java 8版本,保持与项目Gradle配置一致。
- 查看
内容的提问来源于stack exchange,提问作者SHA
相关产品推荐
相关产品推荐

