Android Gradle 常用配置

添加内容:项目目录/build.gradle

android {
// 自动签名
signingConfigs {
println “\n[${project.name}]项目签名配置开始”

// 加载配置文件
String propertiesPath = “${rootDir.path}/keystore/keystore.properties”
File propertiesFile = file(propertiesPath)
// File propertiesFile = project.rootProject.file(‘keystore/keystore.properties’)
Properties properties = new Properties()
properties.load(propertiesFile.newDataInputStream())

// 配置自动签名
release {
storeFile file(properties[‘KEY_APP_STORE_FILE’])
// storeFile file(properties.getProperty(“KEY_APP_STORE_FILE”))
storePassword properties[‘KEY_STORE_PASSWORD’]
keyAlias properties[‘KEY_ALIAS’]
keyPassword properties[‘KEY_PASSWORD’]
}

println “签名配置文件路径:” + propertiesPath
println “[${project.name}]项目签名配置结束”
}

buildTypes {

debug {
signingConfig signingConfigs.release // 自动签名
}

release {
signingConfig signingConfigs.release
}
}
}

添加文件:配置文件,项目目录/keystore/keystore.properties

KEY_APP_STORE_FILE = …/keystore/keystore.jks
KEY_PASSWORD = release
KEY_ALIAS = release
KEY_STORE_PASSWORD = release

buildTypes:混淆、资源压缩优化、自定义编译类型等

android {

buildTypes {

release {
signingConfig signingConfigs.release
debuggable false // 不可调试
zipAlignEnabled true // zipalign 优化,minifyEnabled = true 时生效
shrinkResources true // 删除没有用到的资源,minifyEnabled = true 时生效
minifyEnabled true // 开启混淆

// 混淆规则
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}

// 自定义编译类型,注意一旦添加自定义编译类型,则所有被引用的 module 都要添加,否则导致除 debug 外其他编译类型无法打包
pre.initWith(release) // 以 release 为模板
pre {
debuggable true
}
}
}

关于自定义编译类型

  • 注意一旦添加自定义编译类型,则所有被引用的 module 都要添加,否则导致除 debug 外其他编译类型无法打包

关于混淆,有个自定义混淆

  • 目的:作为 SDK 时,有些内容即便是 app 打包也不能混淆 SDK 里面的内容。为了防止 app 集成时没有添加混淆规则导致的某些问题(如类找不到等),可以采用以下方式解决
  • 优点:使用该方式自定义混淆规则,可以使 app 打包时自动应用 aar 中自带的 proguard 文件,这样就不用让 app 额外添加混淆规则

添加文件:项目目录/build.gradle

android {
defaultConfig {
// 自定义混淆规则(SDK 用)
consumerProguardFiles ‘consumer-rules.pro’
}
}

修改输出 apk 的目录与名称,以及复制 apk 到指定目录

添加内容:项目目录/build.gradle

android {
// 修改输出 apk 的目录与名称,以及复制 apk 到指定目录
applicationVariants.all { variant ->

// 修改输出目录:一般不修改,防止 Studio 直接运行报到不到文件错误
// File outDir = new File(project.buildDir, “outputs/apk”)
// def artifact = variant.getPackageApplicationProvider().get()
// artifact.outputDirectory = outDir
//
// println “\n输出 apk 目录:” + outDir.getAbsolutePath()

// 修改输出 apk 文件名
variant.outputs.all {
String now = new Date().format(“yyyy-MM-dd”, TimeZone.getDefault())
outputFileName = “ p r o j e c t . n a m e − v {project.name}-v project.namev{versionName}- v a r i a n t . f l a v o r N a m e − {variant.flavorName}- variant.flavorName{variant.buildType.name}-${now}.apk”

println “输出 apk 名称:” + outputFileName
}

// 复制输出 apk 到指定文件夹
variant.assemble.doLast {
File outDir = new File(project.buildDir, “outputs/apk”)

variant.outputs.forEach { file ->
copy {
from file.outputFile
into outDir
// rename {
// String fileName -> “ p r o j e c t . n a m e − v {project.name}-v project.namev{versionName}- v a r i a n t . f l a v o r N a m e − {variant.flavorName}- variant.flavorName{variant.buildType.name}-${now}.apk”
// }
}

println “\n复制 apk:” + file.outputFile
println “到指定目录:” + outDir.getAbsolutePath()
}
}
}
}

