on a fresh install of PhoneGap, "phonegap run android" results in "Error: Cannot read property 'length' of undefined"

on a fresh install of PhoneGap, "phonegap run android" results in "Error: Cannot read property 'length' of undefined"

objective:

此时我的目标是基于他们的 hello-world 模板简单地创建一个新的 phonegap 应用程序,并在我的 Windows PC 上的 Android 模拟器中对其进行测试。在此过程中,我 运行 遇到了很多问题,直到现在,我已经解决了其中的大部分问题。我是 node.js 的新手,这是我第一次尝试使用 phonegap;我正在尝试使用此模板作为开始的方式。

这个网站上似乎有两个类似的问题,但我无法从其中任何一个中提取有效的解决方案:

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

首先,我将准确解释我正在输入的内容以及我收到的回复。然后,我将提供有关我的环境的更多详细信息,以及我为实现这一目标所采取的步骤。

命令:

$ phonegap run android

回复:

[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

甚至没有任何迹象表明此错误发生的位置,这使得在充满我以前从未见过的代码的库中进行追踪变得特别困难。

示例应用程序:

此时我什至还没有完成任何编码。我尝试 运行 的应用程序是使用此命令生成的:

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

我的环境:

Windows 7 专业 x64 SP1

Java SDK 8u131(因为7u79太旧,我不得不安装它。)我还必须手动配置JAVA_HOME和ANDROID_HOME环境变量。

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(通过 npm 安装)

cordova 6.5.0(phonegap 的一部分)

cordova android 平台 6.2.2(通过 phonegap 更新,因为旧版本适合缺失的 gradle;有了这个,gradle 问题就消失了。那个解决方案在此处找到:)

Android Studio 2.3.3(这是我 Android SDK tools screen capture 的 link)

更新:

这次我使用 --verbose 标志执行了相同的命令:

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)

这是 gradle 中的错误吗?

更新 2:

由于 PhoneGap 本质上 Cordova,我决定放弃 phonegap 包并安装 cordova。不出所料,结果非常相似。

$ 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

回答我之前的问题:不,这似乎不是 gradle 中的错误。它似乎是 cordova android 平台中的一个错误,它会检查是否安装了 gradle——所以 gradle 甚至还没有被调用。

我怀疑这是因为 6.2.2 之前的 cordova android 平台版本将抛出“错误:错误”而不是“错误:无法读取未定义的 属性 'length'” ".

谁能证实是这样的?

更新 3:

在这里尝试新命令:

$ 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

它说 Gradle 没有安装,但我可以验证它是:

$ 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

我也试过设置 GRADLE_HOME 和 GRADLE_PATH 环境变量,但无济于事。

原来这个问题的解决方案是这样的:

  • 卸载Android工作室
  • 从 Program Files 中删除任何名称类似于 "Android Studio"
  • 的文件夹
  • 重新安装 Android Studio

问题的原因是构建脚本正在寻找明显硬编码的路径 "C:\Program Files\Android\Android Studio\gradle",以找出安装了哪些版本的 gradle。

但我最近在我的机器上升级到新版本的 Android Studio,安装程序将新版本放在不同的文件夹中:"C:\Program Files\Android\Android Studio1" 而不是 "C:\Program Files\Android\Android Studio"。

我可以将 gradle 文件夹从 "Android Studio1" 文件夹复制到 "Android Studio" 文件夹。这将是一个可行的快速修复,但我将无法利用 Android Studio 中 gradle 的自动更新。 "uninstall/delete/install" 方法更可靠。

这是我提交给 Apache 的 Cordova 项目的工单: https://issues.apache.org/jira/browse/CB-13104 Nikita Matrosov 提供了解决这个问题所需的关键信息。

我在 ionic 中遇到了这个错误,通过在 windows 7

中设置 gradle 路径解决了这个问题

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

路径=%GRADLE_HOME%\bin;%路径% 然后打开新控制台并通过此命令检查它是否工作

gradle -v

像 vscode 那样关闭你的控制台或你的 id,然后 运行

phonegap run android
or 
ionic cordova run android