安装cordova-plugin-file-transfer后Android构建失败求助
Hey, let's tackle this build error you're hitting after adding the cordova-plugin-file-transfer plugin. First, let's recap your environment details to make sure we're aligned:
Ionic Environment Info
CLI Packages (C:\Users\jessica\AppData\Roaming\npm\node_modules)
@ionic/cli-utils : 1.19.2 ionic (Ionic CLI) : 3.20.0
Global Packages
cordova (Cordova CLI) : 8.0.0
Local Packages
@ionic/app-scripts : 3.0.1 Cordova Platforms : android 6.2.3 browser 5.0.1 Ionic Framework : ionic-angular 3.8.0
System
Android SDK Tools : 25.2.5 Node : v6.10.3 npm : 6.0.1 OS : Windows 8.1
Environment Variables
ANDROID_HOME : C:\Program Files (x86)\Android\android-sdk
Other
backend : legacy
The Root Cause
Looking at your build logs, the error boils down to:
error: package org.apache.cordova.file does not exist
import org.apache.cordova.file.FileUtils;
This happens because cordova-plugin-file-transfer has a hard dependency on cordova-plugin-file, but either:
- The
cordova-plugin-fileisn't installed in your project, or - You have a version mismatch between the two plugins, or between the plugins and your older Cordova/Android setup (Cordova 8.0.0 + Android 6.2.3).
Newer versions of cordova-plugin-file removed the FileUtils class that file-transfer relies on, which is why the build fails.
Step-by-Step Fix
Here's how to resolve this by installing compatible plugin versions:
Uninstall the existing file-transfer plugin (if needed)
ionic cordova plugin remove cordova-plugin-file-transferInstall a compatible version of
cordova-plugin-file
For your Cordova 8.0.0 and Android 6.2.3 setup, version 6.0.2 is stable and works with the olderFileUtilsdependency:ionic cordova plugin add cordova-plugin-file@6.0.2Reinstall a compatible
cordova-plugin-file-transferversion
Version 1.7.1 is known to work withcordova-plugin-file@6.0.2and your environment:ionic cordova plugin add cordova-plugin-file-transfer@1.7.1Clean and rebuild your Android project
To clear any cached build artifacts and ensure the plugins are properly linked:# Remove the Android platform ionic cordova platform remove android # Re-add the exact Android version you were using ionic cordova platform add android@6.2.3 # Clean build cache ionic cordova clean android # Rebuild the project ionic cordova build android
Extra Checks
- Double-check your
package.jsonto confirm both plugins are listed with the correct versions. - Verify the
platforms/android/project.propertiesfile includes a reference tocordova-plugin-file(it should be added automatically when installing the plugin).
This should resolve the missing FileUtils class error and let your build complete successfully.
内容的提问来源于stack exchange,提问作者user2828442

