自己写了几个gradle文件模板,如果还在使用Support包开发的可以参考。
定义一个全局统一配置文件config.gradle
Project目录下的config.gradle
/**
* 全局统一配置文件
*/
ext {
//true 每个业务Module可以单独开发
//false 每个业务Module以lib的方式运行
//修改之后需要Sync方可生效
isModule = false
//版本号
versions = [
applicationId : "", //应用ID
compileSdkVersion : 28,
buildToolsVersion : "28.0.3",
minSdkVersion : 19,
targetSdkVersion : 28,
versionCode : 1, //版本号
versionName : "1.0.0", //版本名称
androidSupportSdkVersion : "28.0.0",
constraintLayoutVersion : "1.1.3",
runnerVersion : "1.0.2",
espressoVersion : "3.0.2",
junitVersion : "4.12",
annotationsVersion : "24.0.0",
javaSDKVersion : 1.8,//javaSDK版本
multidexVersion : "1.0.2",
butterknifeVersion : "8.8.1",
arouterApiVersion : "1.4.0",
arouterCompilerVersion : "1.2.1",
arouterannotationVersion : "1.0.4",
eventbusVersion : "3.0.0",
loggerVersion : "2.2.0",
glideVersion : "4.9.0",
glideTransformationsVersion: "4.0.0",
picassoVersion : "2.5.2",
greendaoVersion : "3.2.2",
databaseSqlcipherVersion : "3.5.7",
rx2JavaVersion : "2.1.6",
rx2AndroidVersion : "2.0.1",
rxPermissionVersion : "0.9.5",
rxLifeCycleVersion : "2.2.1",
okhttpVersion : "3.3.1",
retrofitVersion : "2.3.0",
urlconnectionVersion : "3.2.0",
gsonVersion : "2.2.8",
draggerVersion : "2.14.1",
guavaVersion : "28.0-jre",
]
dependencies = [
"appcompat_v7" : "com.android.support:appcompat-v7:${versions["androidSupportSdkVersion"]}",
"constraint_layout" : "com.android.support.constraint:constraint-layout:${versions["constraintLayoutVersion"]}",
"runner" : "com.android.support.test:runner:${versions["runnerVersion"]}",
"espresso_core" : "com.android.support.test.espresso:espresso-core:${versions["espressoVersion"]}",
"junit" : "junit:junit:${versions["junitVersion"]}",
"support_annotations" : "com.android.support:support-annotations:${versions["annotationsVersion"]}",
"design" : "com.android.support:design:${versions["androidSupportSdkVersion"]}",
"support-v4" : "com.android.support:support-v4:${versions["androidSupportSdkVersion"]}",
"cardview-v7" : "com.android.support:cardview-v7:${versions["androidSupportSdkVersion"]}",
"recyclerview-v7" : "com.android.support:recyclerview-v7:${versions["androidSupportSdkVersion"]}",
//方法数超过65535解决方法64K MultiDex分包方法
"multidex" : "com.android.support:multidex:${versions["multidexVersion"]}",
//路由
"arouter_api" : "com.alibaba:arouter-api:${versions["arouterApiVersion"]}",
"arouter_compiler" : "com.alibaba:arouter-compiler:${versions["arouterCompilerVersion"]}",
"arouter_annotation" : "com.alibaba:arouter-annotation:${versions["arouterannotationVersion"]}",
//黄油刀
"butterknife_compiler" : "com.jakewharton:butterknife-compiler:${versions["butterknifeVersion"]}",
"butterknife" : "com.jakewharton:butterknife:${versions["butterknifeVersion"]}",
//事件订阅
"eventbus" : "org.greenrobot:eventbus:${versions["eventbusVersion"]}",
//数据库
"greendao" : "org.greenrobot:greendao:${versions["greendaoVersion"]}",
//使用加密数据库时需要添加,使用后会apk会变大很多,如果对安全性要求不高,不建议使用
"database_sqlcipher" : "net.zetetic:android-database-sqlcipher:${versions["databaseSqlcipherVersion"]}",//compileOnly
//日志
"logger" : "com.orhanobut:logger:${versions["loggerVersion"]}",
//图片加载
"picasso" : "com.squareup.picasso:picasso:${versions["picassoVersion"]}",
"glide" : "com.github.bumptech.glide:glide:${versions["glideVersion"]}",
"glide_transformations" : "jp.wasabeef:glide-transformations:${versions["glideTransformationsVersion"]}@aar",
"glidecompiler" : "com.github.bumptech.glide:compiler:${versions["glideVersion"]}",//annotationProcessor
//权限申请
"rxpermission" : "com.tbruyelle.rxpermissions2:rxpermissions:${versions["rxPermissionVersion"]}@aar",
//RxLifeCycle
"rxlifecycle" : "com.trello.rxlifecycle2:rxlifecycle:${versions["rxLifeCycleVersion"]}",
"rxlifecycle_andoird" : "com.trello.rxlifecycle2:rxlifecycle-android:${versions["rxLifeCycleVersion"]}@aar",
//网络请求
"okhttp" : "com.squareup.okhttp3:okhttp:${versions["okhttpVersion"]}",
"retrofit" : "com.squareup.retrofit2:retrofit:${versions["retrofitVersion"]}",
"retrofit_converter_gson": "com.squareup.retrofit2:converter-gson:${versions["retrofitVersion"]}",
"retrofit_adapter_rxjava": "com.squareup.retrofit2:adapter-rxjava2:${versions["retrofitVersion"]}",
//JavaNetCookieJar来实现非持久化cookie
"okhttp_urlconnection" : "com.squareup.okhttp3:okhttp-urlconnection:${versions["urlconnectionVersion"]}",
//依赖注入
"dagger" : "com.google.dagger:dagger:${versions["draggerVersion"]}",
"dagger_compiler" : "com.google.dagger:dagger-compiler:${versions["draggerVersion"]}",
//gson解析
"gson" : "com.google.code.gson:gson:${versions["gsonVersion"]}",
//guava 谷歌提供的工具扩展库,提供用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法
"guava" : "com.google.guava:guava:${versions["guavaVersion"]}",
]
}
Project下build.gradle文件
apply from: "config.gradle" //引用全局配置
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
//黄油刀
classpath "com.jakewharton:butterknife-gradle-plugin:8.8.1"
//数据库GreenDAO
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
module下build.gradle文件
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.phz.myapplication"
minSdkVersion rootProject.ext.versions.minSdkVersion
targetSdkVersion rootProject.ext.versions.targetSdkVersion
versionCode rootProject.ext.versions.versionCode
versionName rootProject.ext.versions.versionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//MultiDex分包方法
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters "armeabi", "armeabi-v7a","arm64-v8a"
// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
}
buildTypes {
debug {
minifyEnabled false//是否混淆
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//防止编译的时候oom、GC
dexOptions {
javaMaxHeapSize "4g"
}
//greendao配置
greendao {
//数据库版本号,升级时进行修改
schemaVersion 1
daoPackage 'com.phz.db.greendao' //生成的DAO,DaoMaster、DaoSession、Dao 的包名。默认是实体的包名
targetGenDir 'src/main/java' //生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)
generateTests false //设置为true以自动生成单元测试
targetGenDirTests 'src/main/java' //应存储生成的单元测试的基本目录。默认为 src/androidTest/java
}
lintOptions {
abortOnError false
}
compileOptions {
targetCompatibility rootProject.ext.versions.javaSDKVersion
sourceCompatibility rootProject.ext.versions.javaSDKVersion
}
signingConfigs {
debug {
/**
* jsk和keystore都可以 ..是回到项目根目录
*/
// storeFile file('../key.keystore')
storeFile file('key.jks')
storePassword "123456"
keyAlias "key"
keyPassword "123456"
}
release {
storeFile file('key.jks')
keyAlias "key"
keyPassword "123456"
v1SigningEnabled true
v2SigningEnabled true
}
}
//自定义apk的文件名
android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "MyApk_${rootProject.ext.versions.versionName}_Bata${releaseTime()}.apk"
}
}
}
//获得当前时间
static def releaseTime() {
//GMT+8
return new Date().format("yyyyMMdd_mmss", TimeZone.getTimeZone("UTC"))
}
dependencies {
api rootProject.ext.dependencies["appcompat_v7"]
api rootProject.ext.dependencies["constraint_layout"]
api rootProject.ext.dependencies["cardview-v7"]
api rootProject.ext.dependencies["recyclerview-v7"]
api rootProject.ext.dependencies["support-v4"]
api rootProject.ext.dependencies["design"]
api rootProject.ext.dependencies["support_annotations"]
api rootProject.ext.dependencies["junit"]
//作为basemodule,使用api,其他组件implementation project就行,如果直接用,可以把api换成implementation节省消耗
//MultiDex分包方法
api rootProject.ext.dependencies["multidex"]
//黄油刀
annotationProcessor rootProject.ext.dependencies["butterknife_compiler"]
api rootProject.ext.dependencies["butterknife"]
//Arouter路由
// annotationProcessor rootProject.ext.dependencies["arouter_compiler"]
// api rootProject.ext.dependencies["arouter_api"]
// api rootProject.ext.dependencies["arouter_annotation"]
//eventbus 发布/订阅事件总线
api rootProject.ext.dependencies["eventbus"]
//数据库
api rootProject.ext.dependencies["greendao"]
api rootProject.ext.dependencies["database_sqlcipher"]
//日志
api rootProject.ext.dependencies["logger"]
//图片加载
// api rootProject.ext.dependencies["picasso"]
api rootProject.ext.dependencies["glide"]
api rootProject.ext.dependencies["glide_transformations"]
annotationProcessor rootProject.ext.dependencies["glidecompiler"]
//权限申请
api rootProject.ext.dependencies["rxpermission"]
//RxLifeCycle
api rootProject.ext.dependencies["rxlifecycle"]
api rootProject.ext.dependencies["rxlifecycle_andoird"]
//网络请求
api rootProject.ext.dependencies["okhttp"]
api rootProject.ext.dependencies["retrofit"]
api rootProject.ext.dependencies["retrofit_converter_gson"]
api rootProject.ext.dependencies["retrofit_adapter_rxjava"]
api rootProject.ext.dependencies["okhttp_urlconnection"]
//依赖注入
api rootProject.ext.dependencies["dagger"]
api rootProject.ext.dependencies["dagger_compiler"]
//gson解析
api rootProject.ext.dependencies["gson"]
//guava
api rootProject.ext.dependencies["guava"]
//上下拉刷新
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0' //1.0.5及以前版本的老用户升级需谨慎,API改动过大
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0' //没有使用特殊Header,可以不加这行
}