How to sign an android app

本文详细介绍Android应用签名的基本规则、策略及具体步骤。包括使用Keytool和Jarsigner生成密钥对并进行签名的方法,以及如何确保应用能在Android Market上顺利发布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to sign an android app
   
    ** basic rule
      1. All applications must be signed. The system will not install an application 
        that is not signed.
      2. You can use self-signed certificates to sign your applications. No certificate
        authority is needed        
      3. The system tests a signer certificate's expiration date only at install time.
      4. You can use standard tools — Keytool and Jarsigner — to generate keys and sign 
        your application .apk files.
      5. There are two signing mode: DEBUG mode/Release mode
      6. When your application is ready for release, you must compile in release mode and then sign
        the .apk with your private key. There are two ways to do this:
            6.1) Using Keytool and Jarsigner in the command-line. 
                a) build API
                b) use jarsigner to sign it
                c) if no private key, using keytool to create key-pair
          6.2) Using the ADT Export Wizard. If you are developing in Eclipse with the ADT plugin, you can 
                use the Export Wizard to compile the application, generate a private key (if necessary), 
                and sign the .apk, all in a single process using the Export Wizard.
               
    ** signing strategy : suggest to sign all app using the same keypairs      
      1. You cannot update an app if with different signing keys. The only method   in such case is to
          "install the later as a pure new app"
      2. If you plan to publish your application(s) on Android Market, the key you use to sign the application(s) 
          must have a validity period ending after 22 October 2033    
         
    ** basic setup for signing
      1. make sure you are using JDK version of keytool, instead of gcj version
      2. both keytool and jarsigner are in JDK package
       
    ** Signing for Public Release
      When your application is ready for release to other users, you must:
            1. Compile the application in release mode
            2. Obtain a suitable private key
            3. Sign the application with your private key    
    ** Compiling for release in Eclipse
      right-click the project in the Package Explorer and select 
            Android Tools > Export Unsigned Application Package. 
      Then simply specify the file location for the unsigned .apk. 
      (Alternatively, open your AndroidManifest.xml file in Eclipse, 
        open the Overview tab, and click Export an unsigned .apk.)

        You can also combine the compiling and signing steps with the Export Wizard.
    ** use keytool to generate key-pair
      Omiited since it is just the same as we once do in J2ME signining
       
    ** signing
      Here's how you would use Jarsigner to sign an application package called 
                    my_application.apk, 
      using the example keystore created above.
          # jarsigner -verbose -keystore my-release-key.keystore my_application.apk alias_name
       
      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:
          # arsigner -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 at 
                http://java.sun.com/j2se/1.5.0/docs/tooldocs/#security
               
In addition:
   

Android签名用keytool和jarsigner制作apk文件

原文地址:http://www.android123.com.cn/androidkaifa/173.html

Android程序的签名和Symbian类似都可以自签名(Self-signed),但是在Android平台中证书初期还显得形同虚设,平时 开发时通过ADB接口上传的程序会自动被签有Debug权限的程序。需要签名验证在上传程序到Android Market上时大家都已经发现这个问题了。Android signed制作方法

  首先在Android开发时没有安装JDK的网友在Sun官方网站下载JDKhttp://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe ,其实仅需要中的Keytool和Jarsigner。

 详细的签名步骤:

 第一步

C:\Program Files\Java\jdk1.6.0_10\bin>keytool -genkey -alias android123.keystore
 -keyalg RSA -validity 20000 -keystore android123.keystore
 
输入keystore密码:[密码不回显]
再次输入新密码:[密码不回显]
您的名字与姓氏是什么?
  [Unknown]:  android123
您的组织单位名称是什么?
  [Unknown]:  www.android123.com.cn 
您的组织名称是什么?
  [Unknown]:  www.android123.com.cn 
您的组织名称是什么?
  [Unknown]:  www.android123.com.cn 
您所在的城市或区域名称是什么?
  [Unknown]:  New York 
您所在的州或省份名称是什么?
  [Unknown]:  New York 
该单位的两字母国家代码是什么
  [Unknown]:  CN 
CN=android123, OU=www.android123.com.cn, O=www.android123.com.cn, L=New York, ST
=New York, C=CN 正确吗?
  [否]:  Y

