
主要文件如上图:
//如果没看懂,可以先看下面几篇:
1. 统一SDK版本管理配置
2. build.gradle 使用each循环引入第三方框架
3. build.gradle中buildConfigField使用
4. build.gradle中buildConfigField使用
注意:以下第1、2、 3点是配置信息,第4是寻找配置的文件,第5是最终引用
1. app目录下的sign.gradle(test.jks 签名信息)
ext {
//签名信息
STORE_FILE = "test.jks"
STORE_PASSWORD = "123456"
KEY_ALIAS = "123456"
KEY_PASSWORD = "123456"
}
2.项目根目录下config包下app.gradle (App主要配置信息)
ext {
//sdk版本管理
COMPILE_SDK_VERSION = 26 // 用于编译的SDK版本
BUILD_TOOLS_VERSION = "28.0.3" // 用于Gradle编译项目的工具版本
APPLICATION_ID = "com.liys.gradleconfig" //包名
MIN_SDK_VERSION = 19 // 最低支持Android版本
TARGET_SDK_VERSION = 26 // 目标版本
VERSION_CODE = 1 //版本号
VERSION_NAME = "1.0"
MINIFY_ENABLED = false //是否混淆
//日志管理
LOG_DEBUG_TYPE = "boolean" //是否输出日志 变量类型
LOG_DEBUG_NANE = "LOG_DEBUG" //是否输出日志 变量名
LOG_DEBUG_DEBUG = "true" //是否输出日志(debug版)
LOG_DEBUG_RELEASE = "false" //是否输出日志(正式版)
//baseUrl地址
BASE_URL_TYPE = "String" //返回值
BASE_URL_NAME = "BASE_URL" //变量名
BASE_URL_RELEASE = "\"http:\"" //线上地址
BASE_URL_DEBUG = "\"http:\"" // 本地地址
// App dependencies
appcompatVersion = "26.1.0"
constraintVersion = "1.1.3"
junitVersion = "4.12"
runnerVersion = "1.0.2"
espressoVersion = "3.0.2"
//公共的(系统的)
roots = [
//项目基本的(每个项目需要的)
"appcompatV7" : "com.android.support:appcompat-v7:${appcompatVersion}",
"constraint" : "com.android.support.constraint:constraint-layout:${constraintVersion}",
"junit" : "junit:junit:${junitVersion}",
"runner" : "com.android.support.test:runner:${runnerVersion}",
"espresso" : "com.android.support.test.espresso:espresso-core:${espressoVersion}",
]
//第三方框架
thirdFrame = [
"gson" :"com.google.code.gson:gson:2.8.0", //gson解析
"glide" :"com.github.bumptech.glide:glide:3.7.0", //glide
]
//App需要导入的包
app = [
thirdFrame.gson,
thirdFrame.glide,
]
}
3.项目根目录下config包下source.gradle (资源分包目录)
ext{
//res资源文件
resSource = [
'src/main/res',
'src/main/res-main/home',
]
}
4.项目根目录下build.gradle,尾部加上:
apply from: 'config/app.gradle'
apply from: 'config/source.gradle'
5.app目录下build.gradle
apply plugin: 'com.android.application'
apply from: 'sign.gradle'
android {
//SDK和App版本
compileSdkVersion COMPILE_SDK_VERSION
defaultConfig {
applicationId APPLICATION_ID
minSdkVersion MIN_SDK_VERSION
targetSdkVersion TARGET_SDK_VERSION
versionCode VERSION_CODE
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
//签名
signingConfigs{
release {
storeFile file(STORE_FILE)
storePassword STORE_PASSWORD
keyAlias KEY_ALIAS
keyPassword KEY_PASSWORD
}
debug {
release
}
}
//混淆和日志
buildTypes {
release {
minifyEnabled MINIFY_ENABLED
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField LOG_DEBUG_TYPE, LOG_DEBUG_NANE, LOG_DEBUG_RELEASE //是否输出LOG信息
buildConfigField BASE_URL_TYPE, BASE_URL_NAME, BASE_URL_RELEASE // baseUrl
signingConfigs.release
}
debug{
buildConfigField LOG_DEBUG_TYPE, LOG_DEBUG_NANE, LOG_DEBUG_DEBUG //是否输出LOG信息
buildConfigField BASE_URL_TYPE, BASE_URL_NAME, BASE_URL_DEBUG //baseUrl
}
}
//资源分包
sourceSets{
main {
res.srcDirs = [ resSource.each{it} ]
}
}
}
dependencies {
//系统的
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation roots.appcompatV7
implementation roots.constraint
testImplementation roots.junit
androidTestImplementation roots.runner
androidTestImplementation roots.espresso
//导入第三方框架
app.each {
implementation it
}
//依赖库
implementation project(':testlibrary')
}
网友评论