轉自: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) 建立自我簽署憑證
複製代碼
- cd C:/Android/
- "C:/Program Files/Java/jdk1.6.0_16/bin/keytool.exe" -genkey -v -keystore android-release-key.keystore -alias androidreleasekey -keyalg RSA --validity 10000
|
回答以下問題
複製代碼
- Enter keystore password: <-- 設定keystore密碼-必須至少6個字元
- What is your first and last name?
- [Unknown]: <-- 輸入你的名字
- What is the name of your organizational unit?
- [Unknown]: <-- 組織單位, 可以忽略
- What is the name of your organization?
- [Unknown]: <-- 組織, 可以忽略
- What is the name of your City or Locality?
- [Unknown]: <-- 城市
- What is the name of your State or Province?
- [Unknown]: <-- 省份
- What is the two-letter country code for this unit?
- [Unknown]: CN <-- 國家
- Is CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN correct?
- [no]: yes <-- 輸入 yes 確認
- Generating 1,024 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10,000 days
- for: CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN
- Enter key password for <androidreleasekey>
- (RETURN if same as keystore password): <-- 設定key密碼,如與keystore密碼相同,斷行符號
- [Storing android-release-key.keystore]
|
(2) Windows 進入command prompt 及更新 NotePad 項目
複製代碼
- cd C:/Android/Projects/samples
- android update project --name NotePad --target 2 --path NotePad
|
(3) 在 C:/Android/Projects/samples/NotePad 目錄下建立 build.properties 檔案, 內容如下
複製代碼
- # This file is used to override default values used by the Ant build system.
- #
- # This file must be checked in Version Control Systems, as it is
- # integral to the build system of your project.
- # The name of your application package as defined in the manifest.
- # Used by the 'uninstall' rule.
- application-package=com.example.android.notepad
- # The name of the source folder.
- #source-folder=src
- # The name of the output folder.
- #out-folder=bin
- # You can also use it define how the release builds are signed by declaring
- # the following properties:
- # 'key.store' for the location of your keystore and
- # 'key.alias' for the name of the key to use.
- # The password will be asked during the build when you use the 'release' target.
- key.store=C:/Android/android-release-key.keystore
- key.alias=androidreleasekey
|
(4) 必須先將手機上的USB調試(Debug)模式開啟,在手機上執行
設定 -> 應用程式 -> 開發 -> USB 調試
Settings -> Applications -> Development -> USB debugging
(5) 手機串連到 USB, 檢查裝置代碼和測試連接
如有多個裝備串連時會看見
複製代碼
- List of devices attached
- HT99XXX99999 device
- emulator-5554 device
|
(6) 程式碼簽署,編譯器 NotePad 項目 (這方法只適合 1.6 的編譯器,即第(2)點更新NotePad項目時使用 --target 2 的選項)
複製代碼
- cd C:/Android/Projects/samples/NotePad
- ant release
|
會要求輸入keystore密碼及key密碼
複製代碼
- [input] Please enter keystore password (store:C:/Android/android-release-key.keystore):
- 輸入keystore密碼
- [input] Please enter password for alias 'androidreleasekey':
- 輸入key密碼
|
(7) 手機安裝 NotePad 項目
方法一
複製代碼
- cd C:/Android/Projects/samples/NotePad
- ant install
|
方法二(如有多個裝備串連時)
複製代碼
- cd C:/Android/Projects/samples/NotePad
- adb -s HT99XXX99999 install bin/NotePad-release.apk
|
HT99XXX99999 是第(5)點找到的裝置代碼
(8) 如需要重新編譯器及手機再安裝 NotePad 項目
方法一
複製代碼
- cd C:/Android/Projects/samples/NotePad
- ant install
|
方法二(如有多個裝備串連時)
複製代碼
- cd C:/Android/Projects/samples/NotePad
- adb -s HT99XXX99999 install -r bin/NotePad-release.apk
|
HT99XXX99999 是第(5)點找到的裝置代碼
(9) 如需要刪除手機內 NotePad 項目
方法一
複製代碼
- cd C:/Android/Projects/samples/NotePad
- ant uninstall
|
方法二(如有多個裝備串連時)
複製代碼
- adb -s HT99XXX99999 uninstall com.example.android.notepad
|
HT99XXX99999 是第(5)點找到的裝置代碼
方法三,用手機的一些管理程式例如: App Manager 或
設定 -> 應用程式 -> 管理應用程式,選擇程式後刪除
Settings -> Applications -> Manage applications 選擇程式後刪除
(10) 上面第(6)點如用於1.5的編譯器的簽名方法如下
更新LunarLander項目時使用 --target 1 的選項
複製代碼
- cd C:/Android/Projects/samples
- android update project --name LunarLander --target 1 --path LunarLander
|
編譯,簽名及對齊
複製代碼
- cd C:/Android/Projects/samples/LunarLander
- ant release
- "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
- zipalign -v 4 bin/LunarLander-unsigned.apk bin/LunarLander-release.apk
|
避免密碼殘留在曆史上
複製代碼
- "C:/Program Files/Java/jdk1.6.0_16/bin/jarsigner" -verbose -keystore C:/Android/android-release-key.keystore bin/LunarLander-unsigned.apk androidreleasekey
|
安裝方法一
複製代碼
- cd C:/Android/Projects/samples/LunarLander
- adb install bin/LunarLander-release.apk
|
安裝方法二(如有多個裝備串連時)
複製代碼
- cd C:/Android/Projects/samples/LunarLander
- adb -s HT99XXX99999 install bin/LunarLander-release.apk
|
HT99XXX99999 是第(5)點找到的裝置代碼
mac / linux 方法
(1) 建立自我簽署憑證
複製代碼
- cd ~/Android
- keytool -genkey -v -keystore android-release-key.keystore -alias androidreleasekey -keyalg RSA --validity 10000
|
回答以下問題
複製代碼
- Enter keystore password: <-- 設定keystore密碼-必須至少6個字元
- What is your first and last name?
- [Unknown]: <-- 輸入你的名字
- What is the name of your organizational unit?
- [Unknown]: <-- 組織單位, 可以忽略
- What is the name of your organization?
- [Unknown]: <-- 組織, 可以忽略
- What is the name of your City or Locality?
- [Unknown]: <-- 城市
- What is the name of your State or Province?
- [Unknown]: <-- 省份
- What is the two-letter country code for this unit?
- [Unknown]: CN <-- 國家
- Is CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN correct?
- [no]: yes <-- 輸入 yes 確認
- Generating 1,024 bit RSA key pair and self-signed certificate (SHA1WithRSA)
- for: CN=ipod4g, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=CN
- Enter key password for <androidreleasekey>
- (RETURN if same as keystore password): <-- 設定key密碼,如與keystore密碼相同,斷行符號
- [Storing android-release-key.keystore]
|
(2) 進入 Terminal 及更新 NotePad 項目
複製代碼
- cd ~/Android/Projects/samples
- android update project --name NotePad --target 2 --path NotePad
|
(3) 在 ~/Android/Projects/samples/NotePad 目錄下建立 build.properties 檔案, 內容如下
複製代碼
- # This file is used to override default values used by the Ant build system.
- #
- # This file must be checked in Version Control Systems, as it is
- # integral to the build system of your project.
- # The name of your application package as defined in the manifest.
- # Used by the 'uninstall' rule.
- application-package=com.example.android.notepad
- # The name of the source folder.
- #source-folder=src
- # The name of the output folder.
- #out-folder=bin
- # You can also use it define how the release builds are signed by declaring
- # the following properties:
- # 'key.store' for the location of your keystore and
- # 'key.alias' for the name of the key to use.
- # The password will be asked during the build when you use the 'release' target.
- #linux
- #key.store=/home/ipod4g/Android/android-release-key.keystore
- #mac
- key.store=/Users/ipod4g/Android/android-release-key.keystore
- key.alias=androidreleasekey
|
(4) 必須先將手機上的USB調試(Debug)模式開啟,在手機上執行
設定 -> 應用程式 -> 開發 -> USB 調試
Settings -> Applications -> Development -> USB debugging
(5) 手機串連到 USB, 檢查裝置代碼和測試連接
如有多個裝備串連時會看見
複製代碼
- List of devices attached
- HT99XXX99999 device
- emulator-5554 device
|
(6) 程式碼簽署,編譯器 NotePad 項目 (這方法只適合 1.6 的編譯器,即第(2)點更新NotePad項目時使用 --target 2 的選項)
複製代碼
- cd ~/Android/Projects/samples/NotePad
- ant release
|
會要求輸入keystore密碼及key密碼
複製代碼
- [input] Please enter keystore password (store:/Users/ipod4g/Android/android-release-key.keystore):
- 輸入keystore密碼
- [input] Please enter password for alias 'androidreleasekey':
- 輸入key密碼
|
(7) 手機安裝 NotePad 項目
方法一
複製代碼
- cd ~/Android/Projects/samples/NotePad
- ant install
|
方法二(如有多個裝備串連時)
複製代碼
- cd ~/Android/Projects/samples/NotePad
- adb -s HT99XXX99999 install bin/NotePad-release.apk
|
HT99XXX99999 是第(5)點找到的裝置代碼
(8) 如需要重新編譯器及手機再安裝 NotePad 項目
方法一
複製代碼
- cd ~/Android/Projects/samples/NotePad
- ant install
|
方法二(如有多個裝備串連時)
複製代碼
- cd ~/Android/Projects/samples/NotePad
- adb -s HT99XXX99999 install -r bin/NotePad-release.apk
|
HT99XXX99999 是第(5)點找到的裝置代碼
(9) 如需要刪除手機內 NotePad 項目
方法一
複製代碼
- cd ~/Android/Projects/samples/NotePad
- ant uninstall
|
方法二(如有多個裝備串連時)
複製代碼
- adb -s HT99XXX99999 uninstall com.example.android.notepad
|
HT99XXX99999 是第(5)點找到的裝置代碼
方法三,用手機的一些管理程式例如: App Manager 或
設定 -> 應用程式 -> 管理應用程式,選擇程式後刪除
Settings -> Applications -> Manage applications 選擇程式後刪除
(10) 上面第(6)點如用於1.5的編譯器的簽名方法如下
更新LunarLander項目時使用 --target 1 的選項
複製代碼
- cd ~/Android/Projects/samples
- android update project --name LunarLander --target 1 --path LunarLander
|
編譯,簽名及對齊
複製代碼
- cd ~/Android/Projects/samples/LunarLander
- ant release
- jarsigner -verbose -keystore ~/Android/android-release-key.keystore -storepass mypassword -keypass mypassword bin/LunarLander-unsigned.apk androidreleasekey
- zipalign -v 4 bin/LunarLander-unsigned.apk bin/LunarLander-release.apk
|
避免密碼殘留在曆史上
複製代碼
- jarsigner -verbose -keystore ~/Android/android-release-key.keystore bin/LunarLander-unsigned.apk androidreleasekey
|
安裝方法一
複製代碼
- cd ~/Android/Projects/samples/LunarLander
- adb install bin/LunarLander-release.apk
|
安裝方法二(如有多個裝備串連時)
複製代碼
- cd ~/Android/Projects/samples/LunarLander
- 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) 並可安裝及發布。安裝或刪除應用程方法請參考上面各點。
.