Gradle基本配置
- 根目录下的build.gradle一般内容如下
buildscript {
//可导入其他gradle的配置
apply from: 'dependencies.gradle'
repositories {
//gradle脚本使用的仓库
jcenter()
}
dependencies {
//脚本路径
classpath 'com.android.tools.build:gradle:1.3.0'
}
}
//所有子工程的依赖库的配置
allprojects {
repositories {
jcenter()
}
}
ext {
compileSdkVersion = 26
targetSdkVersion = 26
minSdkVersion = 19
buildToolsVersion = '26.0.3'
supportVersion = '26.1.0'
}
- app module下面的build.gradle
//能直接运行的module使用application,作为第三方库使用library
apply plugin: 'com.android.application'
ext {
versionCode = 124
versionName = "3.6.1"
}
android {
//6.0之后goole不支持使用HttpClient,如果需要使用,加上这句话
useLibrary 'org.apache.http.legacy'
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
//主module必须配置,依赖module不要此项
applicationId "包名"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
//下面两项会在BuildConfig生成对应的值
versionCode project.property("versionCode")
versionName project.property("versionName")
}
//目录指向配置,一般不需要更改,如果想把so文件也放入libs则需要加上下面的,也可以自己创建jniLibs目录
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
//签名配置
signingConfigs {
release {
storeFile file('./theme.keystore')
storePassword "123"
keyAlias "123"
keyPassword "123"
}
}
//编译类型,分为debug和Release,对应BuildConfig里面的DEBUG字段
buildTypes {
debug {
zipAlignEnabled false //zip压缩
minifyEnabled false //混淆
shrinkResources false //移除无用的资源
//如果有需要可以在BuildConfig下面添加字段
buildConfigField 'boolean', 'BUILD_DEBUG', 'true'
}
release {
zipAlignEnabled true
minifyEnabled true
shrinkResources true
buildConfigField 'boolean', 'BUILD_DEBUG', 'false'
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
}
//在这里可以统一配置
buildTypes.all {
type ->
//修改清单文件的meta值 <meta-data android:value="${VERSION_TYPE_VALUE}" android:name="VERSION_TYPE"/>
manifestPlaceholders = [VERSION_TYPE_VALUE: name,
BUILD_TIME_VALUE : releaseTime()]
signingConfig signingConfigs.release
}
}
//打包渠道,该项可以是多维的,配置完之后会在Build Variants下面生成(第一维)*(第二维)* 2中编译类型,
//最后的2表示debug或resease
//用处有:如果服务器有测试地址和正式地址,则可以根据BuildConfig里面生成的字段设置服务器的地址
//可以看出appName是不一样的,能够更好的区分
productFlavors {
flavorDimensions "enviroment", "channel"
Envtest {
dimension "enviroment"
applicationId = "包名"
manifestPlaceholders = [APP_NAME:'Demo Test']
}
Online {
dimension "enviroment"
applicationId = "包名"
manifestPlaceholders = [APP_NAME:'Demo']
}
Dev {
dimension "enviroment"
applicationId = "包名"
manifestPlaceholders = [APP_NAME:'Demo Dev']
}
xiaomi {
dimension "channel"
}
san60 {
dimension "channel"
}
baidu {
dimension "channel"
}
//这里统一配置
productFlavors.all {
flavor -> manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
}
dependencies {
//表示编译libs下的所有jar包
implementation fileTree(include: ['*.jar'], dir: 'libs')
//还可以添加其他依赖项
//这种形式表示依赖module
implementation project(':gpuimagefilter')
//这种形式表示编译远程库
implementation 'com.android.support:recyclerview-v7:' + config.supportVersion
//或者
implementation deps.support.recyclerview
}
//对应的BuildConfig会自动生成:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "cn.demo";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "DevBaidu";
public static final int VERSION_CODE = 191;
public static final String VERSION_NAME = "3.8.2.debug";
public static final String FLAVOR_enviroment = "Dev";
public static final String FLAVOR_channel = "baidu";
// Fields from build type: debug
public static final boolean BUILD_DEBUG = true;
}
- 被使用的dependencies.gradle
// 声明依赖
def deps = [:]
// support 依赖
def support = [:]
support.appcompat = "com.android.support:appcompat-v7:26.1.0"
support.recyclerview = "com.android.support:recyclerview-v7:26.1.0"
support.constraint = "com.android.support.constraint:constraint-layout:1.0.2"
support.recyclerview = "com.android.support:recyclerview-v7:26.1.0"
deps.support = support
deps.okhttp = "com.squareup.okhttp3:okhttp:3.10.0"
deps.logging_interceptor = "com.squareup.okhttp3:logging-interceptor:3.10.0"
deps.fastjson = "com.alibaba:fastjson:1.1.68.android"
ext.deps = deps
def classpath = [:]
classpath.gradle = "com.android.tools.build:gradle:3.0.0"
classpath.gradle_tools = 'com.huli.android.plugin:gradle-tools:1.0.1'
ext.classpath = classpath
def repo = [:]
repo.releaseUrl = "xxx"
repo.snapshotUrl = "xxx"
ext.repo = repo
// 基本sdk配置
def config = [:]
config.compileSdkVersion = 26
config.targetSdkVersion = 26
config.minSdkVersion = 17
config.buildToolsVersion = '26.0.3'
config.supportVersion = '26.1.0'
config.sourceCompatibilityVersion = JavaVersion.VERSION_1_8
config.targetCompatibilityVersion = JavaVersion.VERSION_1_8
ext.config = config
Gradle新的依赖方式 implementation api 等
implementation
和api
是取代之前的compile
,其中api
和compile
是一样的效果,implementation
有所不同 ,这个指令的特点是,对该库有依赖的项目将无法访问到该库使用implementation依赖的库,举个栗子,A依赖B,而B通过implementation
依赖C,那么A将访问不了C中的内容。
使用implementation的好处有: 1 加快编译速度 2 隐藏对外不必要的接口
compile only
和provided
效果是一样的,只在编译的时候有效,不参与打包,适合使用在第三方库的开发中,避免与主工程造成jar包冲突runtime only
和apk
效果有效,只在打包的时候有效,不参与编译
下面这篇文章对gradle的使用介绍的非常详细,很有必要学习一下
这样使用Gradle可以神奇地打各种渠道包