如何使用Cordova命令行界面创建签名的APK文件? [英] How to create a signed APK file using Cordova command line interface?
问题描述
我制作了一个名为checkStatus
的示例应用程序.现在,我想创建一个签名的APK文件.因此,我可以将其安装在不同的设备中进行测试.
I made a sample application named checkStatus
. Now I want to create a signed APK file. So I can install it in different devices for my testing.
为此,我用Google搜索并找到了文档.
For this, I Googled and found this documentation.
根据文档,我切换到项目目录并运行以下命令:
As per the document, I switched 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
运行上述命令后,在projectRoot/key-name.keystore
处获得了一个名为key-name.keystore
的文件.
After I ran the above command, I got a file named key-name.keystore
at projectRoot/key-name.keystore
.
然后我将该文件复制粘贴到projectRoot/platforms/android/key-name.keystore
.
And then I copy-pasted that file into projectRoot/platforms/android/key-name.keystore
.
之后,我创建了一个名为ant.properties
的文件并将其保存在projectRoot/platforms/android
中.
After that, I created a file named ant.properties
and saved it in projectRoot/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
它抛出以下错误:
/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)
所以这一次,我以如下方式修改了ant.properties
文件中的key.store
值.
So this time, I modified key.store
value in ant.properties
file like in the following way.
key.store=/home/projectRoot/platforms/android/key-name.keystore
同样,我运行了cordova build android --release
命令.它会引发相同的错误.
Again, I ran the cordova build android --release
command. It throws the same error.
谁能告诉我我做错了什么?
Can anyone tell me what I've done wrong?
推荐答案
步骤1:
D:\projects\Phonegap\Example> cordova plugin rm org.apache.cordova.console --save
添加--save
,以便从config.xml
文件中删除插件.
add the --save
so that it removes the plugin from the config.xml
file.
要生成Android的发行版,我们首先需要对Platforms/android中的AndroidManifest.xml
文件进行少量更改.编辑文件并更改行:
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">
,然后将android:debuggable
更改为false
:
<application android:debuggable="false" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
从cordova 6.2.0开始,完全删除android:debuggable标签.这是科尔多瓦的解释:
As of cordova 6.2.0 remove the android:debuggable tag completely. Here is the explanation from cordova:
"HardcodedDebugMode"类型问题的说明: 最好从清单中删除android:debuggable属性. 如果这样做,则该工具将自动插入android:debuggable = true 在模拟器或设备上构建要调试的APK时.当你 执行发布版本(例如导出APK),它将自动设置 它为假.
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.
现在我们可以告诉Cordova生成我们的发行版本:
Now we can tell cordova to generate our release build:
D:\projects\Phonegap\Example> cordova build --release android
然后,我们可以在platforms/android/ant-build
中找到我们未签名的APK文件.在我们的示例中,文件为platforms/android/ant-build/Example-release-unsigned.apk
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
注意:此Git Repo中有我们的密钥库keystoreNAME-mobileapps.keystore
,如果要创建另一个密钥库,请继续执行以下步骤.
Note : We have our keystore keystoreNAME-mobileapps.keystore
in this Git Repo, if you want to create another, please proceed with the following steps.
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
例如:
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
然后已生成名称为NAME-mobileapps.keystore的密钥库
Then the Key store has been generated with name as NAME-mobileapps.keystore
将生成的密钥库放在
旧版本的科尔多瓦
D:\projects\Phonegap\Example\platforms\android\ant-build
新版本的科尔多瓦
D:\projects\Phonegap\Example\platforms\android\build\outputs\apk
要对未签名的APK进行签名,请运行JDK中也包含的jarsigner工具:
To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename> <Unsigned APK file> <Keystore Alias name>
例如:
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'
这会将apk签名到位.
This signs the apk in place.
最后,我们需要运行zip align工具来优化APK:
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
现在,我们有了最终发行版的二进制文件example.apk,可以在Google Play商店中发布它.
Now we have our final release binary called example.apk and we can release this on the Google Play Store.
这篇关于如何使用Cordova命令行界面创建签名的APK文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!