table of Contents
- gradle grammar
- Detailed deployment project
gradle grammar
Development environment android studio 3.4.1
gradle version 5.1.1
What is gradle
gradle in writing AndroidStudio
Write the following files in the main module gradle project:
You will enter the following information in the console log:
gradle unified multi-module configuration code
Exercise gradle
Preparation: Create an app project, making the project contains app modules and library modules.
Requirements: to unify the various modules are common configuration by creating a new gradle file.
Steps:
In the root directory of the project, the new configuration file named config.gradle, and in the project root directory, the introduction of the profile.
test:
Declare properties in config.gradle the username. And gradle module lirary uses username attribute, compile the project, view the output log information.
config.gradle文件内容如下:
//添加多个自定义属性,可以使用ext代码块
ext{
username = "zfc"
}
lirarymodule下的build.gradle内容如下:
...
println "${username}"
rootProject.ext.username = 1234
println "${rootProject.ext.username}"
...
Compile the project was as follows:
Extraction unified configuration
//声明变量
isRelease = true
//打印变量的值
print isRelease
//定义字典
androidId=[
compileSdkVersion:29,
buildToolsVersion:"29.0.2",
applicationId:"com.canjun.components",
minSdkVersion:22,
targetSdkVersion:29,
versionCode:1,
versionName:"1.0"
]
appId = [
"app":"com.canjun.components",
"order":"com.canjun.components.order",
"personal":"com.canjun.components.personal"
]
url = [
debug: "https://10.1.1.1/debug/rest",
release: "https://10.1.1.1/release/rest",
]
supportLirary = "28.0.1"
dependencies = [
appcompat:'androidx.appcompat:appcompat:1.1.0',
constraintlayout:'androidx.constraintlayout:constraintlayout:1.1.3'
]
Use of unified configuration config.gradle
apply plugin: 'com.android.application'
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies
android {
compileSdkVersion androidId.compileSdkVersion
buildToolsVersion androidId.buildToolsVersion
defaultConfig {
applicationId appId.app
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation 'androidx.appcompat:appcompat:1.1.0'
// implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// HashMap<String,String> map = new HashMap<>()
support.each{ k,v-> implementation v}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Constant attribute set to the class BuildConfig
buildConfigFiled("String","debug","\"${url.debug}\"")
apply plugin: 'com.android.application'
...
def url = rootProject.ext.url
...
android {
...
buildTypes {
debug{ buildConfigField("String","debug","\"${url.debug}\"")
}
release {
buildConfigField("String","debug","\"${url.release}\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
Results: debug field generated in the main module BuildConfig (App) directory
Andrews common project configuration gradle
android{
defaultConfig{
...
...
//开启分包
multiDexEnable true
//设置分包配置
multiDexKeepFile file('multidex-config.txt')
//使用svg生成指定纬度的png图片
vectorDrawbles.generatedDensities('xhdpi','xxhdpi')
//使用v7包兼容(5.0以上版本)
vectorDrawbles.useSupportLibrary = true
//只保留指定和默认资源
resConfig("zh-rCN")
//ndk 默认保留架构
ndk{
abiFilters("armeabi","armeabi-v7a")
}
//设置源集属性
sourceSets{
main{
if(!isRelease){
manifest.srcFile ['src/main/AndroidManifest.xml']
java.srcDirs ['src/main/java']
res.srcDirs ['src/main/res']
resources.srcDirs ['src/main/resources']
aidl.srcDirs ['src/main/aidl']
assets.srcDirs ['src/main/assets']
}else{
mainfest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
}
//signingConfigs 的定义一定要放在buildTypes的前面
signingConfigs{
debug{
storeFile file('xxx/xxxx/.android/debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
release{
// 签名证书文件
storeFile file('D:/NetEase/netease.jks')
// 签名证书的类型
storeType "netease"
// 签名证书文件的密码
storePassword "net163"
// 签名证书中密钥别名
keyAlias "netease"
// 签名证书中该密钥的密码
keyPassword "net163"
// 是否开启V2打包
v2SigningEnabled true
}
}
buildTypes{
debug{
signingConfig.debug
}
release{
signingConfig.release
...
}
}
}
//对adb配置
adbOptions{
//操作超时时间为5秒
timeOutInMs = 5 * 1000_0
//adb install选项配置i
installOptions '-r','-s'
}
// 对 dx 操作的配置,接受一个 DexOptions 类型的闭包,配置由 DexOptions 提供
dexOptions {
// 配置执行 dx 命令是为其分配的最大堆内存
javaMaxHeapSize "4g"
// 配置是否预执行 dex Libraries 工程,开启后会提高增量构建速度,不过会影响 clean 构建的速度,默认 true
preDexLibraries = false
// 配置是否开启 jumbo 模式,代码方法是超过 65535 需要强制开启才能构建成功
jumboMode true
// 配置 Gradle 运行 dx 命令时使用的线程数量
threadCount 8
// 配置multidex参数
additionalParameters = [
'--multi-dex', // 多dex分包
'--set-max-idx-number=50000', // 每个包内方法数上限
// '--main-dex-list=' + '/multidex-config.txt', // 打包到主classes.dex的文件列表
'--minimal-main-dex'
]
}
// 执行 gradle lint 命令即可运行 lint 检查,默认生成的报告在 outputs/lint-results.html 中
lintOptions {
// 遇到 lint 检查错误会终止构建,一般设置为 false
abortOnError false
// 将警告当作错误来处理(老版本:warningAsErros)
warningsAsErrors false
// 检查新 API
check 'NewApi'
}
Detailed deployment project
Components of significance
PhoneModule and Android Libray difference
PhoneModule存在的配置:
apply plugin: 'com.android.application'
applicationId "com.canjun.components"
AndroidLibrary存在的配置:
apply plugin: 'com.android.library'
applicationId "com.canjun.components"(x)
Integration and component
Integration is a project only one main module, other modules for Lirary, these Library not run alone. They must run together and become a master module app
Project means that one component of the respective module can be run separately.
Integrated for production, assembly and testing for the development of a multi-stage, the characteristics of highly decoupled in favor of development and testing.
Components of the project configuration
-
modifying the configuration of the main module gradle
apply plugin: 'com.android.application' def androidId = rootProject.ext.androidId def appId = rootProject.ext.appId def url = rootProject.ext.url def support = rootProject.ext.dependencies def isRelease = rootProject.ext.isRelease android { compileSdkVersion androidId.compileSdkVersion buildToolsVersion androidId.buildToolsVersion defaultConfig { applicationId appId.app minSdkVersion androidId.minSdkVersion targetSdkVersion androidId.targetSdkVersion versionCode androidId.versionCode versionName androidId.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField("boolean","isRelease",String.valueOf(isRelease)) // multiDexEnable true } buildTypes { debug{ buildConfigField("String","debug","\"${url.debug}\"") } release { buildConfigField("String","debug","\"${url.release}\"") minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) ... support.each{ k,v-> implementation v} implementation project(":common") //主模块不能依赖主模块 if(isRelease){ implementation project(":order") implementation project(":personal") } ... }
-
library module configuration modification gradle
// 要点一 if(isRelease){ apply plugin: 'com.android.library' }else { apply plugin: 'com.android.application' } def androidId = rootProject.ext.androidId def appId = rootProject.ext.appId def support = rootProject.ext.dependencies android { compileSdkVersion androidId.compileSdkVersion buildToolsVersion androidId.buildToolsVersion defaultConfig { minSdkVersion androidId.minSdkVersion // 要点二 if(!isRelease){ applicationId appId.order } targetSdkVersion androidId.targetSdkVersion versionCode androidId.versionCode versionName androidId.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) support.each{ k,v -> implementation v} implementation project(":common") testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
3. Effect show
Centralized effects show:
Components effects show:
Temporary assembly of code development, when the centralized packaging dynamically isolated
Since the development of the component, write some localized moduel code of the response, but the code, but does not handle, the default will be packed into the main module. As shown below:
Therefore require special handling at:
- Configuration gradle, code isolation
- The localization code is stored in accordance with the configuration gradle
3. Verify