如何使用 Cordova 命令行界面创建签名的 APK 文件? [英] How to create a signed APK file using Cordova command line interface?

查看:29
本文介绍了如何使用 Cordova 命令行界面创建签名的 APK 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个名为 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.

为此,我在谷歌上搜索并找到了这个文档.

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 生成发布版本,我们首先需要对平台/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 标签.这是cordova的解释:

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 中有我们的 keystore 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

将生成的keystore放入

Place the generated keystore in

旧版科尔多瓦

D:\projects\Phonegap\Example\platforms\android\ant-build

新版本科尔多瓦

D:\projects\Phonegap\Example\platforms\android\build\outputs\apk

要签署未签名的 APK,请运行 jarsigner 工具,该工具也包含在 JDK 中:

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

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 

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