Android 如何使用 Cordova 命令行界面创建签名的 APK 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26449512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a signed APK file using Cordova command line interface?
提问by vasan
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.
我制作了一个名为checkStatus
. 现在我想创建一个签名的 APK 文件。所以我可以将它安装在不同的设备上进行测试。
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
After I ran the above command, I got a file named key-name.keystore
at projectRoot/key-name.keystore
.
之后我跑上面的命令,我得到了一个文件名为key-name.keystore
在projectRoot/key-name.keystore
。
And then I copy-pasted that file into projectRoot/platforms/android/key-name.keystore
.
然后我将该文件复制粘贴到projectRoot/platforms/android/key-name.keystore
.
After that, I created a file named ant.properties
and saved it in projectRoot/platforms/android
.
之后,我创建了一个名为的文件ant.properties
并将其保存在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
It's throwing the 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 in ant.properties
file like in the following way.
所以这一次,我像下面这样修改key.store
了ant.properties
文件中的值。
key.store=/home/projectRoot/platforms/android/key-name.keystore
Again, I ran the cordova build android --release
command. It throws the same error.
再次,我运行了cordova build android --release
命令。它抛出同样的错误。
Can anyone tell me what I've done wrong?
谁能告诉我我做错了什么?
回答by cfprabhu
Step 1:
第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.
添加--save
以便它从config.xml
文件中删除插件。
Step 2:
第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:
要为 Android 生成发布版本,我们首先需要对AndroidManifest.xml
platforms/android.conf 中的文件进行小的更改。编辑文件并更改行:
<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
and change android:debuggable
to false
:
并更改android:debuggable
为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:
从cordova 6.2.0 开始,完全删除android:debuggable 标签。这是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.
对“HardcodedDebugMode”类型问题的解释:最好从清单中省略 android:debuggable 属性。如果这样做,那么在构建 APK 以在模拟器或设备上进行调试时,这些工具将自动插入 android:debuggable=true。当您执行发布构建时,例如导出 APK,它会自动将其设置为 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:
第 3 步:
Now we can tell cordova to generate our release build:
现在我们可以告诉cordova生成我们的发布版本:
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
然后,我们可以在platforms/android/ant-build
. 在我们的示例中,文件是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.
注意:我们keystoreNAME-mobileapps.keystore
在这个 Git Repo 中有我们的密钥库,如果你想创建另一个,请继续以下步骤。
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
然后密钥库已经生成,名称为 NAME-mobileapps.keystore
Step 5:
第 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:
要签署未签名的 APK,请运行 jarsigner 工具,该工具也包含在 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.
这标志着 apk 就位。
Step 6:
第 6 步:
Finally, we need to run the zip align tool to optimize the APK:
最后,我们需要运行 zip align 工具来优化 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.
现在我们有了名为 example.apk 的最终发布二进制文件,我们可以在 Google Play 商店上发布它。
回答by Jon
An update to @malcubierre for Cordova 4 (and later)-
Cordova 4(及更高版本)对@malcubierre 的更新-
Create a file called release-signing.properties
and put in APPFOLDER\platforms\android
folder
创建一个名为的文件release-signing.properties
并放入 APPFOLDER\platforms\android
文件夹
Contents of the file: edit after = for all except 2nd line
文件内容:在 = 之后编辑除第二行之外的所有内容
storeFile=C:/yourlocation/app.keystore
storeType=jks
keyAlias=aliasname
keyPassword=aliaspass
storePassword=password
Then this command should build a release version:
然后这个命令应该构建一个发布版本:
cordova build android --release
回答by infinito84
In the current documentationwe can specify a build.json with the keystore:
在当前文档中,我们可以使用密钥库指定 build.json:
{
"android": {
"debug": {
"keystore": "..\android.keystore",
"storePassword": "android",
"alias": "mykey1",
"password" : "password",
"keystoreType": ""
},
"release": {
"keystore": "..\android.keystore",
"storePassword": "",
"alias": "mykey2",
"password" : "password",
"keystoreType": ""
}
}
}
And then, execute the commando with --buildConfig argumente, this way:
然后,使用 --buildConfig 参数执行突击队,这样:
cordova run android --buildConfig
回答by malcubierre
Step1:
第1步:
Go to cordova\platforms\android
ant create a fille called ant.properties
file with the keystore file info (this keystore can be generated from your favorite Android SDK, studio...):
去cordova\platforms\android
ant 创建一个名为ant.properties
file 的文件,其中包含密钥库文件信息(此密钥库可以从您最喜欢的 Android SDK、studio... 生成):
key.store=C:\yourpath\Yourkeystore.keystore
key.alias=youralias
Step2:
第2步:
Go to cordova path and execute:
转到cordova路径并执行:
cordova build android --release
Note: You will be prompted asking your keystore and key password
注意:系统会提示您询问您的密钥库和密钥密码
An YourApp-release.apk will appear in \cordova\platforms\android\ant-build
一个 YourApp-release.apk 将出现在 \cordova\platforms\android\ant-build
回答by KrIsHnA
In cordova 6.2.0, it has an easy way to create release build. refer to other steps here Steps 1, 2 and 4
在cordova 6.2.0 中,它有一种创建发布版本的简单方法。请参阅此处的其他步骤步骤 1、2 和 4
cd cordova/ #change to root cordova folder
platforms/android/cordova/clean #clean if you want
cordova build android --release -- --keystore="/path/to/keystore" --storePassword=password --alias=alias_name #password will be prompted if you have any
回答by Ben
On Mac (osx), I generated two .sh files, one for the first publication and another one for updating :
在 Mac (osx) 上,我生成了两个 .sh 文件,一个用于第一次发布,另一个用于更新:
#!/bin/sh
echo "Ionic to Signed APK ---- [email protected] // Benjamin Rathelot\n"
printf "Project dir : "
read DIR
printf "Project key alias : "
read ALIAS
cd $DIR/
cordova build --release android
cd platforms/android/build/outputs/apk/
keytool -genkey -v -keystore my-release-key.keystore -alias $ALIAS -keyalg RSA -keysize 2048 -validity 10000
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore android-release-unsigned.apk $ALIAS
zipalign -v 4 android-release-unsigned.apk signedApk.apk
And to update your app:
并更新您的应用程序:
#!/bin/sh
echo "Ionic to Signed APK ---- [email protected] // Benjamin Rathelot\n"
printf "Project dir : "
read DIR
printf "Project key alias : "
read ALIAS
cd $DIR/
cordova build --release android
cd platforms/android/build/outputs/apk/
rm signedApk.apk
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore android-release-unsigned.apk $ALIAS
zipalign -v 4 android-release-unsigned.apk signedApk.apk
Assuming you're in your home folder or a folder topping your apps folders. Make sure to set correctly chmod to use this script. Then :
假设您位于您的主文件夹或位于您的应用程序文件夹之上的文件夹中。确保正确设置 chmod 以使用此脚本。然后 :
./ionicToApk.sh # or whatever depending of the name of your file, in CLI
Your signed apk will be in Your App folder/platforms/android/build/outputs/apk/ as SignedApk.apk Make sure to use the correct key alias and password defined with the first script
您已签名的 apk 将在您的 App 文件夹/platforms/android/build/outputs/apk/ 中作为 SignedApk.apk 确保使用第一个脚本定义的正确密钥别名和密码
回答by Om Shankar
Build cordova release APK file in cmd.
在 cmd 中构建cordova 发布APK 文件。
KEY STORE FILE PATH: keystore file path (F:/cordova/myApp/xxxxx.jks)
密钥存储文件路径:密钥库文件的路径(F:/cordova/myApp/xxxxx.jks)
KEY STORE PASSWORD: xxxxx
密钥商店密码:xxxxx
KEY STORE ALIAS: xxxxx
主要商店别名:xxxxx
KEY STORE ALIAS PASSWORD: xxxxx
KEY STORE 别名密码:xxxxx
PATH OF zipalign.exe: zipalign.exe file path (C:\Users\xxxx\AppData\Local\Android\sdk\build-tools\25.0.2\zipalign)
路径zipalign.exe:zipalign.exe文件路径(C:\用户\ XXXX \应用程序数据\本地\ Android的\ SDK \构建工具\ 25.0.2 \的zipalign)
ANDROID UNSIGNED APK NAME: android-release-unsigned.apk
ANDROID 未签名 APK 名称:android-release-unsigned.apk
ANDROID RELEASE APK NAME: android-release.apk
ANDROID 发布 APK 名称:android-release.apk
Run below steps in cmd (run as administrator)
在 cmd 中运行以下步骤(以管理员身份运行)
- cordova build --release android
- go to android-release-unsigned.apk file location (PROJECT\platforms\android\build\outputs\apk)
- jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <KEY STORE FILE PATH> <ANDROID UNSIGNED APK NAME> <KEY STORE ALIAS>
- <PATH OF zipalign.exe> -v 4 <ANDROID UNSIGNED APK NAME> <ANDROID RELEASE APK NAME>
- 科尔多瓦构建--发布android
- 转到 android-release-unsigned.apk 文件位置(PROJECT\platforms\android\build\outputs\apk)
- jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore < KEY STORE FILE PATH> < ANDROID UNSIGNED APK NAME> < KEY STORE ALIAS>
- < zipalign.exe 的路径> -v 4 < ANDROID UNSIGNED APK NAME> < ANDROID RELEASE APK NAME>
回答by nicolsondsouza
##Generated signed apk from commandline
#variables
APP_NAME=THE_APP_NAME
APK_LOCATION=./
APP_HOME=/path/to/THE_APP
APP_KEY=/path/to/Android_key
APP_KEY_ALIAS=the_alias
APP_KEY_PASSWORD=123456789
zipalign=$ANDROID_HOME/build-tools/28.0.3/zipalign
#the logic
cd $APP_HOME
cordova build --release android
cd platforms/android/app/build/outputs/apk/release
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore $APP_KEY ./app-release-unsigned.apk $APP_KEY_ALIAS <<< $APP_KEY_PASSWORD
rm -rf "$APK_LOCATION/$APP_NAME.apk"
$zipalign -v 4 ./app-release-unsigned.apk "$APK_LOCATION/$APP_NAME.apk"
open $APK_LOCATION
#the end
回答by ssz
For Windows, I've created a build.cmd
file:
对于 Windows,我创建了一个build.cmd
文件:
(replace the keystore path and alias)
(替换密钥库路径和别名)
For Cordova:
科尔多瓦:
@echo off
set /P spassw="Store Password: " && set /P kpassw="Key Password: " && cordova build android --release -- --keystore=../../local/my.keystore --storePassword=%spassw% --alias=tmpalias --password=%kpassw%
And for Ionic:
对于离子:
@echo off
set /P spassw="Store Password: " && set /P kpassw="Key Password: " && ionic build --prod && cordova build android --release -- --keystore=../../local/my.keystore --storePassword=%spassw% --alias=tmpalias --password=%kpassw%
Save it in the ptoject's directory, you can double click or open it with cmd.
保存在ptoject的目录下,可以双击或者cmd打开。
回答by Arpit Patel
First Check your version code and version name if you are updating your app. And make sure you have a previous keystore.
如果您正在更新您的应用程序,请首先检查您的版本代码和版本名称。并确保您有以前的密钥库。
If you are updating app then follow step 1,3,4.
如果您正在更新应用程序,请按照步骤 1、3、4 进行操作。
Step 1:
第1步:
Goto your cordova project for generate our release build:
转到您的cordova项目以生成我们的发布版本:
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 中找到我们未签名的APK 文件。在我们的示例中,文件是
if u used ant-build
如果你使用 ant-build
yourproject/platforms/android/ant-build/Example-release-unsigned.apk
OR
或者
if u used gradle-build
如果你使用 gradle-build
yourProject/platforms/android/build/outputs/apk/Example-release-unsigned.apk
Step 2:
第2步:
Key Generation:
密钥生成:
Syntax:
句法:
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
if keytool command not recognize do this step
如果 keytool 命令无法识别,请执行此步骤
Check that the directory the keytool executable is in is on your path. (For example, on my Windows 7 machine, it's in C:\Program Files (x86)\Java\jre6\bin.)
检查 keytool 可执行文件所在的目录是否在您的路径中。(例如,在我的 Windows 7 机器上,它位于 C:\Program Files (x86)\Java\jre6\bin。)
Example:
例子:
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
然后密钥库已经生成,名称为 NAME-mobileapps.keystore
Step 3:
第 3 步:
Place the generated keystore in D:\projects\Phonegap\Example\platforms\android\ant-build
将生成的keystore放在D:\projects\Phonegap\Example\platforms\android\ant-build
To sign the unsigned APK, run the jarsigner tool which is also included in the JDK:
要签署未签名的 APK,请运行 jarsigner 工具,该工具也包含在 JDK 中:
Syntax:
句法:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename <Unsigned APK file> <Keystore Alias name>
If it doesn't reconize do these steps
如果没有重新调整,请执行以下步骤
(1)Right click on "This PC" > right-click Properties > Advanced system settings > Environment Variables > select PATH then EDIT.
(1)右键单击“此PC”> 右键单击属性> 高级系统设置> 环境变量> 选择PATH 然后编辑。
(2)Add your jdk bin folder path to environment variables, it should look like this:
(2)将你的 jdk bin 文件夹路径添加到环境变量中,它应该是这样的:
"C:\Program Files\Java\jdk1.8.0_40\bin".
“C:\Program Files\Java\jdk1.8.0_40\bin”。
Example:
例子:
D:\projects\Phonegap\Example\platforms\android\ant-build> 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.
这标志着 apk 就位。
Step 4:
第四步:
Finally, we need to run the zip align tool to optimize the APK:
最后,我们需要运行 zip align 工具来优化 APK:
if zipalign not recognize then
如果 zipalign 无法识别,则
(1)goto your android sdk path and find zipalign it is usually in android-sdk\build-tools\23.0.3
(1)转到你的android sdk 路径并找到zipalign 它通常在android-sdk\build-tools\23.0.3
(2)Copy zipalign file paste into your generate release apk folder usually in below path
(2)将 zipalign 文件粘贴到您的生成发布 apk 文件夹中,通常在以下路径中
yourproject/platforms/android/ant-build/Example-release-unsigned.apk
yourproject/platforms/android/ant-build/Example-release-unsigned.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
Now we have our final release binary called example.apk and we can release this on the Google Play Store.
现在我们有了名为 example.apk 的最终发布二进制文件,我们可以在 Google Play 商店上发布它。