开发者问题收集

在全新安装的 PhoneGap 上,“phonegap run android”导致“错误:无法读取未定义的属性‘length’”

2017-07-17
728

目标:

目前我的目标是基于他们的 hello-world 模板创建一个新的 phonegap 应用,并在 Windows PC 上的 Android 模拟器中对其进行测试。在此过程中我遇到了许多障碍,到目前为止,大多数问题我都已经解决了。我是 node.js 的新手,这是我第一次尝试使用 phonegap;我试图使用此模板作为入门方式。

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

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

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

命令:

$ 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 Professional 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 SDK 中找不到 gradle 包装器。可能需要更新您的 Android SDK - Android )

Android Studio 2.3.3(这是我的 Android SDK 工具屏幕截图 的链接)

更新:

这次我使用 --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 [email protected]: Use uuid module instead
C:\Users\bbale\AppData\Roaming\npm\cordova -> C:\Users\bbale\AppData\Roaming\npm\node_modules\cordova\bin\cordova
+ [email protected]
added 602 packages in 107.461s

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

$ cd pushtest

$ cordova platform add [email protected]
Using cordova-fetch for [email protected]
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 [email protected]
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 平台版本将抛出“错误:错误”而不是“错误:无法读取未定义的属性‘长度’”。

有人可以验证这是事实吗?

更新 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 环境变量,但无济于事。

2个回答

事实证明,这个问题的解决方案是这样的:

  • 卸载 Android Studio
  • 从 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 自动更新的能力。 “卸载/删除/安装”方法更可靠。

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

B. Bale
2017-08-01

我在 ionic 中遇到了这个错误,并通过在 Windows 7 中设置 gradle 路径解决了它

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

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

gradle -v

关闭您的控制台或您的 id(如 vscode),然后运行

phonegap run android
or 
ionic cordova run android
Mohammad Reza Mrg
2019-12-25