Android 中文API:Running Gradle Builds

来源:互联网 发布:c语言结构体typedef 编辑:程序博客网 时间:2024/05/21 04:21

    ​    ​

一般来说,用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,请扫一扫下面的二维码,您的关注,就是我的动力,做技术,我们认真的。

1 0