android组件化学习-config.gradle统一配置sd
config.gradle文件的配置以及使用
在项目根目录下新建config.gradle文件
并在项目build.gradle中加入apply from: "config.gradle"
具体配置:
build.gradle配置
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config.gradle"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
// 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
}
config.gradle配置
ext {
isRelease = true
// sdk版本信息配置
androidId = [
minSdkVersion : 26,
targetSdkVersion : 28,
compileSdkVersion : 28,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "android.support.test.runner.AndroidJUnitRunner"
]
// 每个模块包名配置
appId = [
app : "com.yan.appmodule",
library: "com.yan.library"
]
// 生产/测试环境配置
url = [
debug : "http://123.34.55/debug",
release: "http://123.34.55/release",
]
//三方依赖库配置
supportLibraryVersion = "28.0.0"
supportDependencies = [
appcompat : "com.android.support:appcompat-v7:${supportLibraryVersion}",
constraint: "com.android.support.constraint:constraint-layout:1.1.3",
junit : "junit:junit:4.12",
runner : "com.android.support.test:runner:1.0.2",
espresso : "com.android.support.test.espresso:espresso-core:3.0.2"
]
}
app和library中使用统一配置
apply plugin: 'com.android.application'
//使用项目config.gradle统一配置
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def supportDependencies = rootProject.ext.supportDependencies
def url = rootProject.ext.url
android {
compileSdkVersion androidId.compileSdkVersion
defaultConfig {
applicationId appId.app
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
testInstrumentationRunner androidId.testInstrumentationRunner
}
buildTypes {
debug {
buildConfigField("String", "debug", "\"${url.debug}\"")// 将测试环境url配置到BuildConfig中
}
release {
buildConfigField("String", "release", "\"${url.release}\"")// 将生产环境url配置到BuildConfig中
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(":library")
// implementation 'com.android.support:appcompat-v7:28.0.0'
// implementation 'com.android.support.constraint:constraint-layout:1.1.3'
// testImplementation 'junit:junit:4.12'
// androidTestImplementation 'com.android.support.test:runner:1.0.2'
// androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//一般写法
implementation supportDependencies.appcompat
implementation supportDependencies.constraint
testImplementation supportDependencies.junit
androidTestImplementation supportDependencies.runner
androidTestImplementation supportDependencies.espresso
}
如下是生成的BuildConfig文件,一般在此目录\app\build\generated\source\buildConfig\debug\com\***\appmodule\BuildConfig.java
/**
* Automatically generated file. DO NOT MODIFY
*/
package com.***.appmodule;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.yan.appmodule";
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 String debug = "http://123.34.55/debug";
}
欢迎关注微信公众号:
![](https://img.haomeiwen.com/i4660593/fbf83267362a791b.jpg)