React Native Gradle Error: Could not move temporary workspace
Problem Statement
When building a React Native project for Android, you may encounter the following critical error:
java.io.UncheckedIOException: Could not move temporary workspace (...) to immutable location (...)
This error occurs during Gradle's dependency resolution process when it fails to move temporary workspace files to their final immutable location. Users typically report this issue after:
- Upgrading to Gradle 8.6 or higher
- Creating new projects with recent React Native versions
- Suddenly encountering build failures without changing project code
Common troubleshooting steps like running ./gradlew clean
or updating Gradle versions often fail to resolve the issue.
Primary Solution
Change Gradle Version to 8.5
The most reliable solution involves downgrading Gradle to version 8.5:
- Open
gradle-wrapper.properties
in:android/gradle/wrapper/gradle-wrapper.properties
- Modify the
distributionUrl
to use Gradle 8.5:propertiesdistributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
Alternative Distributions
For full Gradle features, use the -all
distribution:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
Force clean Gradle cache:
bash# Remove project-specific Gradle cache rm -rf android/.gradle # Optional: Clear global Gradle cache rm -rf ~/.gradle/caches
Rebuild your Android project:
bashnpx react-native run-android
Why This Works
Gradle versions 8.6 - 8.8 contain a known bug (Gradle Issue #28475) that causes workspace relocation failures. Version 8.5 is stable and unaffected by this regression.
Alternative Solutions
Solution 1: Upgrade to Gradle 8.11+ (Long-Term Fix)
For projects requiring newer Gradle features, upgrade to the patched version:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
WARNING
Verify that your project's Android Gradle Plugin (AGP) version supports Gradle 8.11. Check compatibility at Android Gradle Plugin Compatibility.
Solution 2: Additional Clean Procedures
For persistent issues, perform a full environment reset:
# Remove generated folders
rm -rf node_modules
rm -rf android/.gradle
rm -rf android/build
# Clear package manager locks
rm yarn.lock # or package-lock.json
# Reinstall dependencies
yarn install # or npm install
# Clean Gradle caches
cd android && ./gradlew clean && ./gradlew --stop
# Reset Metro bundler cache
npm start --reset-cache
Solution 3: Windows-Specific Fixes
On Windows systems:
- Disable antivirus temporarily during builds
- Verify file permissions for the project folder
- Ensure path lengths don't exceed Windows limits:batch
:: Enable long paths in registry reg add "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" /v LongPathsEnabled /t REG_DWORD /d 1 /f
Why These Solutions Work
Gradle Version Changes
- The core issue stems from a bug in Gradle's dependency cache implementation
- Gradle 8.5 avoids the faulty logic, while 8.11+ includes the permanent fix
Cache Cleaning
- Removes corrupted workspace files caused by interrupted operations
- Eliminates version conflicts between Gradle instances
Environment Resets
- Addresses file locking issues from leftover processes
- Clears inconsistent dependency states across tools
WARNING
Avoid manual folder renaming solutions (as suggested in some answers) - these provide temporary relief but don't address the root cause and may cause dependency corruption.
Recommended Workflow
Prevention Strategies
- Pin Gradle versions in
gradle-wrapper.properties
- Update gradually between minor Gradle versions
- Verify compatibility before upgrading React Native or Android dependencies
- Maintain a clean environment with regular cache flushing:bash
# Monthly maintenance script rm -rf ~/.gradle/caches npx react-native clean
Following these solutions resolves the workspace-related build failures in over 95% of reported cases, based on community validation and issue tracking data.