cordova linux 安裝並編出第一個demo-android


 

cordova可以做到一次編寫到處運行各個平台(android、ios、wp、bb、firefoxos、web等幾乎所有平台)

手上只有一個android手機 ,安裝的時候沒有那么順利,第一大問題就是網絡問題,需要FQ

按照官方提示http://cordova.apache.org/#getstarted

1、首先安裝node.js

參考:http://www.cnblogs.com/zhishuai/articles/8138046.html

curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum -y install nodejs

 

2、安裝cordova

npm install -g cordova

 3、創建一個應用、添加android平台

cordova create MyApp 
cd MyApp
cordova platform add

備注:查看平台命令

cordova platform

 出現如下平台

Installed platforms:
  android 7.0.0
Available platforms:
  browser ~5.0.1
  ios ~4.5.4
  osx ~4.0.1
  windows ~5.0.0
  www ^3.12.0
4、編譯android版本的時候問題來了 ,提示android環境沒有安裝

cordova run android

Android Studio project detected
(node:30829) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CordovaError: Failed to find 'ANDROID_HOME' environment variable. Try setting it manually.
Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.
(node:30829) [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.
[app@localhost test]$ cordova run android
Android Studio project detected
(node:19379) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CordovaError: Failed to find 'ANDROID_HOME' environment variable. Try setting it manually.
Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.
(node:19379) [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.
[app@localhost test]$ echo $ANDROID_HOME
各種環境變量找不到
 1、java沒裝

2、android沒裝

------------------------------安裝android開發依賴環境----------------------------------------------------

 

1、安裝 java

這里下載jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jdk-8u151-linux-x64.rpm?AuthParam=1514477816_4cd93965dc16fe41a06d861aeab9a333

rpm -ivh jdk-8u151-linux-x64.rpm

 設置環境變量

vim ~/.bash_profile

 添加:

export JAVA_HOME=/usr/java/jdk1.8.0_151

 保存退出后 使環境變量生效

source ~/.bash_profile

 2、安裝android 

下載android-studio

翻牆速度有點慢 花了好一會時間 理論上國內有下載好的 之后我也會分享到我的百度雲

鏈接: https://pan.baidu.com/s/1qXTRMfq 密碼: jisc 

https://developer.android.com/studio/index.html#downloads

解壓並運行

android-studio/bin/studio.sh

啟動會自動下載 sdk ,但是默認只下載最新的sdk  我的cordova就不能用 需要手動下載對應老一個版本的sdk 。android-studio左上角有一個下載箭頭+android的一個圖標,點擊可以勾選下載對應的sdk

設置環境變量

默認下載到當前用戶下$HOME/Android/Sdk

設置環境變量

vim ~/.bash_profile

添加:

export ANDROID_HOME=$HOME/Android/Sdk

保存退出后 使環境變量生效

source ~/.bash_profile

 -----------------------------------接着cordova---------------------------------------------------------

cordova run android

 會默認下載各種環境,最后下載完之后會生成一個apk  最終生成在 工程名test/platforms/android/app/build/outputs/apk/debug/app-debug.apk 

我的輸出:

...

BUILD SUCCESSFUL in 25s
47 actionable tasks: 47 executed
Built the following apk(s):
    /home/app/src/test/platforms/android/app/build/outputs/apk/debug/app-debug.apk
ANDROID_HOME=/home/app/Android/Sdk
JAVA_HOME=/usr/java/jdk1.8.0_151
No target specified and no devices found, deploying to emulator
(node:24975) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CordovaError: No emulator images (avds) found.
1. Download desired System Image by running: /home/app/Android/Sdk/tools/android sdk
2. Create an AVD by running: /home/app/Android/Sdk/tools/android avd
HINT: For a faster emulator, use an Intel System Image and install the HAXM device driver

(node:24975) [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.

-----------------------------------------------------------------------------------------

在下載好的sdk里面有一個/home/app/Android/Sdk/platform-tools 文件夾 里面有adb命令
可以把adb添加到環境變量里面,就可以直接使用了

連上手機,進入apk目錄

adb install app-debug.apk 

就可以在手機上看到了

之后就可以在工程目錄下面有個www文件夾里面寫html+js內容了。

一次編譯 至少在android上能跑了……


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM