转自:http://bbs.weiphone.com/read.php?tid=519993

开发Android第三步,签名证书,真机安装编译程序

Android SDK 1.6 最主要改变为模拟器或真机可用系统自动创建的调试签名证书(debug.keystore), 但可发布的安装程序必须要先创建自签名证书 包括密钥库 keystore 和私钥 key alias 。

Android SDK 编译及安装程序主要有两个方法,(A) 用 Apache Ant(B) 用 Eclipse IDE。

首先介绍的 (A) Apache Ant

windows 方法

(1) 创建自签名证书

复制代码
  1. cd C:/Android/
  2. "C:/Program Files/Java/jdk1.6.0_16/bin/keytool.exe" -genkey -v -keystore android-release-key.keystore -alias androidreleasekey-keyalg RSA --validity 10000



回答以下问题

复制代码
  1. Enter keystore password:<-- 设置keystore密码-必须至少6个字符
  2. What is your first and last name?
  3. [Unknown]:<-- 输入你的名字
  4. What is the name of your organizational unit?
  5. [Unknown]:<-- 组织单位, 可以忽略
  6. What is the name of your organization?
  7. [Unknown]:<-- 组织, 可以忽略
  8. What is the name of your City or Locality?
  9. [Unknown]:<-- 城市
  10. What is the name of your State or Province?
  11. [Unknown]:<-- 省份
  12. What is the two-letter country code for this unit?
  13. [Unknown]: CN <-- 国家
  14. Is CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN correct?
  15. [no]:yes<-- 输入 yes 确认
  16. Generating 1,024 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10,000 days
  17. for: CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN
  18. Enter key password for <androidreleasekey>
  19. (RETURN if same as keystore password):<-- 设置key密码,如与keystore密码相同,回车
  20. [Storing android-release-key.keystore]



(2) Windows 进入command prompt 及更新 NotePad 项目

复制代码
  1. cd C:/Android/Projects/samples
  2. android update project --name NotePad --target 2 --path NotePad



(3) 在 C:/Android/Projects/samples/NotePad 目录下建立 build.properties 文件, 内容如下

复制代码
  1. # This file is used to override default values used by the Ant build system.
  2. #
  3. # This file must be checked in Version Control Systems, as it is
  4. # integral to the build system of your project.
  5. # The name of your application package as defined in the manifest.
  6. # Used by the 'uninstall' rule.
  7. application-package=com.example.android.notepad
  8. # The name of the source folder.
  9. #source-folder=src
  10. # The name of the output folder.
  11. #out-folder=bin
  12. # You can also use it define how the release builds are signed by declaring
  13. # the following properties:
  14. #'key.store' for the location of your keystore and
  15. #'key.alias' for the name of the key to use.
  16. # The password will be asked during the build when you use the 'release' target.
  17. key.store=C:/Android/android-release-key.keystore
  18. key.alias=androidreleasekey



(4) 必须先将手机上的USB调试(Debug)模式打开,在手机上执行
设置 -> 应用程序 -> 开发 -> USB 调试
Settings -> Applications -> Development -> USB debugging

(5) 手机连接到 USB, 检查设备代码和测试连接

复制代码
  1. adb devices


如有多个装备连接时会看见

复制代码
  1. List of devices attached
  2. HT99XXX99999device
  3. emulator-5554device



(6) 代码签名,编译程序 NotePad 项目 (這方法只適合 1.6 的编译程序,即第(2)点更新NotePad项目时使用 --target 2 的选项)

复制代码
  1. cd C:/Android/Projects/samples/NotePad
  2. ant release


会要求输入keystore密码及key密码

复制代码
  1. [input] Please enter keystore password (store:C:/Android/android-release-key.keystore):
  2. 输入keystore密码
  3. [input] Please enter password for alias 'androidreleasekey':
  4. 输入key密码



(7) 手机安装 NotePad 项目
方法一

复制代码
  1. cd C:/Android/Projects/samples/NotePad
  2. ant install



方法二(如有多个装备连接时)

复制代码
  1. cd C:/Android/Projects/samples/NotePad
  2. adb -s HT99XXX99999 install bin/NotePad-release.apk



HT99XXX99999 是第(5)点找到的设备代码

(8) 如需要重新编译程序及手机再安装 NotePad 项目
方法一

复制代码
  1. cd C:/Android/Projects/samples/NotePad
  2. ant install



方法二(如有多个装备连接时)

复制代码
  1. cd C:/Android/Projects/samples/NotePad
  2. adb -s HT99XXX99999 install -r bin/NotePad-release.apk



HT99XXX99999 是第(5)点找到的设备代码


(9) 如需要删除手机內 NotePad 项目
方法一

复制代码
  1. cd C:/Android/Projects/samples/NotePad
  2. ant uninstall



方法二(如有多个装备连接时)

复制代码
  1. adb -s HT99XXX99999 uninstall com.example.android.notepad



HT99XXX99999 是第(5)点找到的设备代码

方法三,用手机的一些管理程序例如: App Manager 或
设置 -> 应用程序 -> 管理应用程序,选择程序后删除
Settings -> Applications -> Manage applications 选择程序后删除

(10) 上面第(6)点如用于1.5的编译程序的签名方法如下

更新LunarLander项目时使用 --target 1 的选项

复制代码
  1. cd C:/Android/Projects/samples
  2. android update project --name LunarLander --target 1 --path LunarLander



编译,签名及对齐

复制代码
  1. cd C:/Android/Projects/samples/LunarLander
  2. ant release
  3. "C:/Program Files/Java/jdk1.6.0_16/bin/jarsigner" -verbose -keystore C:/Android/android-release-key.keystore -storepass mypassword -keypass mypassword bin/LunarLander-unsigned.apk androidreleasekey
  4. zipalign -v 4 bin/LunarLander-unsigned.apk bin/LunarLander-release.apk



避免密码残留在历史上

复制代码
  1. "C:/Program Files/Java/jdk1.6.0_16/bin/jarsigner" -verbose -keystore C:/Android/android-release-key.keystore bin/LunarLander-unsigned.apk androidreleasekey



安装方法一

复制代码
  1. cd C:/Android/Projects/samples/LunarLander
  2. adb install bin/LunarLander-release.apk



安装方法二(如有多个装备连接时)

复制代码
  1. cd C:/Android/Projects/samples/LunarLander
  2. adb -s HT99XXX99999 install bin/LunarLander-release.apk


HT99XXX99999 是第(5)点找到的设备代码



mac / linux 方法

(1) 创建自签名证书

复制代码
  1. cd ~/Android
  2. keytool -genkey -v -keystore android-release-key.keystore -alias androidreleasekey-keyalg RSA --validity 10000



回答以下问题

复制代码
  1. Enter keystore password:<-- 设置keystore密码-必须至少6个字符
  2. What is your first and last name?
  3. [Unknown]:<-- 输入你的名字
  4. What is the name of your organizational unit?
  5. [Unknown]:<-- 组织单位, 可以忽略
  6. What is the name of your organization?
  7. [Unknown]:<-- 组织, 可以忽略
  8. What is the name of your City or Locality?
  9. [Unknown]:<-- 城市
  10. What is the name of your State or Province?
  11. [Unknown]:<-- 省份
  12. What is the two-letter country code for this unit?
  13. [Unknown]: CN <-- 国家
  14. Is CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN correct?
  15. [no]:yes<-- 输入 yes 确认
  16. Generating 1,024 bit RSA key pair and self-signed certificate (SHA1WithRSA)
  17. for: CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN
  18. Enter key password for <androidreleasekey>
  19. (RETURN if same as keystore password):<-- 设置key密码,如与keystore密码相同,回车
  20. [Storing android-release-key.keystore]



(2) 进入 Terminal 及更新 NotePad 项目

复制代码
  1. cd ~/Android/Projects/samples
  2. android update project --name NotePad --target 2 --path NotePad



(3) 在 ~/Android/Projects/samples/NotePad 目录下建立 build.properties 文件, 内容如下

复制代码
  1. # This file is used to override default values used by the Ant build system.
  2. #
  3. # This file must be checked in Version Control Systems, as it is
  4. # integral to the build system of your project.
  5. # The name of your application package as defined in the manifest.
  6. # Used by the 'uninstall' rule.
  7. application-package=com.example.android.notepad
  8. # The name of the source folder.
  9. #source-folder=src
  10. # The name of the output folder.
  11. #out-folder=bin
  12. # You can also use it define how the release builds are signed by declaring
  13. # the following properties:
  14. #'key.store' for the location of your keystore and
  15. #'key.alias' for the name of the key to use.
  16. # The password will be asked during the build when you use the 'release' target.
  17. #linux
  18. #key.store=/home/ipod4g/Android/android-release-key.keystore
  19. #mac
  20. key.store=/Users/ipod4g/Android/android-release-key.keystore
  21. key.alias=androidreleasekey



(4) 必须先将手机上的USB调试(Debug)模式打开,在手机上执行
设置 -> 应用程序 -> 开发 -> USB 调试
Settings -> Applications -> Development -> USB debugging

(5) 手机连接到 USB, 检查设备代码和测试连接

复制代码
  1. adb devices


如有多个装备连接时会看见

复制代码
  1. List of devices attached
  2. HT99XXX99999device
  3. emulator-5554device



