一、嫌弃Gradle中各种版本太乱,我们可以统一管理
我们可以在Project 下的build.gradle下定义ext属性:
ext {
versionCompileSdk =27
versionTargetSdk =27
versionBuildTools ='27.0.3'
versionSupportLibirary = '27.1.1'
localString = "Hello from build.gradle"
}
属性直接写在顶层,不能放在其他结构下。
在Moudle的build.gradle下引用:
android {
compileSdkVersion versionCompileSdk
defaultConfig {
...
targetSdkVersion versionTargetSdk
...
}
buildToolsVersion versionBuildTools
}
dependencies {
...
implementation "com.android.support:appcompat-v7:${versionSupportLibirary}" //注意这里是双引号
...
}
二、设置debug和release时的一些常量
buildTypes {
debug {
buildConfigField "String", "API_ URL", "\"http:// test. example. com/ api\""
buildConfigField "boolean", "LOG_ DEBUG", "true"
minifyEnabled false
proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro'
}
release {
buildConfigField "String", "API_ URL", "\"http:// test. example. com/ api\""
buildConfigField "boolean", "LOG_ DEBUG", "false" // 代码中调用 BuildConfig.LOG_DEBUG
resValue "string", "whatEnv", "ExampleDEBUG" // 配置资源值 R.string.whatEnv
minifyEnabled false
proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro'
}
}
三、签名配置
android {
signingConfigs{
release{
storeFilefile("YSZN.jks") //指定keystore文件的路径
storePassword "secretpassword"
keyAlias "gradleforandroid"
keyPassword "secretpassword"
}
}
buildTypes {
release {
......
signingConfig signingConfigs.release
}
}
}