应用能在Android 系统上安装必须是通过有私有key的证书数据签名。Android系统经过证书肯定应用的做者,和与应用创建信任关系。证书不会用于控制应用的安装。证书不须要权威机构签名:它是很是完美和标准。html
关于签名的一些重要点:java
Android 系统不能安装和运行没有正确签名的包。linux
Android 签名应用与构建方式有关,不一样构建方式会致使签名过程的不一样。这里有两个构建方式:调试模式和发布模式。调试模式在开发和测试的状况下使用,发布模式是在将要发布应用给用户的时候才会使用的(好比发布到Google Play)。android
When you build in debug mode the Android SDK build tools use the Keytool utility (included in the JDK) to create a debug key. Because the SDK build tools created the debug key, they know the debug key's alias and password. Each time you compile your application in debug mode, the build tools use the debug key along with the Jarsigner utility (also included in the JDK) to sign your application's .apk
file. Because the alias and password are known to the SDK build tools, the tools don't need to prompt you for the debug key's alias and password each time you compile.算法
When you build in release mode you use your own private key to sign your application. If you don't have a private key, you can use the Keytool utility to create one for you. When you compile your application in release mode, the build tools use your private key along with the Jarsigner utility to sign your application's .apk
file. Because the certificate and private key you use are your own, you must provide the password for the keystore and key alias.shell
The debug signing process happens automatically when you run or debug your application using Eclipse with the ADT plugin. Debug signing also happens automatically when you use the Ant build script with the debug
option. You can automate the release signing process by using the Eclipse Export Wizard or by modifying the Ant build script and building with the release
option.windows
签名应用的方式会影响开发应用的方法。特别是在须要发布多个应用的时候。安全
通常,对于全部的开发者比较推荐的策略是全部的应用用同一个证书签名(在有效期以内),这样作的缘由是:oracle
另一个决定签名方式的重要因素是如何设置密钥的有效期。app
当咱们再设计应用的时候, 要考虑这些要点。
在开始以前,要保证Keytool和Jarsigner 工具都已经就绪,两个工具都在JDK中。一般,经过在PATH 中设置JAVA_HOME的方式以便SDK构建工具能找到。
若是在linux系统上开发,要保证系统用的是JDK的工具, 而不是gcj版本的。
为了更加方便开发和调试应用,Android 系统构建工具提供调试模式签名. 用调试模式构建应用的时候,SDK工具用Keytool自动建立调试密钥库和 密钥。这个密钥在构建应用的时候自动的签名应用,所以不须要手动的签名应用。
SDK 工具提供预约义的name/password建立keystore/key:
若是须要改变keystore/key的位置和名字或者用自定义的keystore/key,都是作到的。 任何自定义的调试密钥都须要保证同一个密钥库和密钥。(To do so in Eclipse/ADT, go to Windows > Preferences > Android > Build.)
Caution: 应用不能用调试密钥签名去发布.
If you are developing in Eclipse/ADT (and have set up Keytool and Jarsigner as described above in Basic Setup for Signing), signing in debug mode is enabled by default. When you run or debug your application, ADT signs the .apk
file with the debug certificate, runs zipalign
on the package, then installs it on the selected emulator or connected device. No specific action on your part is needed, provided ADT has access to Keytool.
If you are using Ant to build your .apk
file, debug signing mode is enabled by using the debug
option with the ant
command (assuming that you are using a build.xml
file generated by the android
tool). When you run ant debug
to compile your app, the build script generates a keystore/key and signs the APK for you. The script then also aligns the APK with the zipalign
tool. No other action on your part is needed. Read Building and Running Apps on the Command Line for more information.
自签名证书在调试模式的时候签名应用,证书的有效期只有365天。
当证书过时,那么在构建的时候会发生错误。在ant 构建中,错误内容:
debug:[echo]Packaging bin/samples-debug.apk,and signing it with a debug key...[exec]DebugCertificate expired on 8/4/083:43 PM
In Eclipse/ADT, 类似的错误会出如今 Android console.
为了解决这个问题, 简单的方法是删除 debug.keystore
文件. 文件的地址在 ~/.android/
on OS X and Linux, in C:\Documents and Settings\<user>\.android\
on Windows XP, and in C:\Users\<user>\.android\
on Windows Vista and Windows 7.
下次构建的时候, 构建工具会自动生成keystore 和 调试密钥。
Note that, if your development machine is using a non-Gregorian locale, the build tools may erroneously generate an already-expired debug certificate, so that you get an error when trying to compile your application. For workaround information, see the troubleshooting topic I can't compile my app because the build tools generated an expired debug certificate.
发布应用给其余用户的时候,必须:
若是是用eclipse ADT开发,能够用导出向导编译,签名,对其应用。这个向导甚至能够帮助咱们生成私有的密钥。能够参考 Compile and sign with Eclipse ADT.
在签名应用以前,保证有一个应用,私有密钥有如下特色:
If you plan to publish your application(s) on Google Play, note that a validity period ending after 22 October 2033 is a requirement. You can not upload an application if it is signed with a key whose validity expires before that date.
The key may be self-signed. If you do not have a suitable key, you must generate one using Keytool. Make sure that you have Keytool available, as described in Basic Setup.
To generate a self-signed key with Keytool, use the keytool
command and pass any of the options listed below (and any others, as needed).
Warning: Keep your private key secure. Before you run Keytool, make sure to read Securing Your Private Key for a discussion of how to keep your key secure and why doing so is critically important to you and to users. In particular, when you are generating your key, you should select strong passwords for both the keystore and key.
Warning: Keep the keystore file you generate with Keytool in a safe, secure place. You must use the same key to sign future versions of your application. If you republish your app with a new key, Google Play will consider it a new app. For more information on settings that must remain constant over the life of your app, see the Android Developer Blog post Things That Cannot Change.
Keytool Option | Description |
---|---|
-genkey |
Generate a key pair (public and private keys): 生成密钥对(共有的和私有的) |
-v |
Enable verbose output.:容许输出 |
-alias <alias_name> |
An alias for the key. Only the first 8 characters of the alias are used. :密钥的别名,仅仅前面八个字符会被使用 |
-keyalg <alg> |
The encryption algorithm to use when generating the key. Both DSA and RSA are supported. :生成密钥的加密算法。支持:DSA 和 RSA |
-keysize <size> |
The size of each generated key (bits). If not supplied, Keytool uses a default key size of 1024 bits. In general, we recommend using a key size of 2048 bits or higher. :生成密钥的大小。若是支持,Keytool 使用默认的大小(1024位)。通常来说,使用2048位 或者更大 |
-dname <name> |
A Distinguished Name that describes who created the key. The value is used as the issuer and subject fields in the self-signed certificate. Note that you do not need to specify this option in the command line. If not supplied, Jarsigner prompts you to enter each of the Distinguished Name fields (CN, OU, and so on). :这个名字说明谁建立了这个密钥。 |
-keypass <password> |
The password for the key. As a security precaution, do not include this option in your command line. If not supplied, Keytool prompts you to enter the password. In this way, your password is not stored in your shell history. :密钥的密码 |
-validity <valdays> |
The validity period for the key, in days. Note: A value of 10000 or greater is recommended. :密钥的有效期 |
-keystore <keystore-name>.keystore |
A name for the keystore containing the private key. :keystore 的名字 |
-storepass <password> |
A password for the keystore. As a security precaution, do not include this option in your command line. If not supplied, Keytool prompts you to enter the password. In this way, your password is not stored in your shell history. : keystore的密码 |
Here's an example of a Keytool command that generates a private key:
$ keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048-validity 10000
Running the example command above, Keytool prompts you to provide passwords for the keystore and key, and to provide the Distinguished Name fields for your key. It then generates the keystore as a file called my-release-key.keystore
. The keystore and key are protected by the passwords you entered. The keystore contains a single key, valid for 10000 days. The alias is a name that you — will use later, to refer to this keystore when signing your application.
For more information about Keytool, see the documentation athttp://docs.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html
In order to release your application to users, you must compile it in release mode. In release mode, the compiled application is not signed by default and you will need to sign it with your private key.
Caution: You can not release your application unsigned, or signed with the debug key.
To export an unsigned APK from Eclipse, right-click the project in the Package Explorer and select Android Tools > Export Unsigned Application Package. Then specify the file location for the unsigned APK. (Alternatively, open yourAndroidManifest.xml
file in Eclipse, select the Manifest tab, and click Export an unsigned APK.)
Note that you can combine the compiling and signing steps with the Export Wizard. See Compiling and signing with Eclipse ADT.
If you are using Ant, you can enable release mode by using the release
option with the ant
command. For example, if you are running Ant from the directory containing your build.xml
file, the command would look like this:
$ ant release
By default, the build script compiles the application APK without signing it. The output file in your project bin/
will be<your_project_name>-unsigned.apk
. Because the application APK is still unsigned, you must manually sign it with your private key and then align it using zipalign
.
However, the Ant build script can also perform the signing and aligning for you, if you have provided the path to your keystore and the name of your key alias in the project's ant.properties
file. With this information provided, the build script will prompt you for your keystore and alias password when you perform ant release
, it will sign the package and then align it. The final output file in bin/
will instead be <your_project_name>-release.apk
. With these steps automated for you, you're able to skip the manual procedures below (steps 3 and 4). To learn how to specify your keystore and alias in the ant.properties
file, see Building and Running Apps on the Command Line.
应用签名须要用到Jarsigner工具。确保Jarsigner和密钥都是可用状态。
To sign your application, you run Jarsigner, referencing both the application's APK and the keystore containing the private key with which to sign the APK. The table below shows the options you could use.
Jarsigner Option | Description |
---|---|
-keystore <keystore-name>.keystore |
The name of the keystore containing your private key. :keystore的名字 |
-verbose |
Enable verbose output. :输出详细内容 |
-sigalg |
The name of the signature algorithim to use in signing the APK. Use the value SHA1withRSA .:签名应用的加密算法。值是SHA1withRSA |
-digestalg |
The message digest algorithim to use in processing the entries of an APK. Use the value SHA1 . |
-storepass <password> |
The password for the keystore. As a security precaution, do not include this option in your command line unless you are working at a secure computer. If not supplied, Jarsigner prompts you to enter the password. In this way, your password is not stored in your shell history.
|
-keypass <password> |
The password for the private key. As a security precaution, do not include this option in your command line unless you are working at a secure computer. If not supplied, Jarsigner prompts you to enter the password. In this way, your password is not stored in your shell history. |
Here's how you would use Jarsigner to sign an application package called my_application.apk
, using the example keystore created above.
$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore my_application.apk alias_name
Running the example command above, Jarsigner prompts you to provide passwords for the keystore and key. It then modifies the APK in-place, meaning the APK is now signed. Note that you can sign an APK multiple times with different keys.
Caution: As of JDK 7, the default signing algorithim has changed, requiring you to specify the signature and digest algorithims (-sigalg
and -digestalg
) when you sign an APK.
To verify that your APK is signed, you can use a command like this:
$ jarsigner -verify my_signed.apk
If the APK is signed properly, Jarsigner prints "jar verified". If you want more details, you can try one of these commands:
$ jarsigner -verify -verbose my_application.apk
or
$ jarsigner -verify -verbose -certs my_application.apk
The command above, with the -certs
option added, will show you the "CN=" line that describes who created the key.
Note: If you see "CN=Android Debug", this means the APK was signed with the debug key generated by the Android SDK. If you intend to release your application, you must sign it with your private key instead of the debug key.
For more information about Jarsigner, see the documentation athttp://docs.oracle.com/javase/6/docs/technotes/tools/windows/jarsigner.html
Once you have signed the APK with your private key, run zipalign
on the file. This tool ensures that all uncompressed data starts with a particular byte alignment, relative to the start of the file. Ensuring alignment at 4-byte boundaries provides a performance optimization when installed on a device. When aligned, the Android system is able to read files with mmap()
, even if they contain binary data with alignment restrictions, rather than copying all of the data from the package. The benefit is a reduction in the amount of RAM consumed by the running application.
The zipalign
tool is provided with the Android SDK, inside the tools/
directory. To align your signed APK, execute:
$ zipalign -v 4your_project_name-unaligned.apk your_project_name.apk
The -v
flag turns on verbose output (optional). 4
is the byte-alignment (don't use anything other than 4). The first file argument is your signed .apk
file (the input) and the second file is the destination .apk
file (the output). If you're overriding an existing APK, add the -f
flag.
Caution: Your input APK must be signed with your private key before you optimize the package with zipalign
. If you sign it after using zipalign
, it will undo the alignment.
For more information, read about the zipalign tool.
If you are using Eclipse with the ADT plugin, you can use the Export Wizard to export a signed APK (and even create a new keystore, if necessary). The Export Wizard performs all the interaction with the Keytool and Jarsigner for you, which allows you to sign the package using a GUI instead of performing the manual procedures to compile, sign, and align, as discussed above. Once the wizard has compiled and signed your package, it will also perfom package alignment withzipalign
. Because the Export Wizard uses both Keytool and Jarsigner, you should ensure that they are accessible on your computer, as described above in the Basic Setup for Signing.
To create a signed and aligned APK in Eclipse:
The Export Android Application wizard now starts, which will guide you through the process of signing your application, including steps for selecting the private key with which to sign the APK (or creating a new keystore and private key).
维护私有密钥对本身和对用户都是最重要的。若是私有密钥不被好好的保护,那么颇有可能会被其余人盗用。
若是第三方在没有通过受权和容许的状况下管理你的密钥,那我的能够很容易的签名而且发布应用,达到替换你的应用和入侵你的应用。应用的数据将不会在安全。
私有的密钥在未来签名包的时候都有用。若是密钥丢失,那么将不能发布更新应用。你不能从新生成和以前同样的密钥。
Your reputation as a developer entity depends on your securing your private key properly, at all times, until the key is expired. Here are some tips for keeping your key secure:
通常来说,依照注意事项生成,使用和存储密钥,均可以保证它的安全性。