配置自定义常量与资源

添加内容:项目目录/build.gradle

添加位置不止以下,也可以在 build.gradle 的其他地方

android {

defaultConfig {
println “\n[${project.name}]配置自定义常量与资源中…”

// 读取自定义配置文件
Properties properties = new Properties()
properties.load(file(“${rootDir.path}/keystore/keystore.properties”).newDataInputStream())

// 自定义常量
def assembleTime = new Date().format(“yyyy-MM-dd HH:mm”, TimeZone.getDefault())

buildConfigField “String”, “ASSEMBLE_TIME”, “”KaTeX parse error: Expected group as argument to '\"' at end of input: …KEY_ALIAS", "\"{properties.getProperty(“KEY_ALIAS”)}“”

// 自定义资源
resValue “string”, “app_name”, “Sample”

println “[${project.name}]配置自定义常量与资源结束”
}

添加内容:根目录/gradle.properties

DEV_USER = “user”

添加文件:配置文件,项目目录/keystore/keystore.properties

KEY_APP_STORE_FILE = …/keystore/keystore.jks
KEY_PASSWORD = release
KEY_ALIAS = release
KEY_STORE_PASSWORD = release

修改内容:项目目录/…/strings.xml

多渠道

添加内容:项目目录/build.gradle

android {

defaultConfig {
// 多渠道用
flavorDimensions “applicationId”
}

// 多渠道
productFlavors {
// 正式版本
publish {
}

// 开发渠道
dev {
applicationIdSuffix “.dev” // 修改包名
}

// 测试渠道
dtest {
}
}

// 多渠道:根据渠道,自定义常量与资源
applicationVariants.all { variant ->

def buildType = variant.buildType // 编译类型
def flavorName = variant.flavorName // 渠道名称

// 开发意图、测试意图、生产意图
def isDev = flavorName.startsWith(“dev”)
def isTest = flavorName.startsWith(“dtest”)
def isRelease = !isTest && !isDev

// 调试总开关
def isDebuggable = isDev || isTest || buildType.debuggable || buildType.name == “debug”

// 应用名称
def appName = “Sample”
if (isDebuggable)
appName = appName + “(${variant.flavorName})”

// 自定义常量与资源
resValue “string”, “app_name”, “${appName}”

buildConfigField “boolean”, “IS_DEBUGGABLE”, “ i s D e b u g g a b l e " / / 请不要把 I S D E B U G G A B L E 改成 D E B U G G A B L E ,避免混淆 b u i l d C o n f i g F i e l d " b o o l e a n " , " I S D E V " , " {isDebuggable}" // 请不要把 IS_DEBUGGABLE 改成 DEBUGGABLE,避免混淆 buildConfigField "boolean", "IS_DEV", " isDebuggable"//请不要把ISDEBUGGABLE改成DEBUGGABLE,避免混淆buildConfigField"boolean","ISDEV","{isDev}”
buildConfigField “boolean”, “IS_TEST”, “ i s T e s t " b u i l d C o n f i g F i e l d " b o o l e a n " , " I S R E L E A S E " , " {isTest}" buildConfigField "boolean", "IS_RELEASE", " isTest"buildConfigField"boolean","ISRELEASE","{isRelease}”

println “\n[ p r o j e c t . n a m e ] 多渠道:根据渠道,配置自定义常量与资源情况: " p r i n t l n " 编译渠道: {project.name}]多渠道:根据渠道,配置自定义常量与资源情况:" println "编译渠道: project.name]多渠道:根据渠道,配置自定义常量与资源情况:"println"编译渠道:{variant.flavorName}”
println “编译类型:” + variant.buildType.name
println “应用名称:” + appName
println “调试总开关:” + isDebuggable
println “开发意图:” + isDev
println “测试意图:” + isTest
println “生产意图:” + isRelease
}
}

