Cordova创建项目、编译、安装
一、准备工作
1.操作系统:ubuntu18.04
2.安装Java Development Kit (JDK) 8
3.安装gradle,如果有一开始就搭建好android studio的开发环境,那么可以共享它的gradle
4.安装好android SDK
5.设置好环境变量,如:
~$ sudo vim /etc/profile
- 1
打开环境变量配置文件后,添加如下信息:
export JAVA_HOME=/home/kyun/Documents/jdk1.8.0_221
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export ADB_HOME=/home/kyun/Android/Sdk/platform-tools
export PATH=$ADB_HOME:$PATH
export ANDROID_HOME=/home/kyun/Android/Sdk
export ANDROID_SDK_ROOT=/home/kyun/Android/Sdk
export GRADLE_HOME=/home/kyun/.gradle/wrapper/dists/gradle-4.6
export PATH=$GRADLE_HOME/bin:$PATH
export PATH=$ANDROID_HOME:$PATH
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
~$source /etc/profile //让配置生效
- 1
二、安装Cordova
1、安装cordova
~$sudo npm install -g cordova
- 1
2、创建项目
(1)创建项目目录
~$ mkdir -p ~/Desktop/Cordova/sample
- 1
(2)使用cordova命令创建cordova项目
创建命令:cordova create [文件名] [包名]
~$ sudo cordova create ~/Desktop/Cordova/sample io.cordova.hellocordova
- 1
3、添加android平台
(1)检查是否有android平台
命令要在cordova创建的项目下执行。
~/Desktop/Cordova/sample$ cordova platform
Available platforms: browser ^6.0.0
electron ^1.0.0
ios ^5.0.0
osx ^5.0.0
windows ^7.0.0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
(2)添加android平台
如果(1)检查发现没有相应的平台,那么添加就会失败。
~$ sudo cordova platform add android
- 1
三、编译项目
1、检查编译条件是否具备
~/Desktop/Cordova/sample$ cordova requirements
Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: installed android-29,android-28,android-27,android-26,android-25,android-24,android-23,android-22,android-19
Gradle: installed /home/kyun/.gradle/wrapper/dists/gradle-4.6/bin/gradle
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2、从上面可知条件具备,可以开始编译
~/Desktop/Cordova/sample$ cordova build android
或
~/Desktop/Cordova/sample$ cordova build
- 1
- 2
- 3
3、编译结果
编译结束时在以下目录可以看到debug版本的apk:
/home/kyun/Desktop/Cordova/sample/platforms/android/app/build/outputs/apk/debug
- 1
4、安装调试包
使用adb命令把apk安装到手机上运行
~/home/kyun/Desktop/Cordova/sample/platforms/android/app/build/outputs/apk/debug$adb install debug.apk
- 1
最快的安装测试方式:(1)用USB数据线连接手机和手提电脑;(2)把手机的调试模式打开;(3)在Cordova项目的根目录下,运行命令:
~/home/kyun/Desktop/Cordova/sample$ cordova run android
- 1
四、cordova打release包
1、准备签名证书
签名证书有两种:一种是 .keystore,这是Eclipse 打包生成的签名;另一种是.jks ,这是Android studio 生成的签名。我们这里生成后缀名为jks的签名证书。
打开命令终端,执行以下命令:
~$ keytool -genkey -v -keystore ~/Desktop/cordova.jks -alias cordova_android -keyalg RSA -validity 20
Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]: kyunban wong
What is the name of your organizational unit?
[Unknown]: TCH
What is the name of your organization?
[Unknown]: Technology World What is the name of your City or Locality?
[Unknown]: Guangzhou
What is the name of your State or Province?
[Unknown]: Guangdong
What is the two-letter country code for this unit?
[Unknown]: cn
Is CN=kyunban wong, OU=TCH, O=Technology World, L=Guangzhou, ST=Guangdong, C=cn correct?
[no]: yes
Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 20 days
for: CN=kyunban wong, OU=TCH, O=Technology World, L=Guangzhou, ST=Guangdong, C=cn
Enter key password for <cordova_android>
(RETURN if same as keystore password):
Re-enter new password:
[Storing /home/kyun/Desktop/cordova.jks]
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore /home/kyun/Desktop/cordova.jks -destkeystore /home/kyun/Desktop/cordova.jks -deststoretype pkcs12".
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
命令中每个参数含义解释
- -keystore ~/Desktop/cordova.jks: 指定生成的证书的文件名及其存放路径,要为签名证书设置密码,在上面的交互过程里已有体现。
- -alias cordova_android:指定签名证书的别名,也需要另外为其设置密码,在上面已体现。
- -keyalg RSA : 指定证书签名采用的加密算法 ,这里使用RSA,它会产生2048bit的密钥对。
- -validity 20 : 指定证书的有效期是20天
2、生成release包
~/Desktop/Cordova/sample$ cordova build --release
- 1
编译完成后我们可以得到一个还没有签名的release包:
/home/kyun/Desktop/Cordova/sample/platforms/android/app/build/outputs/apk/release/app-release-unsigned.apk
3、对release安装包进行签名
~/Desktop/Cordova/sample/platforms/android/app/build/outputs/apk/release$ jarsigner -sigalg SHA1withRSA -digestalg SHA1 -keystore ~/Desktop/cordova.jks app-release-unsigned.apk cordova_android
Enter Passphrase for keystore:
jar signed.
Warning:
The signer's certificate is self-signed.
- 1
- 2
- 3
- 4
- 5
- 6
虽然apk的文件名称没有改变, 但其实是已经签名了的
4、安装release包
~/Desktop/Cordova/sample/platforms/android/app/build/outputs/apk/release$ adb install app-release-unsigned.apk
Performing Streamed Install
Success
- 1
- 2
- 3
谢谢阅读!
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/100749396
- 点赞
- 收藏
- 关注作者
评论(0)