gradle常见设置
gradle配置
虽说自从转到android studio上开发后,接触gradle构建项目,但对于用法上还是模糊,经常用到的就一些配置,至于脚本也就在apk命名上用到,更高阶的脚本会在往后去了解
这些是在实际开发中碰到一些问题,以下会挑一些常用到的配置
支持ndk
gradle.properties
android.useDeprecatedNdk = true
apk打包时包含所需平台so
ndk {
abiFilters 'armeabi-v7a', 'x86' //只提取两种so
}
打包时排除的文件,可以是so,也会是META-INF下的文件 当初在解决64架位上用到,android的系统会优先历遍64下的so,如果不补全,造成so缺失,就会引起找不到相应so的异常
//移除fresco 64架构的so
packagingOptions{
exclude 'lib/arm64-v8a/libbitmaps.so'
exclude 'lib/arm64-v8a/libgifimage.so'
exclude 'lib/arm64-v8a/libimagepipeline.so'
exclude 'lib/arm64-v8a/libmemchunk.so'
exclude 'lib/arm64-v8a/libwebp.so'
exclude 'lib/arm64-v8a/libwebpimage.so'
exclude 'lib/x86_64/libbitmaps.so'
exclude 'lib/x86_64/libgifimage.so'
exclude 'lib/x86_64/libimagepipeline.so'
exclude 'lib/x86_64/libmemchunk.so'
exclude 'lib/x86_64/libwebp.so'
exclude 'lib/x86_64/libwebpimage.so'
}
签名
android {
signingConfigs {
release {
storeFile file("myreleasekey.keystore")
storePassword "123"
keyAlias "fylder"
keyPassword "123"
}
debug {
storeFile file("mydebugkey.keystore")
storePassword "123"
keyAlias "fylder"
keyPassword "123"
}
}
defaultConfig {
signingConfig signingConfigs.debug
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled true
}
}
}
ps:本地key配置,可将口令和密码在local.properties文件下,防止上传同步
keystore.alias=剑指锁妖塔
keystore.password=123
keystore.store_file=H\:\\photo\\fylder.jks
keystore.store_password=123
signingConfigs {
config {
Properties properties = new Properties()
//newDataInputStream()读取会出现中文乱码,采用读取字符,而不用读取字节
properties.load(project.rootProject.file('local.properties').newReader())
def ALIAS = properties.getProperty('keystore.alias')
def PASSWORD = properties.getProperty('keystore.password')
def STORE_FILE = properties.getProperty('keystore.store_file')
def STORE_PASSWORD = properties.getProperty('keystore.store_password')
keyAlias ALIAS
keyPassword PASSWORD
storeFile file(STORE_FILE)
storePassword STORE_PASSWORD
}
}
定义apk名字
android {
buildTypes {
debug {
applicationVariants.all { variant ->
versionApk(variant, defaultConfig)
}
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
versionApk(variant, defaultConfig)
}
}
}
}
// 定义输出apk的文件名
// debug和release都追加版本,release带有生成日期
def versionApk(variant, defaultConfig) {
def today = new Date()
variant.outputs.each { output ->
def file = output.outputFile
def appName = ".apk"
if (variant.name.equals("debug")) {
appName = "-v" + defaultConfig.versionName + ".apk"
} else if (variant.name.equals("release")) {
appName = "-v" + defaultConfig.versionName + "-" + today.format('yyyyMMdd') + ".apk"
}
output.outputFile = new File(
file.parent,
file.name.replace(".apk", appName))//更换apkName
}
}
支持lambda
build.gradle
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
app.gradle
apply plugin: 'me.tatarka.retrolambda'
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}