Android 自定义gradle property
在Android studio上运行项目,gradle的配置是必不可少的,但是随着项目的逐渐成长,迎面而来的就是.各种依赖包的添加,数不胜数的签名,渠道包等,整个gradle变得很乱,这样其实我们可以将gradle的部分内容分离出来放在另一个自定义gradle内. 如这时我们添加的Plugin 就只要对其赋值就可以了.
需要注意的是在dependencies.gradle文件中向Project添加额外的Property时,我们并不能直接定义,而是应该通过ext来定义。
一般我们使用闭包的方式,代码如下:
ext {
//添加supportLibraryVersion属性
supportLibraryVersion = '23.1.1'
//添加dependenciesretrofitVersion属性
dependenciesretrofitVersion = '2.0.0-beta2'
//添加dependencies数组
dependencies = [
retrofit : "com.squareup.retrofit:retrofit:$retrofitVersion",
retrofitConverterGson: "com.squareup.retrofit:converter-gson:$retrofitVersion",
retrofitAdapterRxJava: "com.squareup.retrofit:adapter-rxjava:$retrofitVersion",
]
}
也可以不使用闭包的
//添加supportLibraryVersion属性
ext.supportLibraryVersion = '23.1.1'
//添加dependenciesretrofitVersion属性
ext.dependenciesretrofitVersion = '2.0.0-beta2'
当然gradle其实已经提供了很多自定义的property,一些常用的有:
project:Project本身
name:Project的名
description:Project的描述
version:Project的版本号
path:Project的绝对路径
buildDir:Project构建结果存放目录
在根目录的build.gragle下添加
apply from: 'dependencies.gradle'
接着在知道app工程的builde.gradle下添加依赖如下:
dependencies {
//获取 dependencies.gradle 自定义的数组
Map<String, String> dependencies = rootProject.ext.dependencies
compile dependencies.retrofit
compile dependencies.retrofitConverterGson
compile dependencies.retrofitAdapterRxJava
}
当然也可以配置其他,如 defaultConfig
当前app下的defaultConfig,引用dependencies.gradle 配置的参数
defaultConfig { minSdkVersion rootProject.ext.androidMinSdkVersion targetSdkVersion rootProject.ext.androidTargetSdkVersion }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。