React-Native Android无法从node_modules目录导入问题求助
Hey there, let's work through fixing this frustrating Gradle dependency issue where it's pulling React Native 0.55.3 from jcenter instead of using your forked 0.43.4 version from node_modules. Here are actionable steps to resolve this:
1. Fix Your Project-Level Gradle Repository Order
Gradle resolves dependencies in the order you list repositories, so your local React Native maven repo needs to come first. Your modified config had jcenter at the top, which is why Gradle checked there first and grabbed the latest version. Update your project-level build.gradle to this:
allprojects { repositories { // Prioritize local React Native Android repo from node_modules maven { url "$rootDir/../node_modules/react-native/android" } mavenLocal() jcenter() google() } }
This ensures Gradle checks your local node_modules repo first before falling back to remote repos.
2. Force React Native Version in App-Level Gradle
Even with correct repo order, Gradle might resolve to a newer version due to dependency conflicts. Lock down the version explicitly in your app-level build.gradle:
dependencies { // Force use of your forked 0.43.4 version implementation('com.facebook.react:react-native:0.43.4') { force = true } // ... other dependencies }
You can also add a global resolution strategy to enforce this version across all dependencies (useful if third-party libs pull newer RN versions):
configurations.all { resolutionStrategy { force 'com.facebook.react:react-native:0.43.4' } }
3. Clear Gradle Cache and Refresh Dependencies
Gradle often caches dependency resolution results, so it might not pick up your repo changes right away. Run these commands to reset:
- For macOS/Linux:
./gradlew clean build --refresh-dependencies - For Windows:
gradlew clean build --refresh-dependencies
If that doesn't work, you can manually delete your Gradle cache directory:
- macOS/Linux:
~/.gradle/caches - Windows:
C:\Users\<YourUsername>\.gradle\caches
4. Verify Your Node Modules and Lock File
Double-check that your local node_modules has the correct forked RN version:
- Delete existing modules and lock file:
rm -rf node_modules package-lock.json - Reinstall dependencies:
npm install - Confirm the correct version exists at
node_modules/react-native/android/com/facebook/react/react-native/0.43.4(this is the local maven repo Gradle uses).
5. Check for Transitive Dependency Conflicts
Third-party libraries might be pulling in a newer React Native version without you noticing. Run this command to generate a dependency tree:
./gradlew app:dependencies
Search for react-native in the output to see if any other dependency is requesting 0.55.3. If you find one, use the global resolution strategy from step 2 to force all uses to 0.43.4.
内容的提问来源于stack exchange,提问作者okarakose

