標籤:
? ?
一般來說,用gradle編譯去產生apk,有兩種編譯設定,一種是調試用的-debug mode,一種是最終包-release mode。但是無論是哪種類型,app必須在安裝到虛擬機器或裝置上必須簽名。當編譯為debug mode 的時候,用debug key。編譯為release mode時候用private key。
無論是用debug還是release模式去編譯,你都需要run and build你的module。這個過程會產生一個可以安裝在模擬器或者裝置上的apk。當你選擇使用debug mode 模式去編譯一個apk的時候,這個apk檔案會被SDK tools在build.gradle檔案中debuggable true的基礎設定上,使用debug key自動簽名。以至於改apk可以立即安裝。你不能散布一個用debug key簽名的應用。當你選擇使用release mode 編譯一個apk,這個.apk沒有被簽名,所以必須使用private key手動簽名,利用Keytool and Jarsigner 設定module 中的build.gradle 檔案。
Building in Debug Mode
on Windows platforms, typethis command:
> gradlew.bat assembleDebug
On Mac OS and Linux platforms,type these commands:
$ chmod +x gradlew
$ ./gradlew assembleDebug
The first command (chmod) adds the executionpermission to the Gradle wrapper script and is only necessary the first timeyou build this project from the command line
編譯完成的apk,在app/build/outputs/apk/
為任何lib moudels 輸入的AAR在lib/build/outputs/libs/.
To see a list of all available build tasks for yourproject, type this command:
$ ./gradlew tasks
Building in Release Mode
? ?在開始以release mode編譯一個apk之前,請注意你必須用private key簽名,然後使用zipalign 工具。
有兩種途徑去編譯一個release mode模式的apk。
第一種(Build unsigned):在release mode下編譯未簽名的package,然後手動sign and align package。
第二種(Build signed and aligned):容許build script 去sign and align package。
Build unsigned
On Windows platforms, type this command:
> gradlew.bat assembleRelease
On Mac OS and Linuxplatforms, type this command:
$ ./gradlew assembleRelease
這建立的apk檔案在項目的bin目錄下,名字格式為<your_module_name>-unsigned.apk.
Note:在此時,未簽名的apk不可以安裝到裝置上,直到其簽名為止。
一旦你建立了一個為簽名的apk,下一步就是使用private key去簽名,然後用zipalign 去對其位元組。如何?,請看我的下一篇文章《signing your application》
Build signed and aligned
在指令碼中配置,實現自動簽名,build.gradle寫法如下。
...
android {
...
defaultConfig {...}
signingConfigs {
release {
storeFile file("myreleasekey.keystore")
storePassword "password"
keyAlias "MyReleaseKey"
keyPassword "password"
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
...
作者有話說:如果您需要Android中文API,請掃一掃下面的二維碼,您的關注,就是我的動力,做技術,我們認真的。
Android 中文API:Running Gradle Builds