自己总结了下gradle在安卓中的配置经验,只当做是记录,后面忘记咋配了,可以在这里找得到,当前关于gradle的知识点还有很多,只能慢慢学习,积累了。
apply plugin: 'com.android.application'
//定义编译时间
def compileTime() {
return new Date().format("_yyyy.MM.dd'T'HH:mm_") + "bate"
}
android {
compileSdkVersion 22//安卓编译版本,最好和target版本一致
buildToolsVersion '25.0.0'//tools版本,最好更新到最新的
defaultConfig {//代码中需要动态配置的参数,都可以在这里配置;这里的配置项,同样可以在productFlavors每个变体中配置
applicationId "com.raise.fota"//包名
minSdkVersion 19//最小兼容版本
// publishNonDefault true//不常用,library编译时,默认都是release,开启此配置,在依赖中可配置library成debug编译模式:http://stackoverflow.com/questions/20176284/buildconfig-debug-always-false-when-building-library-projects-with-gradle
targetSdkVersion 22//目标版本,决定是否使用兼容模式运行;比如设置为22,就可以不用考虑写权限申明代码,因为从23开始才需要动态申请权限
versionCode 38
versionName "1.0.0.$versionCode"//将versionCode值放在后面
buildConfigField("String", "COMPILE_TIME", "\"${compileTime()}\"")//配置编译时间,编译完成将会在BuildConfig.clsaa中生成常量;在写代码时,可以使用BuildConfig.COMPILE_TIME获取编译时间;注意声明String类型时,双引号需要转义;
manifestPlaceholders = [//此处配置参数,可在AndroidManifest.xml清单文件中引用变量
app_name:"raise"//配置应用名称,AndroidManifest.xml文件中使用:android:label="${app_name}"
]
ndk {//ndk开发配置
abiFilters "armeabi", "arm64-v8a"//编译so的abi类型
}
}
//读取签名配置文件信息
def File propFile = new File('signing.properties')
if (propFile.canRead()) {
def Properties props = new Properties()
props.load(new FileInputStream(propFile))
if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&
props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {
android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])
android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']
android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']
android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']
println 'all good to go'
} else {
android.buildTypes.release.signingConfig = null
println 'signing.properties found but some entries are missing'
}
} else {
println 'signing.properties not found'
android.buildTypes.release.signingConfig = null
}
signingConfigs {//签名配置,申明
raise {//签名的名称
keyAlias 'raise'
keyPassword '111222'
storeFile file('raise.jks')
storePassword '111222'
}
}
buildTypes {//构建编译类型
release {//release编译时配置参数
minifyEnabled true//是否使用Proguard混淆
shrinkResources true//是否移除没有用到的资源(必须配置minifyEnabled true 才会生效)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//配置混淆配置文件
signingConfig signingConfigs.raise//设置签名
}
debug {//debug编译时配置参数
// applicationIDsuffix '.debug'//包名后缀,设置后可以同时安装2个版本
// versionNameSuffix '-debug'//versionName后缀
debuggable true //启用debug的buildType配置
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//编译构建时的构建类型,对打包之后的apk文件做相关操作,下例示例表示修改apk文件名称和位置
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "${variant.productFlavors[0].name}_${releaseTime()}.apk"
output.outputFile = new File("D:\\apk", fileName)
}
}
}
productFlavors {//此处可以构建变体,产生多种渠道的apk包
withIcon {
// applicationId "com.raise.fota.withIcon"//配置包名
manifestPlaceholders = [
app_name:"raise_withIcon"
]
}
mobile {
}
}
//过滤buildType类型
variantFilter { variant ->
if (variant.name.equals("mobile") &&
variant.buildType.name.('debug')) {
variant.setIgnore(true)
}
}
externalNativeBuild {//配置native编译时参数
cmake {
path 'CMakeLists.txt'//cmake编译器路径
}
}
//指定java版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
//编译时过滤某些任务,加快编译时间
tasks.whenTaskAdded { task ->
if (task.name.contains("lint")//检查代码
|| task.name.equals("clean")//清除之前编译
|| task.name.contains("mockableAndroidJar")//mockable测试
|| task.name.contains("UnitTest")//单元测试
) {
task.enabled = false;
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])//编译libs文件夹下的所有jar
provided 'com.google.code.gson:gson:2.8.0'//只编译,不打包;适合多个library的情况
compile files('libs/httpmime-4.1.3.jar')//单独编译某个jar
compile 'com.squareup.okhttp:okhttp:2.7.0'//编译maven上的库
testCompile 'junit:junit:4.12'//测试运行时,才编译
compile project(path: ':downutil')//编译library
compile(name:'down-release', ext:'aar')//编译aar
}
//自定义任务,在编译完成后,运行cmd文件做某些操作
build.doLast {
println "build.doLast start."//打印console
java.lang.Runtime runtime = new Runtime();
runtime.exec("cmd /c start D:\\apk\\moveFile.bat")
}
maven私有仓库
//
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
// maven{ url ''}