Android 项目实例config.gradle配置

gradle支持自定义config.gradle,在GoogleSamples中提到我们必须使用关键字ext(对应ExtraPropertitesExtension的实例)来定义动态属性

  • config.gradle

config.gradle

自定义一个config.gradle,将一些配置信息及第三方依赖统一管理。代码如下

ext {//这里的ext是必须的 android = [ compileSdkVersion : 25, buildToolsVersion : ‘25.0.0’, applicationId : ‘com.alpha.alphaapp’, minSdkVersion : 17, targetSdkVersion : 25, versionCode : 1, versionName : ‘v 1.1.0’, defaultPublishConfig: ‘release’, publishNonDefault : true ] dependencies = [ "appcompat-v7" : ‘com.android.support:appcompat-v7:25.1.0’, "support-design" : ‘com.android.support:design:+’, "support-v4" : ‘com.android.support:support-v4:25.3.1’, "roundedimageview" : ‘com.makeramen:roundedimageview:2.2.1’, "Android-PickerView": ‘com.contrarywind:Android-PickerView:3.2.5’, "XTabLayout" : ‘com.androidkun:XTabLayout:1.1.1’, "status-bar-compat" : ‘com.githang:status-bar-compat:0.3’, "recyclerview-v7" : ‘com.android.support:recyclerview-v7:23.2.0’, "crashreport" : ‘com.tencent.bugly:crashreport:latest.release’, "crashreport-native": ‘com.tencent.bugly:nativecrashreport:latest.release’, "junit" : ‘junit:junit:4.12’, "volley" : ‘com.android.volley:volley:1.0.0’, "okhttp" : ‘com.squareup.okhttp3:okhttp:3.2.0’, "gson" : ‘com.google.code.gson:gson:2.8.0’, "glide" : ‘com.github.bumptech.glide:glide:3.7.0’, "rxandroid" : ‘io.reactivex:rxandroid:1.2.1’, "rxjava" : ‘io.reactivex:rxjava:1.3.0’ ]}

在app(module)目录下的build.gradle文件中使用如下配置:

apply plugin: ‘com.android.application’android { //在android 标签下,唯一的属性就是compileSdkVersion,buildToolsVersion compileSdkVersion rootProject.ext.android["compileSdkVersion"] buildToolsVersion rootProject.ext.android["buildToolsVersion"] defaultConfig { applicationId rootProject.ext.android["applicationId"] minSdkVersion rootProject.ext.android["minSdkVersion"] targetSdkVersion rootProject.ext.android["targetSdkVersion"] versionCode rootProject.ext.android["versionCode"] versionName rootProject.ext.android["versionName"] testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } …………dependencies { //当有很多包时,使用这个就是将所有libs下添加为依赖 compile fileTree(include: [‘*.jar’], dir: ‘libs’) androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2’, { exclude group: ‘com.android.support’, module: ‘support-annotations’ }) testCompile rootProject.ext.dependencies["junit"] // 这里不需要添加这么多compile,第一行就是执行该操作的 // compile files(‘libs/open_sdk_r5788_lite.jar’) // compile files(‘libs/bottomView.jar’) // compile files(‘libs/xUtils-2.6.14.jar’) // //微信 记得后面修改为gradle模式 //第三方依赖库 compile rootProject.ext.dependencies["appcompat-v7"] compile rootProject.ext.dependencies["support-design"] compile rootProject.ext.dependencies["support-v4"] compile rootProject.ext.dependencies["roundedimageview"] compile rootProject.ext.dependencies["Android-PickerView"] compile rootProject.ext.dependencies["XTabLayout"] compile rootProject.ext.dependencies["status-bar-compat"] compile rootProject.ext.dependencies["okhttp"] //腾讯bugly日志 compile rootProject.ext.dependencies["crashreport"]; compile rootProject.ext.dependencies["crashreport-native"] //依赖其他两个library releaseCompile project(path: ‘:lib_sdk’, configuration: ‘release’) releaseCompile project(path: ‘:lib_stub’, configuration: ‘release’) debugCompile project(path: ‘:lib_sdk’, configuration: ‘debug’) debugCompile project(path: ‘:lib_stub’, configuration: ‘debug’)}

使用这种方式的好处是当Android Support Repository有更新时
可以直接在gradle文件中展现提示,又有提示,又能统一管理依赖版本号,一举两得

注意:这里要在想使用这个config.gradle,我们要在project下的build.gradle中添加 apply from : "config.gradle"

添加后build.gradle的代码如下:

apply from : "config.gradle"buildscript { repositories { jcenter() //是一个类似于github的代码托管仓库,声明了此配置, //可以轻松引用 jcenter上的开源项目 } dependencies { classpath ‘com.android.tools.build:gradle:2.2.0’ //gradle是一个强大的项目构建工具,不仅可以构建Android,还可以构建java,C++等 //此处引用android的插件 // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}

这样在library,app的build.gradle中使用依赖时,就可以如上面的方式进行依赖。