在 Project/build.gradle 中定义, 在 module/build.gradle 中使用
1, 直接在 Project/build.gradle 中定义和引用:
- // Top-level build file where you can add configuration options common to all sub-projects/modules.
- buildscript {
- ext.compileSdkVersion = 26
- ext.targetSdkVersion = 26
- ext.support_appcompat_v7 = '26.1.0'
- repositories {
- google()
- jcenter()
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:3.0.1'
- // NOTE: Do not place your application dependencies here; they belong
- // in the individual module build.gradle files
- }
- }
数字引用:
compileSdkVersion rootProject.ext.compileSdkVersion
三方库版本号引用:
implementation "com.android.support:appcompat-v7:$support_appcompat_v7"
2, 单独在 xxx.gradle 中定义和引用:
1在 Project 层级下新建 config.build 文件 (这里的 config 可以替换为任何你喜欢的名字), 在里面书写配置信息:
- ext {// 定义所有 project 公用参数
- Android = [
- compileSdk: 27,
- buildTools: "27.0.3",
- minSdk : 19,
- minLimitSdk: 19,// 限制低版本用户安装
- targetSdk : 27,
- ]
- dependencies = [
- // App dependencies
- junit : '4.12',
- espresso : '2.2.2',
- supportLibraryVersion: '27.1.1',
- supportPercentVersion: '25.3.1',
- butterknife : '8.8.1',
- gson : '2.7',
- retrofit : '2.4.0',
- rxjava : '1.1.6',
- rxandroid : '1.2.1',
- loggingInterceptor : '3.1.0',
- stetho : '1.4.2',
- guavaVersion : '18.0',
- leakcanary : '1.5.4'
- ]
- }
2在 Project/build.gradle 中引用刚才定义好的 config.gradle 文件:
apply from: "config.gradle"
3数字引用:
compileSdkVersion rootProject.ext.Android.compileSdk
4三方库版本号引用:
API "com.android.support:appcompat-v7:$rootProject.ext.dependencies.supportLibraryVersion"
在 Project/gradle.properties 中配置, 在 mudule/build.gradle 中使用.
1在 Project/gradle.properties 中定义:
- COMPILE_SDK_VERSON = 26
- BUILD_TOOLS_VERSION = 25.0.2
- SUPPORTV7_VERSON=25.0.1
2引用到的变量默认是 String 类型, 如果需要 in 类型, 需要在后面添加 as int 声明
- compileSdkVersion COMPILE_SDK_VERSON as int
- buildToolsVersion BUILD_TOOLS_VERSION
- compile "com.android.support:appcompat-v7:${SUPPORTV7_VERSON}"
读者福利限时分享
Android 开发资料 + 面试架构资料 免费分享 点击链接 即可领取
《Android 架构师必备学习资源免费领取 (架构视频 + 面试专题文档 + 学习笔记)》
来源: http://www.jianshu.com/p/cae1e7f50fd8