输入<android123.keystore>的主密码
        (如果和 keystore 密码相同,按回车):

 其中参数-validity为证书有效天数,这里我们写的大些20000天。还有在输入密码时没有回显,只管输入就可以了,一般位数建议使用20位,最后需要记下来后面还要用,整个过程如图:

android keytool

  接下来我们开始为apk文件签名了。

 第二步

  执行下面这句jarsigner -verbose -keystore android123.keystore -signedjar android123_signed.apk android123.apk android123.keystore 就可以生辰签名的apk文件,这里输入文件android123.apk,最终生成android123_signed.apk为Android签名后的 APK执行文件。下面提示输入的密码和keytool输入的一样就行了,如图:

 android jarsigner

  有关Android程序发布和签名可以查看SDK中 http://code.google.com/android/devel/sign-publish.html  一文.

  附上keytool参数以及jarsigner参数:

  keytool用法 

-certreq     [-v] [-protected]
             [-alias <别名>] [-sigalg <sigalg>]
             [-file <csr_file>] [-keypass <密钥库口令>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-changealias [-v] [-protected] -alias <别名> -destalias <目标别名>
             [-keypass <密钥库口令>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-delete      [-v] [-protected] -alias <别名>
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-exportcert  [-v] [-rfc] [-protected]
             [-alias <别名>] [-file <认证文件>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-genkeypair  [-v] [-protected]
             [-alias <别名>]
             [-keyalg <keyalg>] [-keysize <密钥大小>]
             [-sigalg <sigalg>] [-dname <dname>]
             [-validity <valDays>] [-keypass <密钥库口令>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-genseckey   [-v] [-protected]
             [-alias <别名>] [-keypass <密钥库口令>]
             [-keyalg <keyalg>] [-keysize <密钥大小>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-help

-importcert  [-v] [-noprompt] [-trustcacerts] [-protected]
             [-alias <别名>]
             [-file <认证文件>] [-keypass <密钥库口令>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-importkeystore [-v]
             [-srckeystore <源密钥库>] [-destkeystore <目标密钥库>]
             [-srcstoretype <源存储类型>] [-deststoretype <目标存储类型>]
             [-srcstorepass <源存储库口令>] [-deststorepass <目标存储库口令>]
             [-srcprotected] [-destprotected]
             [-srcprovidername <源提供方名称>]
             [-destprovidername <目标提供方名称>]
             [-srcalias <源别名> [-destalias <目标别名>]
               [-srckeypass <源密钥库口令>] [-destkeypass <目标密钥库口令>]]
             [-noprompt]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-keypasswd   [-v] [-alias <别名>]
             [-keypass <旧密钥库口令>] [-new <新密钥库口令>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-list        [-v | -rfc] [-protected]
             [-alias <别名>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

-printcert   [-v] [-file <认证文件>]

-storepasswd [-v] [-new <新存储库口令>]
             [-keystore <密钥库>] [-storepass <存储库口令>]
             [-storetype <存储类型>] [-providername <名称>]
             [-providerclass <提供方类名称> [-providerarg <参数>]] ...
             [-providerpath <路径列表>]

 

jarsigner用法 : [选项] jar 文件别名
       jarsigner -verify [选项] jar 文件

[-keystore <url>]           密钥库位置
[-storepass <口令>]         用于密钥库完整性的口令
[-storetype <类型>]         密钥库类型
[-keypass <口令>]           专用密钥的口令(如果不同)
[-sigfile <文件>]           .SF/.DSA 文件的名称
[-signedjar <文件>]         已签名的 JAR 文件的名称
[-digestalg <算法>]    摘要算法的名称
[-sigalg <算法>]       签名算法的名称
[-verify]                   验证已签名的 JAR 文件
[-verbose]                  签名/验证时输出详细信息
[-certs]                    输出详细信息和验证时显示证书
[-tsa <url>]                时间戳机构的位置
[-tsacert <别名>]           时间戳机构的公共密钥证书
[-altsigner <类>]           替代的签名机制的类名
[-altsignerpath <路径列表>] 替代的签名机制的位置
[-internalsf]               在签名块内包含 .SF 文件
[-sectionsonly]             不计算整个清单的散列
[-protected]                密钥库已保护验证路径
[-providerName <名称>]      提供者名称
[-providerClass <类>        加密服务提供者的名称
[-providerArg <参数>]] ... 主类文件和构造函数参数


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值