源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android 自定义gradle property详解及实例代码

  • 时间:2022-02-24 15:51 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android 自定义gradle property详解及实例代码
[b]Android 自定义gradle property[/b] 在Android studio上运行项目,gradle的配置是必不可少的,但是随着项目的逐渐成长,迎面而来的就是.各种依赖包的添加,数不胜数的签名,渠道包等,整个gradle变得很乱,这样其实我们可以将gradle的部分内容分离出来放在另一个自定义gradle内. 如这时我们添加的Plugin 就只要对其赋值就可以了. [list=1] [*]步骤:[/*] [*]在总项目根目录下创建 dependencies.gradle文件(名字可以自定义)[/*] [*]根目录下创建的自定义内容如下:[/*] [/list] 需要注意的是在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
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部