简体   繁体   中英

Cordova Android build error “Cannot read property 'length' of undefined”

I have an older cordova project that I am updating. As a part of this process, I removed the android platform and readded it.

Now when I try to build the project I get the following error:

C:\App>cordova build android --verbose
No scripts found for hook "before_build".
No scripts found for hook "before_prepare".
Checking config.xml and package.json for saved platforms that haven't been added to the project
Config.xml and package.json platforms are the same. No pkg.json modification.
Package.json and config.xml platforms are different. Updating config.xml with most current list of platforms.
PlatformApi successfully found for platform android
Checking config.xml for saved plugins that haven't been added to the project
Checking for any plugins added to the project that have not been installed in android platform
No differences found between plugins added to project and installed in android platform. Continuing...
Generating platform-specific config.xml from defaults for android at C:\App\platforms\android\res\xml\config.xml
Merging project's config.xml into platform-specific android config.xml
Found "merges/android" folder. Copying its contents into the android project.
Merging and updating files from [www, platforms\android\platform_www, merges\android] to platforms\android\assets\www
Wrote out android application name "App" to C:\App\platforms\android\res\values\strings.xml
android-versionCode not found in config.xml. Generating a code based on version in config.xml (1.0.20): 10020
Wrote out Android package name "com.nge.app" to C:\App\platforms\android\src\com\nge\bca\MainActivity.java
Updating icons at platforms\android\res
Updating splash screens at platforms\android\res
This app does not have additional resource files defined
Prepared android project successfully
No scripts found for hook "after_prepare".
No scripts found for hook "before_compile".
ANDROID_HOME=C:\Android\sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_40
Error: Cannot read property 'length' of undefined

This did work before I removed the platform so I am guessing it is a bug or a config file that changed.

There is a similar issue here: https://pt.stackoverflow.com/questions/219442/cordova-build-android-error-cannot-read-property-length-of-undefined

I found another solution by downgrading Android to version 6.1.2, since the suggestions here (and many other suggestions on various forums, like reinstalling plugins, etc.) didn't work for me:

cordova platform update android@6.1.2 --save

If that doesn't work at first hand, try the following:

  1. First remove android platform:

     cordova platform remove android 
  2. Add Android version 6.1.2:

     cordova platform add android@6.1.2 
  3. Build Android:

     cordova build android 

Here are some additional steps I took prior to above (on Mac), just to make sure:

  1. Download the latest JDK and install.

  2. Add JAVA_HOME this way:

     echo export "JAVA_HOME=\\$(/usr/libexec/java_home)" >> ~/.bash_profile 
  3. Restart shell (CMD+Q and open again)

  4. Make sure echo $JAVA_HOME equals to Android Studio -> File -> Project Structure -> JDK Location

For me, this is the following path and version:

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home

  1. Update Android Studio and all of the SDK packages

  2. Update npm

     npm update -g 

Note: On OS X 10.11 El Capitan or greater, run:

sudo npm update -g --unsafe-perm=true
  1. Update Cordova

     npm update -g cordova 

If all of this didn't work for you and you are left with no other choice, I suggest you try downgrading Cordova to version 6.4.0:

npm update -g cordova@6.4.0

You can downgrade cordova cli using

npm install -g cordova@6.4.0

and build the project with give command

ionic cordova build android

no need to add platform, it'll automatically add. If it ask to update cordova cli, then choose no

Resolved an error by following: Hi all, 在此输入图像描述 Resolved problem to run cordova run android via command prompt for cordova project attached problem screenshot. To resolve this problem removed all environment variables under Advanced System settings. Uninstalled java and installed again, downloaded old sdk tools to avoid prompt for eclipse now.

Under user variable added JAVA_HOME = C:\\Program Files\\Java\\jdk1.8.0_131

Under System variable ANDROD_HOME = C:\\cordova\\android-sdk Path = C:\\Program Files\\Android\\Android Studio2\\gradle\\gradle-3.2\\bin\\;%JAVA_HOME%\\bin to avoid error: No installed build tools found. Install the Android build tools version 19.1.0 or higher. Problem resolved now 😊

The problem was that my gradle version was 2.1 where Cordova wanted 3.2. Updating Android Studio (It was at 2.1) and making sure the environment variables are upto date was the main trick to get it to work again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM