在project下创建config.gradle文件:
作用:
1.统一性:每个modle用同一的版本等
2.便于管理:修改一次就能让每个modle都修改了
3.版本管理
步骤:
1,在project右键new,点击file,创建config.gradle文件;
ext{
//android配置
android=[ compileSdkVersion:28,
applicationId :"com.example.meet",
minSdkVersion :16,
targetSdkVersion: 28,
versionCode :1,
versionName :"1.0",
testInstrumentationRunner :"androidx.test.runner.AndroidJUnitRunner"
]
dependencies =[
"appcompat":'androidx.appcompat:appcompat:1.0.2'
]
2,在project下的build.gadle引入config.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
//引入自定义gradle配置文件
apply from:"config.gradle"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3,引用gradle常量:将app下的buile.gradle中的android和dependencies属性用config.gradle中的替代;
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
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 rootProject.ext.android["testInstrumentationRunner"]
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.dependencies["appcompat"]
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
4,自定义gradle常量:
在app的build.gradle下用buildConfigField()方法添加,第一个参数为类型,第二参数为名字,第三个参数为值,这里添加了一个名叫LOG_DEBUG,值为true的布尔型常量。
buildTypes {
debug{
buildConfigField("boolean","LOG_DEBUG","true")
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
5,查看常量:在MainActivity中写上BuildConfig,ctrl+B查看源码: Fields //from build type: debug下的就是自定义gradle常量Log_DEBUG;
(注意:如果在写上BuildConfig提示没有该类,选中找不到BuildConfig类的module ,Build ——> Make Module“module name”,生成BuildConfig类,系统自动补充import语句)
package com.example.meet;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.meet";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from build type: debug
public static final boolean LOG_DEBUG = true;
}