1 . 最外层build.grade
一般最外层的build.gradle文件只要配置buildscript就行了,配置如下:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
}
}
有时候我们会在主项目的build.gradle配置maven/jcenter仓库用来对dependencies的compile的下载,配置如下:
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
但是如果有多个moudle的时候,需要对每一个moudle进行仓库的配置,这就不爽了,所以一般就在最外层配置好整个项目的仓库,配置好后主项目或者moudle就不用再去配置仓库了,所以最外层的配置一般是这样的
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
}
}
//对整个项目进行仓库的配置
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
但是有时候会用到一些公司内部的代码仓库,这个仓库外网是访问不到的,那么需要这样引用
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven {
credentials {
username "xxx"
password "xxx" }
url 'http://xxx'
}
}
}
2 . 主项目的build.grade
主项目的配置比较多,废话不说,直接上最终配置情况
apply plugin: 'com.android.application'
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile 'com.android.support:support-v4:21.0.3'
//使用开源项目
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.alibaba.mobileim:IMCore:2.0.2.1@aar'
//使用本地module
compile project(':jakewharton')
//使用本地aar
compile(name: 'test', ext: 'aar')
}
android {
//开发工具
compileSdkVersion 23
buildToolsVersion "23.0.3"
//项目信息
defaultConfig {
applicationId = "com.xxx"
minSdkVersion 16
targetSdkVersion 25
versionCode 2016032401
versionName = "1.0.0"
}
//使用本地aar必需要加的路径配置,如若使用的是module里面的aar,应该加上'../module name/libs'
repositories {
flatDir {
dirs 'libs'
}
}
//资源路径
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
// // Move the tests to tests/java, tests/res, etc...
// instrumentTest.setRoot('tests')
//
// // Move the build types to build-types/
// // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// // This moves them out of them default location under src//... which would
// // conflict with src/ being used by the main source set.
// // Adding new build types or product flavors should be accompanied
// // by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
packagingOptions {
pickFirst 'META-INF/LICENSE.txt'
pickFirst 'META-INF/NOTICE.txt'
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/NOTICE'
}
lintOptions {
checkReleaseBuilds false
}
//签名信息
signingConfigs {
debug {
}
release {
storeFile file("xxx.keystore")
storePassword "xxxxxx"
keyAlias "xxx"
keyPassword "xxxxxx"
}
}
//混淆配置
buildTypes {
debug {
}
release {
minifyEnabled true
zipAlignEnabled true
// Remove useless resource
shrinkResources true
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (variant.buildType.name.equalsIgnoreCase("release") && outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "APP_v${versionName}_${getTime()}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
//指定运行CPU
ndk {
abiFilters "armeabi",'armeabi-v7a'
}
}
}
}
def getDate() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
def getTime() {
return new java.text.SimpleDateFormat("yyyyMMdd").format(new Date());
}
有时候一个项目会有多个flavor,可以随时切换需要运行的项目,配置如下:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.alibaba.mobileim:IMCore:2.0.2.1@aar'
compile project(':jakewharton')
compile(name: 'test', ext: 'aar')
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
productFlavors {
Flavor_name_01 {
applicationId = "com.xxx.name1"
minSdkVersion 16
targetSdkVersion 25
versionCode 2016032401
versionName = "1.0.0"
}
Flavor_name_02 {
applicationId = "com.xxx.name2"
minSdkVersion 16
targetSdkVersion 25
versionCode 2016032401
versionName = "1.0.0"
}
}
repositories {
flatDir {
dirs 'libs'
}
}
sourceSets {
main {
java.srcDirs = ['src/main']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
Flavor_name_01 {
java.srcDirs = ['src/Flavor_name_01']
jniLibs.srcDirs = ['libs/Flavor_name_01']
}
Flavor_name_02 {
java.srcDirs = ['src/Flavor_name_02']
jniLibs.srcDirs = ['libs/Flavor_name_02']
}
// // Move the tests to tests/java, tests/res, etc...
// instrumentTest.setRoot('tests')
//
// // Move the build types to build-types/
// // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// // This moves them out of them default location under src//... which would
// // conflict with src/ being used by the main source set.
// // Adding new build types or product flavors should be accompanied
// // by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
packagingOptions {
pickFirst 'META-INF/LICENSE.txt'
pickFirst 'META-INF/NOTICE.txt'
pickFirst 'META-INF/LICENSE'
pickFirst 'META-INF/NOTICE'
}
lintOptions {
checkReleaseBuilds false
}
signingConfigs {
debug {
}
release {
storeFile file("xxx.keystore")
storePassword "xxxxxx"
keyAlias "xxx"
keyPassword "xxxxxx"
}
}
buildTypes {
debug {
}
release {
minifyEnabled true
zipAlignEnabled true
// Remove useless resource
shrinkResources true
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// apk file's name
def fileName = "APP_${variant.productFlavors[0].versionName}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
ndk {
abiFilters "armeabi",'armeabi-v7a'
}
}
}
}