如何使用cordova命令行界面创建签名的APK文件 [英] how to create signed APK file using cordova command line interface
问题描述
这是第一次使用 apache Cordova
。我做了一个示例应用程序名称为 checkStatus
。现在我想创建签名的
APK文件。因此我可以安装在不同的设备上进行测试。
根据文档,我切换到我的项目目录并运行以下命令。
keytool -genkey -v -keystore key-name.keystore -alias别名-keyalg RSA -keysize 2048 -validity 10000
运行上述命令后,我得到一个文件名为 key-name.keystore
在 projectRoot / key-name.keystore
。
然后我将该文件粘贴到 projectRoot / platforms / android / key-name.keystore
p>
之后,我创建了一个文件名为 ant.properties
并保存在 projectRoot / platforms / android
我在文件中写了以下代码
key.store = projectRoot / key-name.keystore
key.alias = myApp
之后,我运行以下命令来释放
Cordova builds android --release
它抛出以下错误
/home/projectRoot/platforms/android/cordova/node_modules/q/q.js:126
throw e;
^
命令的错误代码1:ant with args:release,-f,/ home / projectRoot / platforms / android / build.xml,-Dout.dir = ant-build,-Dgen.absolute .dir = ant-gen
错误:/ home / projectRoot / platforms / android / cordova / build:命令失败,退出代码8
在ChildProcess.whenDone(/ usr / lib / node_modules /cordova/node_modules/cordova-lib/src/cordova/superspawn.js:135:23)
在ChildProcess.EventEmitter.emit(events.js:98:17)
at maybeClose(child_process.js :753:16)
在Process.ChildProcess._handle.onexit(child_process.js:820:5)
所以这次,我修改了 ant.properties
文件中的 key.store
key.store = / home / projectRoot / platforms / android / key-name.keystore
再次执行 Cordova build android --release
命令。
任何人都可以告诉我哪里出错了?
步骤1:
D:\projects\Phonegap \Example> cordova插件rm org.apache.cordova.console --save
添加--save,使其从config.xml文件中删除插件。
步骤2:
生成Android版本,我们首先需要对平台/ android中的AndroidManifest.xml文件做一个小的更改。编辑文件并更改行:
< application android:debuggable =trueandroid:hardwareAccelerated = :icon =@ drawable / iconandroid:label =@ string / app_name>
并将android:debuggable更改为false:
$ b b:
< application android:debuggable =falseandroid:hardwareAccelerated =trueandroid:icon =@ drawable / iconandroid:label =@ string / app_name >
从cordova 6.2.0开始,完全删除android:debuggable标签。这里是cordova的解释:
解释类型HardcodedDebugMode的问题:
最好从清单中省去android:debuggable属性。
如果你这样做,那么当构建一个APK来在模拟器或设备上调试时,工具会自动插入android:debuggable = true
。当
执行发布版本(例如Exporting APK)时,它会自动将
设置为false。
在清单文件中指定一个特定的值,
然后工具将总是使用它。这可能会导致意外地发布带有调试信息的应用程序
。
步骤3:
我们可以告诉cordova生成我们的发布版本:
D:\projects\Phonegap\Example> cordova build --release android
然后,我们可以在platforms / android / ant中找到我们的unsigned APK文件-建立。在我们的示例中,文件是platforms / android / ant-build / Example-release-unsigned.apk
步骤4:
注意:我们在这个Git Repo中有我们的密钥库keystoreNAME-mobileapps.keystore,如果要创建另一个,请继续执行以下步骤。
< h2>密钥生成:
语法:
keytool -genkey - v -keystore< keystoreName> .keystore -alias< Keystore AliasName> -keyalg< Key algorithm> -keysize< Key size> - 有效性<关键有效天数>
Egs:
code> keytool -genkey -v -keystore NAME-mobileapps.keystore -alias NAMEmobileapps -keyalg RSA -keysize 2048 -validity 10000
密钥库密码? :xxxxxxx
您的名字和姓氏是什么? :xxxxxx
您的单位部门的名称是什么? :xxxxxxxx
您的组织的名称是什么? :xxxxxxxxx
您的城市或地点的名称是什么? :xxxxxxx
您所在州或省的名称是什么? :xxxxx
本单元的双字母国家/地区代码是什么? :xxx
然后生成名为NAME-mobileapps.keystore的密钥存储
步骤5:
将生成的密钥库放入
old版本cordova
D:\projects\Phonegap\Example\platforms\android\ant-build
新版本cordova
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk
要签署未签名的APK,请运行jarsigner工具,它也包含在JDK中:
语法:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore< keystorename< Unsigned APK file> <密钥库别名>
Egs:
code> D:\projects\Phonegap\Example \platforms\android\ant-build> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps.keystore示例-lease-unsigned.apk xxxxxmobileapps
OR
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps。 keystore Example-release-unsigned.apk xxxxxmobileapps
输入KeyPhrase为'xxxxxxxx'
这标志着apk到位。
步骤6:
最后,我们需要运行zip对齐工具来优化APK:
D: \\ projects\Phonegap\Example \platforms\android\ant-build> zipalign -v 4 Example-release-unsigned.apk Example.apk
或
D:\projects\Phonegap\Example \platforms\android\ant-build> C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign -v 4示例-example-unsigned.apk Example.apk
或
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign -v 4示例-example-unsigned.apk Example.apk
现在我们有最终版本的二进制文件example.apk,我们可以在Google Play Store上发布。 >
This is the first time working on
apache Cordova
. I made a sample application name ascheckStatus
. Now I want to createsigned
APK file. so I can install in different devices for my testing.For this, I Googled and found this documentation.
As per document, I switch to my project directory and ran the following command.
keytool -genkey -v -keystore key-name.keystore -alias alias-name -keyalg RSA -keysize 2048 -validity 10000
After ran the above command, I got a file name as
key-name.keystore
atprojectRoot/key-name.keystore
.And then I copy pasted that file into
projectRoot/platforms/android/key-name.keystore
.After that, I created a file name as
ant.properties
and saved it inprojectRoot/platforms/android
I wrote the following code inside the file
key.store=projectRoot/key-name.keystore key.alias=myApp
after that, I ran the following command to release
Cordova builds android --release
It's throwing following error
/home/projectRoot/platforms/android/cordova/node_modules/q/q.js:126 throw e; ^ Error code 1 for command: ant with args: release,-f,/home/projectRoot/platforms/android/build.xml,-Dout.dir=ant-build,-Dgen.absolute.dir=ant-gen Error: /home/projectRoot/platforms/android/cordova/build: Command failed with exit code 8 at ChildProcess.whenDone (/usr/lib/node_modules/cordova/node_modules/cordova-lib/src/cordova/superspawn.js:135:23) at ChildProcess.EventEmitter.emit (events.js:98:17) at maybeClose (child_process.js:753:16) at Process.ChildProcess._handle.onexit (child_process.js:820:5)
So this time, I modified
key.store
value inant.properties
file like in the following way.key.store=/home/projectRoot/platforms/android/key-name.keystore
Again ran
Cordova build android --release
command. the same error it's throwing.Can anyone tell where I went wrong?
解决方案Step 1:
D:\projects\Phonegap\Example> cordova plugin rm org.apache.cordova.console --save
add the --save so that it removes the plugin from the config.xml file.
Step 2:
To generate a release build for Android, we first need to make a small change to the AndroidManifest.xml file found in platforms/android. Edit the file and change the line:
<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
and change android:debuggable to "false":
<application android:debuggable="false" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
As of cordova 6.2.0 remove the android:debuggable tag completely. Here is the explanation from cordova:
Explanation for issues of type "HardcodedDebugMode": It's best to leave out the android:debuggable attribute from the manifest. If you do, then the tools will automatically insert android:debuggable=true when building an APK to debug on an emulator or device. And when you perform a release build, such as Exporting APK, it will automatically set it to false.
If on the other hand you specify a specific value in the manifest file, then the tools will always use it. This can lead to accidentally publishing your app with debug information.
Step 3:
Now we can tell cordova to generate our release build:
D:\projects\Phonegap\Example> cordova build --release android
Then, we can find our unsigned APK file in platforms/android/ant-build. In our example, the file was platforms/android/ant-build/Example-release-unsigned.apk
Step 4:
Note : We have our keystore 'keystoreNAME-mobileapps.keystore' in this Git Repo, if you want to create another, please proceed with the following steps.
Key Generation:
Syntax:
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
Egs:
keytool -genkey -v -keystore NAME-mobileapps.keystore -alias NAMEmobileapps -keyalg RSA -keysize 2048 -validity 10000 keystore password? : xxxxxxx What is your first and last name? : xxxxxx What is the name of your organizational unit? : xxxxxxxx What is the name of your organization? : xxxxxxxxx What is the name of your City or Locality? : xxxxxxx What is the name of your State or Province? : xxxxx What is the two-letter country code for this unit? : xxx
Then the Key store has been generated with name as NAME-mobileapps.keystore
Step 5:
Place the generated keystore in
old version cordova D:\projects\Phonegap\Example\platforms\android\ant-build
New version cordova D:\projects\Phonegap\Example\platforms\android\build\outputs\apk
To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:
Syntax:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename <Unsigned APK file> <Keystore Alias name>
Egs:
D:\projects\Phonegap\Example\platforms\android\ant-build> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps.keystore Example-release-unsigned.apk xxxxxmobileapps
OR D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore NAME-mobileapps.keystore Example-release-unsigned.apk xxxxxmobileapps
Enter KeyPhrase as 'xxxxxxxx'
This signs the apk in place.
Step 6:
Finally, we need to run the zip align tool to optimize the APK:
D:\projects\Phonegap\Example\platforms\android\ant-build> zipalign -v 4 Example-release-unsigned.apk Example.apk
OR
D:\projects\Phonegap\Example\platforms\android\ant-build> C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign -v 4 Example-release-unsigned.apk Example.apk
OR
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk> C:\Phonegap\adt-bundle-windows-x86_64-20140624\sdk\build-tools\android-4.4W\zipalign -v 4 Example-release-unsigned.apk Example.apk
Now we have our final release binary called example.apk and we can release this on the Google Play Store.
这篇关于如何使用cordova命令行界面创建签名的APK文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!