一.
apply from: "config.gradle",项目的build.gradle中另起config.gradle配置参数文件,全部moudle的build文件都可以引用
ext {
android = [
compileSdkVersion: 26,
buildToolsVersion: '26.0.2',
defaultConfig : [
minSdkVersion : 16,
targetSdkVersion : 26,
testInstrumentationRunner: 'android.support.test.runner.AndroidJUnitRunner',
flavorDimensions : 'versionCode',
javaCompileOptions {
annotationProcessorOptions {
javaCompileOptions {
annotationProcessorOptions {
],
signingConfigs : [
release: [
keyAlias : '中国',
keyPassword : '6781842',
storeFile : file('D:/android_keystore/NewGamePlat'),
storePassword: '6781842',
],
debug : [
keyAlias : '中国',
keyPassword : '6781842',
storeFile : file('D:/android_keystore/NewGamePlat'),
storePassword: '6781842',
],
]
]
dependencies = [
// android官方依赖
appcompat_v7 : 'com.android.support:appcompat-v7:26.1.0',
design : 'com.android.support:design:26.1.0',
support_annotations : 'com.android.support:support-annotations:26.1.0',
// 测试依赖
junit : 'junit:junit:4.12',
runner : 'com.android.support.test:runner:1.0.1',
espresso : 'com.android.support.test.espresso:espresso-core:3.0.1',
// 第三方依赖
butterknife : 'com.jakewharton:butterknife:8.5.1',
butterknife_compiler : 'com.jakewharton:butterknife-compiler:8.5.1',
okio : 'com.squareup.okio:okio:1.13.0',
okhttp : 'com.squareup.okhttp3:okhttp:3.8.1',
okhttp3_loginterceptor: 'com.parkingwang:okhttp3-loginterceptor:0.5',
gson : 'com.google.code.gson:gson:2.6.2',
glide : 'com.github.bumptech.glide:glide:4.3.1',
eventbus : 'org.greenrobot:eventbus:3.1.1',
]
}
二.
common.gradle,moudle通用的配置文件,为了简化模块的build文件
apply plugin: 'com.jakewharton.butterknife'
def androidCfg = rootProject.ext.android
def defaultCfg = rootProject.ext.android.defaultConfig
def signingCfg = rootProject.ext.android.signingConfigs
def dependensCfg = rootProject.ext.dependencies
android {
// Android SDK配置
compileSdkVersion androidCfg.compileSdkVersion
buildToolsVersion androidCfg.buildToolsVersion
defaultConfig {
minSdkVersion defaultCfg.minSdkVersion
targetSdkVersion defaultCfg.targetSdkVersion
testInstrumentationRunner defaultCfg.testInstrumentationRunner
// flavorDimensions defaultCfg.flavorDimensions
}
// 签名配置
signingConfigs {
release {
keyAlias signingCfg.release.keyAlias
keyPassword signingCfg.release.keyPassword
storeFile signingCfg.release.storeFile
storePassword signingCfg.release.storePassword
}
debug {
keyAlias signingCfg.debug.keyAlias
keyPassword signingCfg.debug.keyPassword
storeFile signingCfg.debug.storeFile
storePassword signingCfg.debug.storePassword
}
}
// 混淆配置
buildTypes {
release {
minifyEnabled true
zipAlignEnabled true
shrinkResources true
//指定签名为release
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
zipAlignEnabled false
shrinkResources false
//指定签名为debug
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile dependensCfg.junit
androidTestCompile dependensCfg.runner
androidTestCompile dependensCfg.espresso
compile project(':product_basic')
annotationProcessor dependensCfg.butterknife_compiler
// 解决Conflict with dependency 'com.android.support:support-annotations'问题。
configurations.all {
resolutionStrategy.force dependensCfg.support_annotations
}
}
三.
具体的模块的build文件,重点:多变体以及debug,release的判断,jnilib的指定,buildConfigField定义不同编译版本下的字段值,debugCompile/releaseCompile fileTree(dir:'libs/debug',include: '*.jar')
apply plugin: 'com.android.application'
apply from: "../common.gradle"
def defaultAppId = 'com.*.oon'
android {
defaultConfig {
applicationId defaultAppId
}
productFlavors {
newtvdrawplanet {
manifestPlaceholders = [appname: "*****1"]
applicationId "com.**.dr*et"
versionCode 12
versionName '1.1.2'
flavorDimension "12"
}
newtvcastle {
manifestPlaceholders = [appname: "*****2"]
applicationId "com.*.c*e"
versionCode 2
versionName '1.0.2'
flavorDimension "2"
}
}
//多渠道相关设置
applicationVariants.all { variant ->
// 渠道相关参数设置
if (variant.buildType.name == 'release') {
if (variant.flavorName == 'newtvdrawplanet') {
//正式地址
buildConfigField("String", "SERVER", "\"http://winside.a045.ottcn.com:8005/\"")
}else if (variant.flavorName == 'newtvcastle') {
//正式地址
buildConfigField("String", "SERVER", "\"http://testeduhipi.a043.ottcn.com:8005/\"")
}
}
//测试
else {
if (variant.flavorName == 'newtvdrawplanet') {
//测试地址
buildConfigField("String", "SERVER", "\"http://testpicbook.a045.ottcn.com:8006/\"")
}else if (variant.flavorName == 'newtvcastle') {
buildConfigField("String", "SERVER", "\"http://testeduhipi.a043.ottcn.com:8005/\"")
}
}
}
sourceSets {
main {
debug{
jniLibs.srcDirs =['libs/debug']
}
release{
jniLibs.srcDirs =['libs/release']
}
}
}
}
dependencies {
compile fileTree(dir:'libs/common',include: '*.jar')
debugCompile fileTree(dir:'libs/debug',include: '*.jar')
releaseCompile fileTree(dir:'libs/release',include: '*.jar')
}
四.相关资源目录配置
sourceSets {
main{
if (rootProject.ext.moduleShopMall) {
manifest.srcFile 'src/main/module/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'
java {
//排除java/debug文件夹下的所有文件
exclude '*module'
}
}
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src/main/res']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res/main/res']
assets.srcDirs = ['assets'] }
}
网友评论