0

The objective:

My goal at this point is to simply create a new phonegap app based on their hello-world template, and test it in an Android emulator on my Windows PC. I've run into a number of snags along the way, most of which I've been able to resolve, until now. I'm a relative newcomer to node.js, and this is the first time I've ever tried using phonegap; I'm trying to use this template as a way to get started.

There seem to be two similar issues on this site, but I wasn't able to extract a working solution from either:

Cordova Android build error "Cannot read property 'length' of undefined"

https://pt.stackoverflow.com/questions/219442/cordova-build-android-error-cannot-read-property-length-of-undefined

First I'll explain exactly what I'm typing and what response I'm getting. Then I'll provide more details about my environment, and the steps I've taken to get this far.

The command:

$ phonegap run android

The response:

[phonegap] executing 'cordova run android ' ...
ANDROID_HOME=c:\android\sdk

JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131

Error: Cannot read property 'length' of undefined

There's not even any indication of where this error happened, which makes it especially difficult to track down in a library full of code I've never seen before.

The sample app:

I haven't even done any coding at this point. The app I'm trying to run was generated using this command:

phonegap create pgHello --id "com.pghello.app" --name "pgHello" --template hello-world

My environment:

Windows 7 Professional x64 SP1

Java SDK 8u131 (I had to install this because 7u79 was too old.) I also had to manually configure the JAVA_HOME and ANDROID_HOME environment variables.

npm 5.2.0:

$ npm version
{ npm: '5.2.0',
  ares: '1.10.1-DEV',
  cldr: '30.0.3',
  http_parser: '2.7.0',
  icu: '58.2',
  modules: '51',
  node: '7.9.0',
  openssl: '1.0.2k',
  tz: '2016j',
  unicode: '9.0',
  uv: '1.11.0',
  v8: '5.5.372.43',
  zlib: '1.2.11' }

phonegap 6.5.2 (installed via npm)

cordova 6.5.0 (part of phonegap)

cordova android platform 6.2.2 (updated via phonegap because the older version was having fits over a missing gradle; with this, the gradle problem went away. That solution was found here: Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK - Android)

