使用Gradle命令打包Android应用

gradlew -- gradle wrapper

./gradlew -v 版本号

./gradlew clean app目录下的build文件夹

./gradlew build 检查依赖并编译打包,同时把debug、release环境的包都打出来。

./gradlew installRelease Release模式打包并安装

./gradlew uninstallRelease 卸载Release模式包


 配合assemble 

./gradlew assembleDebug 编译并打Debug包

./gradlew assembleRelease 编译并打Release的包

其实 assemble 是和 Build Variants 一起结合使用的,而 Build Variants = Build Type + Product Flavor 除此之外 assemble 还能和 Product Flavor 结合创建新的任务,

./gradlew assembleWandoujiaRelease 生成wandoujia渠道的release版本

./gradlew assembleWandoujia 生成wandoujia渠道的Release和Debug版本

./gradlew assembleRelease 生成全部渠道Release版本。即生成Product Flavor下的所有渠道的Release版本都打出来。

总结一下,assemble 命令创建task有如下用法:

**assemble**: 允许直接构建一个Variant版本,例如assembleFlavor1Debug。

**assemble**: 允许构建指定Build Type的所有APK,例如assembleDebug将会构建Flavor1Debug和Flavor2Debug两个Variant版本。

**assemble**: 允许构建指定flavor的所有APK,例如assembleFlavor1将会构建Flavor1Debug和Flavor1Release两个Variant版本。

最佳实践

signingConfigs {
release {
try {
storeFile file("xxxx")
storePassword KEYSTORE_PASSWORD
keyAlias "xxxxx"
keyPassword KEY_PASSWORD
} catch (ex) {
throw new InvalidUserDataException("You should define KEYSTORE_PASSWORD and KEY_PASSWORD in gradle.properties.")
}
}
}
然后将password定义在gradle.properties中:
KEYSTORE_PASSWORD=pass
KEY_PASSWORD=pass

参考:http://stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/