- 添加依赖:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:22.1.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
}
- 自定义函数:
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
-
配置java编译参数:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
-
配置签名文件
signingConfigs {
config {
keyAlias 'huanghai'
keyPassword 'huanghai'
storeFile file('E:/ibest/key/mytest.keystore')
storePassword 'huanghai'
}
}
默认配置参数
defaultConfig {
applicationId "com.hai.aosp"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
编译types
buildTypes {
release {
debuggable false
signingConfig signingConfigs.config //引用签名配置
minifyEnabled false//混淆,不能为true,否则断点时不能查看变量值
zipAlignEnabled false//Zipalign优化
shrinkResources false// 移除无用的resource文件
proguardFile 'E:/Workspace_studio/Test_android/app/proguard-rules.pro'
applicationIdSuffix 'release'
versionNameSuffix 'release'//版本名后缀
pseudoLocalesEnabled true //本地化支持管理
//修改apk文件名
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def fileName = "myapp_v${defaultConfig.versionName}_${releaseTime()}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
//manifest占位符
manifestPlaceholders = [
APP_NAME : "@string/app_name_uat"
]
buildConfigField('String', 'BUGLY_ID', rootProject.buglyid_uat)//自定义BuildConfig字段
}
}
- 配置全局Gradle参数,并引用
//在project的build.gradle添加
ext {
compileSdkVersion = 28
minSdkVersion =16
targetSdkVersion = 28
supportLibraryVersion = '28.0.0'
}
//引用
compileSdkVersion rootProject.ext.compileSdkVersion
//或者
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
}
- 自定义gradle文件,并使用
//创建config.gradle,内容如下
ext {
servers = [
debug : '"https://appdev.hai.cn"',//开发环境地址
release: '"https://app.hai.com"'//生产环境地址
]
version_code = 2111//应用版本号,每次发布新版本必须记得加1
version_name = '2.1.0'//应用版本名称
}
//在Project的gradle引入config.gradle
apply from: "config.gradle" // 引入全局配置文件
//使用config.gradle里的定义项
versionCode rootProject.version_code
buildConfigField('String', 'SERVER_ADDRESS', rootProject.servers.release)//引用数组项
buildConfigField('String', 'imgDir', '"imgDir"')//注意单引号里有双引号
- 使用gradle.properties的配置项
比如gradle.properties里配置如下:
VERSION_CODE=2
VERSION_NAME=1.0.0
ORG_CODE="000"
在module中的build.gralde引用
defaultConfig {
versionCode VERSION_CODE as int//直接写key名
versionName = "$VERSION_NAME"
buildConfigField('String', 'ORG_CODE', project.properties['ORG_CODE'])
}
- 自定义apk文件名和路径
android {
flavorDimensions "default"
productFlavors {
shandong {
versionName "3.0.2"
}
anhui {
versionName "3.3.3.1"
}
}
applicationVariants.all { variant ->
variant.outputs.all {
//指定apk输出目录
// println("目录="+project.projectDir.absolutePath)
// variant.getPackageApplication().outputDirectory = new File(project.projectDir.absolutePath + '/outputs/' + versionName)
def appName = "virgoTarot";
def time = new Date().format("yyyyMMddHHmmss", TimeZone.default)
def flavor=productFlavors[0].name+"-"+variant.buildType.name+"-"+productFlavors[0].versionName;
outputFileName = "${appName}-${flavor}-${time}.apk"
}
}
}