http://blog.chengyunfeng.com/?p=833
Gradle 奇技淫巧
Gradle 现在已经是 Android 开发必备的建构工具了。本文总结了一些在日常开发过程中会遇到的各种小技巧。
Android Gradle Plugin 的参考文档在这里 http://google.github.io/android-gradle-dsl/current/index.html
buildTypes 和 productFlavors
buildTypes 是指建构的类型,一般只用两种默认类型 debug 和 release ,顾名思义 debug 用来配置开发过程中的一些内容;release 用来配置正式发布版本的内容;这里面可以设置 applicationIdSuffix 、minifyEnabled 、name 等属性,所有的可配置属性参考这里 — BuildType 和 ProductFlavor。
productFlavors 指各种产品分类,比如免费版本和收费版本。有些定制软件可能会出很多版本,通过 productFlavors 就非常方便, 比如某某输入法就经常出各种定制版,这样以后管理代码和发版就方便多了。
例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
defaultConfig
{
applicationId
"org.goodev.material"
// 配置生成的 BuildConfig 文件中的常量
buildConfigField
"String"
,
"CHANNEL"
,
'"PLAY_STORE"'
}
buildTypes
{
debug
{
buildConfigField
"String"
,
"API_URL"
,
'"http://dev.goodev.org/"'
buildConfigField
"boolean"
,
"SHOW_LOG"
,
"true"
}
release
{
buildConfigField
"String"
,
"API_URL"
,
'"http://api.goodev.org/"'
buildConfigField
"boolean"
,
"SHOW_LOG"
,
"false"
signingConfig
signingConfigs
.
release
}
}
productFlavors
{
youmi
{
applicationId
=
"org.goodev.material.youmi"
buildConfigField
"String"
,
"CHANNEL"
,
'"YOUMI"'
resValue
"string"
,
"app_name"
,
"Material free"
}
pro
{
applicationId
=
"org.goodev.material.pro"
buildConfigField
"String"
,
"CHANNEL"
,
'"OTHER"'
resValue
"string"
,
"app_name"
,
"Material pro"
}
}
signingConfigs
{
release
{
storeFile
"${System.env.PRIVATE_KEY}"
keyAlias
"${System.env.ALIAS}"
storePassword
"${System.env.STORE_PW}"
keyPassword
"${System.env.APP_PW}"
}
}
|
保护私有数据
上面示例中还演示了如何通过 System.env 来访问系统环境变量中的值,这样你就可以把一些私有的内容排除在代码外,这样当你提交代码的时候,就不会泄露这些内容。还可以把私有数据放到local.properties 文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
signingConfigs
{
release
{
def
Properties
localProps
=
new
Properties
(
)
localProps
.
load
(
new
FileInputStream
(
file
(
'local.properties'
)
)
)
def
Properties
keyProps
=
new
Properties
(
)
if
(
localProps
[
'keystore.props.file'
]
!=
null
)
{
keyProps
.
load
(
new
FileInputStream
(
file
(
localProps
[
'keystore.props.file'
]
)
)
)
}
storeFile
keyProps
[
"store"
]
!=
null
?
file
(
keyProps
[
"store"
]
)
:
null
keyAlias
keyProps
[
"alias"
]
?
:
""
storePassword
keyProps
[
"storePass"
]
?
:
""
keyPassword
keyProps
[
"pass"
]
?
:
""
}
}
|
使用 Git tag 或者 hash 作为版本号
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// query git for the SHA, Tag and commit count. Use these to automate versioning.
def
gitSha
=
'git rev-parse --short HEAD'
.
execute
(
[
]
,
project
.
rootDir
)
.
text
.
trim
(
)
def
gitTag
=
'git describe --tags'
.
execute
(
[
]
,
project
.
rootDir
)
.
text
.
trim
(
)
def
gitCommitCount
=
100
+
Integer
.
parseInt
(
'git rev-list --count HEAD'
.
execute
(
[
]
,
project
.
rootDir
)
.
text
.
trim
(
)
)
defaultConfig
{
versionCode
gitCommitCount
versionName
gitTag
archivesBaseName
=
"material-${versionName}"
buildConfigField
"String"
,
"GIT_SHA"
,
"\"${gitSha}\""
}
|
使用建构的时间
1
2
3
4
5
6
7
8
|
def
buildTime
=
new
Date
(
)
.
format
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
,
TimeZone
.
getTimeZone
(
"UTC"
)
)
defaultConfig
{
buildConfigField
"String"
,
"BUILD_TIME"
,
"\"${buildTime}\""
// 还可以这样
buildConfigField
"long"
,
"BUILD_TIME_LONG"
,
"${System.currentTimeMillis()}L"
}
|
动态生成 Android Manifest 中的内容
为了让所有的 productFlavors 都可以安装到同一个手机上, 安卓系统要求不同的应用具有不同的 provider 等内容,如果冲突则其他应用无法安装,可以通过如下方式来解决该问题
1
2
3
4
5
6
7
|
<permission android
:
name
=
"${applicationId}.permission.XX"
android
:
protectionLevel
=
"signature"
/>
<uses-permission android
:
name
=
"${applicationId}.permission.XX"
/>
<provider android
:
name
=
".MyProvider"
android
:
authorities
=
"${applicationId}.provider"
…
/>
|
不同的 productFlavors 具有不同的 permission
有的应用,不同的版本可能要求的权限不一样,也就是不同的 productFlavors 所对应的 Manifest 文件内容是不同的,
除了通过上面的方式替代部分内容外,就要用到Manifest 文件合并功能了。
比如可以分别创建所有 productFlavors 对应的 AndroidManifest 文件:
app/src/free/AndroidManifest.xml
app/src/pro/AndroidManifest.xml
app/src/adsfree/AndroidManifest.xml
然后在默认的 Manifest 文件中写上所有的内容, 如果那个里面不需要某个 XML 属性,可以通过 tools:node=”remove” 来删除:
1
2
3
|
<permission
android
:
name
=
"com.android.vending.CHECK_LICENSE"
tools
:
node
=
"remove"
/>
|
另外还有一种方式就是,默认的 Manifest 文件中值写上共同的内容, 不同的内容分别写到对应的 productFlavors 的 Manifest 文件中。