在module的gradle文件中读取project下 build.gradle
project下 build.gradle:
ext {
compileSdk = 28
minSdk = 17
targetSdk = 28
support = "28.0.0"
buildTools = "28.0.0"
supportLibraryVersion = "28.0.0"
versionCode = 4
versionName = "1.0.4"
cfgs = [
utilcodeVersion: "1.23.0",
eventbusVersion: "3.0.0",
glideVersion: "3.7.0",
retrofitVersion: "2.4.0",
rxandroidVersion: "1.1.0",
rxjavaVersion: "1.1.0",
okhttpVersion: "3.5.0",
loggingInterceptorVersion: "3.0.0",
autosizeVersion: "1.1.2",
baseRecyclerViewAdapterHelperVersion: "2.9.30",
lottieVersion: "2.1.0",
fastjsonVersion: "1.2.47",
qmuiVersion: "1.1.3",
bottomNavigationBarVersion: "2.1.0",
recyclerViewVersion: "28.0.0",
zhihuMatisse:"0.5.2-beta2"
]
}``
在module中引用
defaultConfig {
applicationId “com.star.commodity.release”
minSdkVersion rootProject.ext.minSdk
targetSdkVersion rootProject.ext.targetSdk
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
multiDexEnabled true
}
…
implementation “com.android.support:appcompat-v7:
s
u
p
p
o
r
t
L
i
b
r
a
r
y
V
e
r
s
i
o
n
"
i
m
p
l
e
m
e
n
t
a
t
i
o
n
"
c
o
m
.
b
l
a
n
k
j
:
u
t
i
l
c
o
d
e
:
{supportLibraryVersion}" implementation "com.blankj:utilcode:
supportLibraryVersion"implementation"com.blankj:utilcode:{cfgs.utilcodeVersion}”
``
2,在module的gradle文件中读取gradle.properties
gradle.properties定义变量
isModule=false
在module 的gradle 中读取
isModule.toBoolean() 或者 直接取isModule就可以了
3,在gradle文件中读取local.properties
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def sdkDir = properties.getProperty('sdk.dir')
def ndkDir = properties.getProperty('ndk.dir')
4,在代码中读取gradle.propeties
第一步:在gradle.properties中添加属性
IS_USE_IJKPLAYER_FOR_VOD=false
第二步: 在build.gradle文件中的android 下的 defaultConfig中定义如下:String要加转移符 Boolean不需要,注意此方式也可以读取build.gradle中的定义的变量
defaultConfig {
//java代码中引用的定义形式
buildConfigField "Boolean", "IS_USE_IJKPLAYER_FOR_VOD", "${IS_USE_IJKPLAYER_FOR_VOD}"
}
第三步:代码中读取
private boolean isUseIjkPlayer = BuildConfig.IS_USE_IJKPLAYER_FOR_VOD;
附实战示例:
示例一,利用gradle.properties适配多个项目
第一步:在gradle.properties中添加属性
CUR_PROJECT=UGA
第二步:在App下的build.gradle中写一个动态获取的方法
def getCurProjectVersionName(){
if (CUR_PROJECT.equals("UGA")) {
return "2.0.5"
} else {
return "2.3.1"
}
}
def getCurProjectVersionCode() {
if (CUR_PROJECT.equals("UGA")) {
return 2000005
} else {
return 2003001
}
}
第三步:调用方法获取版本信息
defaultConfig {
versionCode getCurProjectVersionCode()
versionName getCurProjectVersionName()
signingConfig signingConfigs.config
multiDexEnabled true
}