(6) 代码签名,编译程序 NotePad 项目 (這方法只適合 1.6 的编译程序,即第(2)点更新NotePad项目时使用 --target 2 的选项)

复制代码
  1. cd ~/Android/Projects/samples/NotePad
  2. ant release


会要求输入keystore密码及key密码

复制代码
  1. [input] Please enter keystore password (store:/Users/ipod4g/Android/android-release-key.keystore):
  2. 输入keystore密码
  3. [input] Please enter password for alias 'androidreleasekey':
  4. 输入key密码



(7) 手机安装 NotePad 项目
方法一

复制代码
  1. cd ~/Android/Projects/samples/NotePad
  2. ant install



方法二(如有多个装备连接时)

复制代码
  1. cd ~/Android/Projects/samples/NotePad
  2. adb -s HT99XXX99999 install bin/NotePad-release.apk



HT99XXX99999 是第(5)点找到的设备代码

(8) 如需要重新编译程序及手机再安装 NotePad 项目
方法一

复制代码
  1. cd ~/Android/Projects/samples/NotePad
  2. ant install



方法二(如有多个装备连接时)

复制代码
  1. cd ~/Android/Projects/samples/NotePad
  2. adb -s HT99XXX99999 install -r bin/NotePad-release.apk



HT99XXX99999 是第(5)点找到的设备代码


(9) 如需要删除手机內 NotePad 项目
方法一

复制代码
  1. cd ~/Android/Projects/samples/NotePad
  2. ant uninstall



方法二(如有多个装备连接时)

复制代码
  1. adb -s HT99XXX99999 uninstall com.example.android.notepad



HT99XXX99999 是第(5)点找到的设备代码

方法三,用手机的一些管理程序例如: App Manager 或
设置 -> 应用程序 -> 管理应用程序,选择程序后删除
Settings -> Applications -> Manage applications 选择程序后删除

(10) 上面第(6)点如用于1.5的编译程序的签名方法如下

更新LunarLander项目时使用 --target 1 的选项

复制代码
  1. cd ~/Android/Projects/samples
  2. android update project --name LunarLander --target 1 --path LunarLander



编译,签名及对齐

复制代码
  1. cd ~/Android/Projects/samples/LunarLander
  2. ant release
  3. jarsigner -verbose -keystore ~/Android/android-release-key.keystore -storepass mypassword -keypass mypassword bin/LunarLander-unsigned.apk androidreleasekey
  4. zipalign -v 4 bin/LunarLander-unsigned.apk bin/LunarLander-release.apk



避免密码残留在历史上

复制代码
  1. jarsigner -verbose -keystore ~/Android/android-release-key.keystore bin/LunarLander-unsigned.apk androidreleasekey



安装方法一

复制代码
  1. cd ~/Android/Projects/samples/LunarLander
  2. adb install bin/LunarLander-release.apk



安装方法二(如有多个装备连接时)

复制代码
  1. cd ~/Android/Projects/samples/LunarLander
  2. adb -s HT99XXX99999 install bin/LunarLander-release.apk


HT99XXX99999 是第(5)点找到的设备代码

(B) Eclipse IDE

(1) 在Package Explorer 选择程序項目,然后选择菜单 File -> Export...

(2) 打开 Android 文件夹,选择 Export Android Application,然后单击 Next

(3) 这将引导您的应用程序的签署,包括选择密钥库 keystore 和 key alias 私钥与其签署的步骤过程 (或创建一个新的密钥库 keystore 和私钥 key alias)

(4) 完成后你的应用程序 YourProgram-release.apk 将被编译(compiled),签署(signed),对齐(aligned) 并可安装及发布。安装或删除应用程方法请参考上面各点。
.

更多相关文章

  1. Android(安卓)WebView优化
  2. cocos2d-x 播放视频 on Android
  3. 《第一行代码》-第一次学习笔记(第一章).md
  4. android中如何通过代码来开启和关闭移动网络
  5. Android跨进程通信IPC之19——AIDL
  6. android 垂直 SeekBar 源代码(VerticalSeekBar)
  7. Android之Intent(一)
  8. Android(安卓)不得不知Tag的妙用
  9. Android中捕捉menu按键点击事件的方法

随机推荐

  1. git 忽略未提交
  2. 使RPA效用最大化的业务流程应具备的5大特
  3. 基础命令8
  4. 第25章 0119-组件的知识,学习心得,笔记(Vue,
  5. PHP基础知识:数组相关函数和操作
  6. 初探jQuery($(),attr(),css(),val(),html(
  7. 数组排序、数组合并
  8. 通讯录的实现
  9. 什么是爬虫?Python爬虫的工作流程怎样?
  10. 【我的Linux,我做主!】Docker容器技术基础