開發Android第三步,簽署憑證,真機安裝編譯器

來源:互聯網
上載者:User

轉自: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. HT99XXX99999      device
  3. emulator-5554     device

(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. HT99XXX99999      device
  3. emulator-5554     device

(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) 並可安裝及發布。安裝或刪除應用程方法請參考上面各點。
.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.