如何在Windows中手动自签名和验证android应用程序(apk)

本文概述

  • 1.创建一个自签名证书。
  • 2.使用自签名证书对.apk文件进行签名。
  • 3.验证你的apk(可选)
  • 4.使用zipalign对齐你的.apk
【如何在Windows中手动自签名和验证android应用程序(apk)】根据android开发人员网站上的发布文章, 在将应用程序启动到Play商店之前, 需要使用zipalign对其进行签名和压缩。
对于某些开发人员而言, 出于多种原因(而不是如何做, 你的应用程序是使用混合技术创建的), 很容易手动使用android studio来执行此过程。要签名我们的android应用程序, 请按照以下步骤操作。
  • 在继续之前, 请确认你已在PC中安装了Java开发工具包和android SDK, 否则你将迷失在步骤中。
  • 切记始终在管理员模式下执行cmd。
1.创建一个自签名证书。该证书稍后将用于对你的apk进行签名, 以生成证书, 我们需要使用控制台(cmd)转到PC上的java sdk bin路径
# Usually is located in C:\Program Files (x86)\Java\jdk1.7.0_71\bin , but anyway check the location on your pc.# Then in the cmd type :C:\ > cd C:\Program Files (x86)\Java\jdk1.7.0_71\bin# You'll be located in the bin path and we can use all the java tools.C:\Program Files (x86)\Java\jdk1.7.0_71\bin >

当你通过控制台位于bin路径中时, 执行以下命令:
REMRemember to change the mycustomname and mycustom_alias to your correct keystore name and alias (whatever you want)keytool -genkey -v -keystore mycustomname.keystore -alias mycustom_alias -keyalg RSA -keysize 2048 -validity 10000

遵循所有提示, 将要求你输入代码国家, 位置, 名称和其他信息以及密码, 只需填写这些字段即可。如果一切顺利, 你将在bin路径中获得一个文件, 该文件具有你提供的名称(在本例中为mycustomname.keystore)。
2.使用自签名证书对.apk文件进行签名。现在, 我们需要一个未签名的.apk文件, 我们需要将该文件复制到jdk的bin路径中(只需将你的apk文件复制到jdk中的C:\ Program Files(x86)\ Java \ jdk1.7.0_71 \ bin)。
然后执行以下命令:
REMChange the name of the apk, the name of the certificate and the alias with your own data.jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore mycustomname.keystore MYAPKFILE.apk mycustom_alias

这将使用位于同一bin路径中的以前的自签名证书, 你只需要提供证书的密码即可成功对.apk进行签名。
3.验证你的apk(可选)如果要检查一切正常, 请使用以下命令验证.apk
jarsigner -verify -verbose -certs MYAPKFILE.apk

4.使用zipalign对齐你的.apk现在, 我们将压缩签名的apk文件。为了对apk进行签名, 我们需要在PC中安装android sdk。然后, 使用Windows控制台(cmd)进入build-tools文件夹:
# Go to the android build tools folder that contains the zipalign.exe with the console# Usually is located in C:\Users\MyUser\AppData\Local\Android\android-sdk\build-tools\23.0.0# Or just browse to the folder that contains the zipalign.exe file in your sdkC\: > cd C:\Users\MyUser\AppData\Local\Android\android-sdk\build-tools\23.0.0# We will be now on the build tools directory :C:\Users\MyUser\AppData\Local\Android\android-sdk\build-tools\23.0.0 >

首先将步骤2中已签名的.apk文件复制到zipalign.exe所在的构建工具文件夹中。然后只需使用以下命令执行zip aling命令:
REMThis will be use the name of the unaligned file from the previous 2 and will create a new one with the new name in the build tools folderzipalign -v 4 MyUnalignedFile.apk MyNewUnalignedFile.apkREMThen a file named MyNewUnalignedFile.apk will be located in C:\Users\MyUser\AppData\Local\Android\android-sdk\build-tools\23.0.0REMNow this file can be uploaded to the play store

现在一切都应该设置好了, 可以将你的APK上传到Play商店, 玩得开心!

    推荐阅读