无法在 Android 上构建和运行基于 ionic 和 cordova 的应用程序

Posted

技术标签:

【中文标题】无法在 Android 上构建和运行基于 ionic 和 cordova 的应用程序【英文标题】:Not able to build and run an ionic and cordova based app on Android 【发布时间】:2019-02-25 06:15:29 【问题描述】:

我有一个非常基本的Ionic 应用程序,我正在尝试在Win 10 机器上构建和运行它。但是当我运行此命令时出现以下错误。不确定是否缺少其他内容,因为我对 NodeJSandroid 技术还很陌生。

# ionic cordova build android
> ionic-app-scripts build --target cordova --platform android
[16:57:21]  ionic-app-scripts 3.2.0
[16:57:21]  build dev started ...
[16:57:21]  clean started ...
[16:57:21]  clean finished in 21 ms
[16:57:21]  copy started ...
[16:57:21]  deeplinks started ...
[16:57:21]  deeplinks finished in 26 ms
[16:57:21]  transpile started ...
[16:57:23]  transpile finished in 2.77 s
[16:57:23]  preprocess started ...
[16:57:23]  preprocess finished in 1 ms
[16:57:23]  webpack started ...
[16:57:24]  copy finished in 2.94 s
[16:57:26]  webpack finished in 2.57 s
[16:57:26]  sass started ...
[16:57:27]  sass finished in 913 ms
[16:57:27]  postprocess started ...
[16:57:27]  postprocess finished in 10 ms
[16:57:27]  lint started ...
[16:57:27]  build dev finished in 6.43 s
[16:57:29]  lint finished in 2.17 s
> cordova build android
Android Studio project detected
ANDROID_HOME=C:\Program Files\Android\Android Studio
JAVA_HOME=C:\Java\jdk
studio
Subproject Path: CordovaLib
Subproject Path: app

FAILURE: Build failed with an exception.

* Where:
Script 'C:\Users\nital\Documents\Tech\RESET-CODE-SAMPLES\IONIC-CODE-SAMPLES\ionic-basics\platforms\android\CordovaLib\cordova.gradle' line: 68

* What went wrong:
A problem occurred evaluating project ':CordovaLib'.
> No installed build tools found. Install the Android build tools version 19.1.0 or higher.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
(node:15236) UnhandledPromiseRejectionWarning: Error: cmd: Command failed with exit code 1 Error output:
FAILURE: Build failed with an exception.

* Where:
Script 'C:\Users\nital\Documents\Tech\RESET-CODE-SAMPLES\IONIC-CODE-SAMPLES\ionic-basics\platforms\android\CordovaLib\cordova.gradle' line: 68

* What went wrong:
A problem occurred evaluating project ':CordovaLib'.
> No installed build tools found. Install the Android build tools version 19.1.0 or higher.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
    at ChildProcess.whenDone (C:\Users\nital\Documents\Tech\RESET-CODE-SAMPLES\IONIC-CODE-SAMPLES\ionic-basics\platforms\android\cordova\node_modules\cordova-common\src\superspawn.js:169:23)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:961:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:250:5)
(node:15236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15236) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

离子信息

# ionic info
√ Gathering environment info - done!

Ionic:

   ionic (Ionic CLI)  : 4.1.2 (C:\Users\nital\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.2.0

Cordova:

   cordova (Cordova CLI) : 8.0.0
   Cordova Platforms     : android 7.0.0
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 2.1.4, (and 4 other plugins)

System:

   NodeJS : v10.9.0 (C:\Program Files\nodejs\node.exe)
   npm    : 6.2.0
   OS     : Windows 10

Cordova 要求:

# cordova requirements
Android Studio project detected

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: not installed
cmd: Command failed with exit code 1 Error output:
'android' is not recognized as an internal or external command,
operable program or batch file.
Gradle: installed C:\Program Files\Android\Android Studio\gradle\gradle-4.4\bin\gradle
(node:10252) UnhandledPromiseRejectionWarning: CordovaError: Some of requirements check failed
    at C:\Users\nital\AppData\Roaming\npm\node_modules\cordova\src\cli.js:414:27
    at _fulfilled (C:\Users\nital\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\q\q.js:787:54)
    at C:\Users\nital\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\q\q.js:816:30
    at Promise.promise.promiseDispatch (C:\Users\nital\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\q\q.js:749:13)
    at C:\Users\nital\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\q\q.js:557:44
    at flush (C:\Users\nital\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\q\q.js:108:17)
    at process._tickCallback (internal/process/next_tick.js:61:11)
(node:10252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

环境变量:

JAVA_HOME = C:\Java\jdk
GRADLE_HOME = C:\Program Files\Android\Android Studio\gradle\gradle-4.4
ANDROID_HOME = C:\Program Files\Android\Android Studio
Path =  %JAVA_HOME%\bin; %GRADLE_HOME%\bin; %ANDROID_HOME%\bin;

Java 版本信息:

# javac -version
javac 1.8.0_181

# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

我也查看了这个在线链接https://cordova.apache.org/docs/en/latest/guide/platforms/android/ 并在安装 Android Studio 后安装了所有必需的工具和插件(截图如下)

【问题讨论】:

【参考方案1】:

您的路径中缺少 android sdk 目录。

如果您查看 Cordova 需求日志,它会显示 'android' is not recognized as an internal or external command

因此,您需要从系统路径中安装 android sdk 的任何位置添加 toolsplatform-tools

【讨论】:

感谢 Sumana,我只是在查看了一些 SO 链接后添加了这一点。让我再试一次。 确保在更改路径后关闭命令提示符并打开一个新提示符 现在当我运行命令cordova requirements 命令时我得到Android target: installed android-28,android-27,android-26 然后当我运行ionic cordova build android 时我得到BUILD FAILED in 0s (node:14976) UnhandledPromiseRejectionWarning: Error: cmd: Command failed with exit code 1 Error output: FAILURE: Build failed with an exception. * Where: Script 'C:\ionic-basics\platforms\android\CordovaLib\cordova.gradle' line: 68 * What went wrong: A problem occurred evaluating project ':CordovaLib'. > No installed build tools found. Install the Android build tools version 19.1.0 or higher. 你在你的 android sdk 文件夹中看到 build-tools 文件夹中有任何文件夹吗? 您的系统环境变量中是否还有ANDROID_HOME【参考方案2】:

找到了解决办法。

所以这里有两个问题:

    ANDROID_HOME 环境变量值不正确。这不应该指向您安装 android 的位置。 AppData 是 Win 中的一个隐藏文件夹,所以你需要找到它,然后指向正确的路径

    ANDROID_HOME = C:\Users\nital\AppData\Local\Android\Sdk

    未将 PATH 环境变量指向位于 Sdk 文件夹中的 platform_toolstools 文件夹

    PATH = C:\Users\nital\AppData\Local\Android\Sdk\platform-tools

    PATH = C:\Users\nital\AppData\Local\Android\Sdk\tools

【讨论】:

以上是关于无法在 Android 上构建和运行基于 ionic 和 cordova 的应用程序的主要内容,如果未能解决你的问题,请参考以下文章

构建失败:无法在 Unity 上构建 Android .apk

Ionic实战 自动升级APP(Android版)

IOS颤振构建没有创建,但它在android上运行

Phonegap 构建应用程序在 android 上无法正常工作

OpenCV Android 应用程序(基于 Qt)在一个版本上运行,但在另一个版本上失败

基于 MAC 10.7.4 构建的 Qt 应用程序无法在 10.6 上运行