Android Studio 2.3.3 (here's a link to my Android SDK tools screen capture)

UPDATE:

This time I executed the same command with the --verbose flag:

No scripts found for hook "before_run".

No scripts found for hook "before_prepare".

Checking config.xml for saved platforms 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:\nodejs\apps\pgHello\platforms\android\res\xml\config.xml

Merging project's config.xml into platform-specific android config.xml

Merging and updating files from [www, platforms\android\platform_www] to platforms\android\assets\www

Wrote out android application name "pgHello" to C:\nodejs\apps\pgHello\platforms\android\res\values\strings.xml

android-versionCode not found in config.xml. Generating a code based on version in config.xml (1.0.0): 10000

Wrote out Android package name "com.pghello.app" to C:\nodejs\apps\pgHello\platforms\android\src\com\pghello\app\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".

Checking config.xml for saved plugins that haven't been added to the project

ANDROID_HOME=c:\android\sdk

JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131

Error: TypeError: Cannot read property 'length' of undefined
    at Object.module.exports.check_gradle (C:\nodejs\apps\pgHello\platforms\android\cordova\lib\check_reqs.js:143:19)
    at GradleBuilder.prepEnv (C:\nodejs\apps\pgHello\platforms\android\cordova\lib\builders\GradleBuilder.js:176:23)
    at Api.module.exports.run (C:\nodejs\apps\pgHello\platforms\android\cordova\lib\build.js:152:20)
    at C:\nodejs\apps\pgHello\platforms\android\cordova\Api.js:348:43
    at _fulfilled (C:\nodejs\apps\pgHello\platforms\android\cordova\node_modules\q\q.js:854:54)
    at self.promiseDispatch.done (C:\nodejs\apps\pgHello\platforms\android\cordova\node_modules\q\q.js:883:30)
    at Promise.promise.promiseDispatch (C:\nodejs\apps\pgHello\platforms\android\cordova\node_modules\q\q.js:816:13)
    at C:\nodejs\apps\pgHello\platforms\android\cordova\node_modules\q\q.js:624:44
    at runSingle (C:\nodejs\apps\pgHello\platforms\android\cordova\node_modules\q\q.js:137:13)
    at flush (C:\nodejs\apps\pgHello\platforms\android\cordova\node_modules\q\q.js:125:13)

Is this a bug in gradle?

UPDATE 2:

Since PhoneGap essentially is Cordova, I decided to nix the phonegap package and install cordova instead. The results, perhaps unsurprisingly, are very similar.

$ npm uninstall -g phonegap
removed 873 packages in 87.918s

$ npm install -g cordova
npm WARN deprecated node-uuid@1.4.8: Use uuid module instead
C:\Users\bbale\AppData\Roaming\npm\cordova -> C:\Users\bbale\AppData\Roaming\npm\node_modules\cordova\bin\cordova
+ cordova@7.0.1
added 602 packages in 107.461s

$ cordova create pushTest com.copperfielld.kostizi.pushtest pushTest
Creating a new cordova project.

$ cd pushtest

$ cordova platform add android@6.2.3
Using cordova-fetch for cordova-android@6.2.3
Adding android project...
Creating Cordova project for the Android platform:
    Path: platforms\android
    Package: com.copperfielld.kostizi.pushtest
    Name: pushTest
    Activity: MainActivity
    Android target: android-25
Subproject Path: CordovaLib
Android project created with cordova-android@6.2.3
Discovered plugin "cordova-plugin-whitelist" in config.xml. Adding it to the project
Installing "cordova-plugin-whitelist" for android

           This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in.

Adding cordova-plugin-whitelist to package.json
Saved plugin info for "cordova-plugin-whitelist" to config.xml
--save flag or autosave detected
Saving android@~6.2.3 into config.xml file ...

$ cordova run android
ANDROID_HOME=c:\android\sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
Error: Cannot read property 'length' of undefined

$ cordova run android --verbose
No scripts found for hook "before_run".
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:\nodejs\apps\pushTest\platforms\android\res\xml\config.xml
Merging project's config.xml into platform-specific android config.xml
Merging and updating files from [www, platforms\android\platform_www] to platforms\android\assets\www
Wrote out android application name "pushTest" to C:\nodejs\apps\pushTest\platforms\android\res\values\strings.xml
android-versionCode not found in config.xml. Generating a code based on version in config.xml (1.0.0): 10000
Wrote out Android package name "com.copperfielld.kostizi.pushtest" to C:\nodejs\apps\pushTest\platforms\android\src\com\copperfielld\kostizi\pushtest\MainActivity.java
This app does not have launcher icons defined
This app does not have splash screens defined
This app does not have additional resource files defined
Prepared android project successfully
No scripts found for hook "after_prepare".
ANDROID_HOME=c:\android\sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
Error: Cannot read property 'length' of undefined

To answer my own question from earlier: no, this doesn't appear to be a bug in gradle. It seems to be a bug in the cordova android platform where it checks to see if gradle is installed--so gradle hasn't even been invoked yet.

I suspect this because versions of the cordova android platform prior to 6.2.2 will throw "Error: Error" instead of "Error: Cannot read property 'length' of undefined".

Can anyone verify that this is the case?

UPDATE 3:

Trying a new command here:

$ cordova requirements

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: installed android-26,android-25,android-24,Google Inc.:Google APIs:24,android-23,android-22,android-21
Gradle: not installed
Cannot read property 'length' of undefined
Error: Some of requirements check failed

It says Gradle is not installed, but I can verify that it is:

$ gradle -v

------------------------------------------------------------
Gradle 4.0.1
------------------------------------------------------------

Build time:   2017-07-07 14:02:41 UTC
Revision:     38e5dc0f772daecca1d2681885d3d85414eb6826

Groovy:       2.4.11
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_131 (Oracle Corporation 25.131-b11)
OS:           Windows 7 6.1 amd64

I've also tried setting GRADLE_HOME and GRADLE_PATH environment variables, to no avail.

Community
  • 1
  • 1
B. Bale
  • 86
  • 8

2 Answers2

0

It turns out the solution to this problem was this:

  • uninstall Android Studio
  • delete any folders from Program Files with names similar to "Android Studio"
  • reinstall Android Studio

The cause of the problem is that the build script was looking for an apparently hard-coded path, "C:\Program Files\Android\Android Studio\gradle", to find out what versions of gradle were installed.

But I had recently upgraded to a new version of Android Studio on my machine, and the installer put the new version in a different folder: "C:\Program Files\Android\Android Studio1" instead of "C:\Program Files\Android\Android Studio".

I could have copied the gradle folder from the "Android Studio1" folder to the "Android Studio" folder. That would have been a workable quick fix, but I would have lost the ability to take advantage of automatic updates for gradle in Android Studio. The "uninstall/delete/install" method is much more reliable.

This is the ticket I submitted to Apache's Cordova project: https://issues.apache.org/jira/browse/CB-13104 Nikita Matrosov provided the key piece of information I needed to solve this problem.

B. Bale
  • 86
  • 8
0

I had this error in ionic and solved it by setting gradle path in windows 7

GRADLE_HOME=C:\Users\myUser\.gradle\wrapper\dists\gradle-5.4.1-all\xyz..lablabla\gradle-5.4.1

path= %GRADLE_HOME%\bin;%path% and then open new console and check if it's work by this command

gradle -v

close your console or your id like vscode and then run

phonegap run android
or 
ionic cordova run android
Mohammad Reza Mrg
  • 1,552
  • 15
  • 30