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

时间:2017-07-17 17:33:23

标签: android node.js windows cordova phonegap

目标:

我现在的目标是简单地根据他们的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问题就消失了。这个解决方案在这里找到:Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK - Android

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

更新

这次我使用--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平台的版本会抛出“Error:Error”而不是“Error:无法读取未定义的属性'长度'。”

任何人都可以验证是这种情况吗?

更新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 个答案:

答案 0 :(得分:0)

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

  • 卸载Android Studio
  • 从程序文件中删除名称类似于“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提供了解决这个问题所需的关键信息。

答案 1 :(得分:0)

我在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

关闭控制台或类似vscode的ID,然后运行

phonegap run android
or 
ionic cordova run android