作为一只Android程序汪,不会gradle就有点尴尬了,所以功能简单介绍下。
1.配置 maven库
buildscript {
repositories {
// 内网maven库
maven { url “http://xxx” }
// jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:1.3.0’
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
// 内网maven库
maven { url “http://xxx” }
// jcenter()
}
}
2.Android studio引用so文件:
sourceSets {
main {
jniLibs.srcDir 'libs'
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
3.方法和变量的定义
def gitVersionCode() {
def cmd = 'git rev-list HEAD --first-parent --count'
cmd.execute().text.trim().toInteger()
}
def gitVersionTag() {
def cmd = 'git describe --tags'
def version = cmd.execute().text.trim()
def pattern = "-(\\d+)-g"
def matcher = version =~ pattern
if (matcher) {
version = version.substring(0, matcher.start()) + "." + matcher[0][1]
} else {
version = version + ".0"
}
return version
}
def PRODUCT_NAME = "xxxx"
def date = new Date()
def BUILD_TIME = date.format("yyyy-MM-dd HH:mm:ss")
def BUILD_TIME_FORMAT = new Date().format("yyyyMMdd")
def host_url = "https://xxxxx/";
4.Android 相关的一些设置
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
applicationId "xxxxx"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
}
5.静态代码检验设置
lintOptions {
abortOnError false
}
- 签名
Properties props = new Properties()
props.load(new FileInputStream(file("signing.properties")))
signingConfigs {
release {
keyAlias '....'
keyPassword '......'
storeFile file('../../.jks')
storePassword 'xxxxx'
}
debug {
storeFile file('../../xxxx')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
buildConfigField "boolean", "LOG_DEBUG", "false"
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
minifyEnabled true
shrinkResources true
buildConfigField "boolean", "LOG_DEBUG", "true"
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
8.本地 jar包相关
repositories {
flatDir {
dirs 'libs'
}
}
9、打包命令:
//打debug包的命令
task appStart(type: Exec, dependsOn: 'installDevDebug') {
// linux
// def adb = android.getAdbExe().toString()
//commandLine "$adb", 'shell', 'am', 'start', '-n', 'xxxx/xxxx.Activity'
//windows
commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', 'xxxx/xxxx.Activity'
}
// 替换渠道名,替换AndroidManifest下面的占位符${GRADLE_CHANNEL_VALUE}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [GRADLE_CHANNEL_VALUE: name]
}
// 指定apk名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
// 打包类型
def buildTypeName = variant.buildType.name
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 打包名称
def flavorName = variant.productFlavors[0].name
// 版本名称
def versionName = defaultConfig.versionName
// 开发环境
buildConfigField "String", "APP_ENV", "\"${flavorName} \""
// url
buildConfigField "String", "HOST_URL", "\" ${host_url}\""
// xxxx_release_ver1.0.0_build20150721.apk
def fileName = "${PRODUCT_NAME}_${buildTypeName}_${flavorName}_env${flavorName}_ver${versionName}_build${BUILD_TIME_FORMAT}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
//渠道号
productFlavors {
// 线上版本
/* rel{
}*/
//开发版本,
dev {
host_url = "http://xxxx/"
}
//Qa测试版本
/*qa{
host_url = "http://xxxx/"
}*/
}
10、git版本号
android {
buildTypes {
debug {
resValue "string", "git_revision", "\"${gitRevision()}\""
}
release {
resValue "string", "git_revision", "\"${gitRevision()}\""
}
}
}
def gitRevision() {
def cmd = "git rev-parse --short HEAD"
return cmd.execute().text.trim()
}
好了 就此完结吧。
其中两个好用的是混淆和自动删除多余文件配置:
minifyEnabled true
shrinkResources true
前面是混淆,设置为true,就是打开混淆
后面是自动删除多余资源文件,设置为true就好。