全局统一管理版本号

添加文件:依赖配置文件,项目目录/gradle/dependencies.gradle

ext {
// sdk version
compileSdkVersion = 30
minSdkVersion = 19
targetSdkVersion = 30

// java version
sourceCompatibilityVersion = JavaVersion.VERSION_1_8
targetCompatibilityVersion = JavaVersion.VERSION_1_8

dep = [
// 基本
appcompat : ‘androidx.appcompat:appcompat:1.2.0’,
recyclerview : ‘androidx.recyclerview:recyclerview:1.1.0’,
constraintlayout: ‘androidx.constraintlayout:constraintlayout:2.0.4’,

// 测试单元
junit : ‘junit:junit:4.13.1’,
junit_test : ‘androidx.test.ext:junit:1.1.2’,
espresso_core : ‘androidx.test.espresso:espresso-core:3.3.0’,
]
}

添加内容:根目录/build.gradle

apply from: “${rootDir.path}/gradle/dependencies.gradle”

修改内容:项目目录/build.gradle

android {

compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
}

// java 1.8
compileOptions {
sourceCompatibility rootProject.ext.sourceCompatibilityVersion
targetCompatibility rootProject.ext.targetCompatibilityVersion
}
}

dependencies {
implementation fileTree(dir: ‘libs’, include: [‘.jar’, '.aar’])
implementation dep.appcompat
implementation dep.recyclerview
implementation dep.constraintlayout

// 单元测试
testImplementation dep.junit
androidTestImplementation dep.junit_test
androidTestImplementation dep.espresso_core
}

多个 module 共用 gradle 配置

添加文件:通用配置文件,根目录/gradle/config.gradle

apply plugin: ‘com.android.library’

android {
compileSdkVersion rootProject.ext.compileSdkVersion

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion

testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner”
consumerProguardFiles ‘consumer-rules.pro’
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}
}

}

dependencies {
implementation fileTree(dir: ‘libs’, include: [‘.jar’, '.aar’])
implementation dep.appcompat

testImplementation dep.junit
androidTestImplementation dep.junit_test
androidTestImplementation dep.espresso_core
}

修改内容:项目目录/build.gradle

apply from: “${rootDir.path}/gradle/config.gradle”

android {

defaultConfig {
versionCode 1
versionName “1.0.0”
}
}

dependencies {

// 其他 Module
implementation project(‘:lib-utils’)
}
可使用场景

  • 解决 annotationProcessor 不能共用的问题(该编译指令不能像 api 一样能对外可见),不过一般也不是这么用的,这里只是做个记录
    自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

我一直以来都有整理练习大厂面试题的习惯,有随时跳出舒服圈的准备,也许求职者已经很满意现在的工作,薪酬,觉得习惯而且安逸。

不过如果公司突然倒闭,或者部门被裁减,还能找到这样或者更好的工作吗?

我建议各位,多刷刷面试题,知道最新的技术,每三个月可以去面试一两家公司,因为你已经有不错的工作了,所以可以带着轻松的心态去面试,同时也可以增加面试的经验。

我可以将最近整理的一线互联网公司面试真题+解析分享给大家,大概花了三个月的时间整理2246页,帮助大家学习进步。

由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是部分内容截图:

部分目录截图

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

吗?

我建议各位,多刷刷面试题,知道最新的技术,每三个月可以去面试一两家公司,因为你已经有不错的工作了,所以可以带着轻松的心态去面试,同时也可以增加面试的经验。

我可以将最近整理的一线互联网公司面试真题+解析分享给大家,大概花了三个月的时间整理2246页,帮助大家学习进步。

由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是部分内容截图:

[外链图片转存中…(img-Nv5vYjCB-1713715104471)]

[外链图片转存中…(img-oMP0L52U-1713715104472)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值