Ubuntu环境下执行sudo ionic cordova build android命令失败求助
Hey there, let's work through this build issue step by step—since you're new to Ubuntu and Ionic/Cordova, we'll start with the most common pitfalls first.
1. Fix Permission Issues Caused by sudo
First off, using sudo to run ionic cordova commands messes up file ownership in your project directory, which often leads to weird errors like unhandled promise rejections. Let's fix the permissions first:
sudo chown -R $USER:$USER /home/tel/Desktop/AnimalTrackerApp/UserApplication
Never use sudo for Ionic/Cordova commands again unless explicitly told to (which is almost never).
2. Verify Android SDK Components
Your log shows ANDROID_HOME=/usr/lib/android-sdk, but Ubuntu's default Android SDK installation often misses critical components. Let's check and install what's needed:
- First, make sure you have permission to modify the SDK directory (so you don't need
sudofor future installs):sudo chown -R $USER:$USER /usr/lib/android-sdk - List all available/installed components:
sdkmanager --list - Install the core components (replace versions with what your project targets—check
platforms/android/app/build.gradlefortargetSdkVersion):
Note: If you get a "command not found" error forsdkmanager "platform-tools" "platforms;android-33" "build-tools;33.0.2"sdkmanager, add the SDK tools path to your environment variables (see next step).
3. Fix Environment Variable Configuration
Ubuntu doesn't always set up the Android SDK paths correctly. Open your shell config file (either ~/.bashrc for Bash or ~/.zshrc for Zsh) in a text editor:
nano ~/.bashrc
Add these lines at the end:
export ANDROID_HOME=/usr/lib/android-sdk export PATH=$PATH:$ANDROID_HOME/tools export PATH=$PATH:$ANDROID_HOME/platform-tools export PATH=$PATH:$ANDROID_HOME/build-tools/33.0.2
Save and exit (Ctrl+O, Enter, Ctrl+X), then apply the changes:
source ~/.bashrc
4. Check Gradle Setup
Cordova relies on Gradle to build Android projects, and Ubuntu's default Gradle version might be outdated or missing. Install a compatible version:
sudo apt install gradle
Then, clean the Android project's Gradle cache to avoid conflicts:
cd /home/tel/Desktop/AnimalTrackerApp/UserApplication/platforms/android gradle clean cd ../..
5. Validate build.json
Your log mentions reading build.json—if this file has incorrect signing credentials (like wrong keystore paths or passwords), it can cause build failures. If you don't need signed builds yet, temporarily rename or delete build.json to test if that's the issue.
After going through these steps, try running the build again without sudo:
ionic cordova build android
内容的提问来源于stack exchange,提问作者Gowtham Raj

