1.代码
1.build.gradle(app)
plugins {
id 'com.android.application'
}
//打印的内容格式为"",里面一个$,大括号,公共的config.gradle里的名字
//缩略
println "${username}"
//真正的写法
println "${rootProject.ext.username}"
//语言类型,弱语言,可以被推倒,下面把字符串改为了int类型
rootProject.ext.username = 666
println "${rootProject.ext.username}"
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies
def url = rootProject.ext.url
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"
}
buildTypes {
debug{
"dfsdfdsf"
buildConfigField("String","debug","\"${url.debug}\"")
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation project(path: ':library')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
support.each { k, v -> implementation v }
}
2.build.gradle(library)
plugins {
id 'com.android.library'
}
println('hello gradle 我是library')
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
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'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
support.each { k, v -> implementation v }
}
3.build.gradle(root),就是根(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "config.gradle"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
4.config.gradle(创建的),跟build.gradle(root)同级
ext{
username = "simon,我是来自config.gradle,是一个公共的gradle"
//生产/开发环境(正式/测试)
isRelealse=true
//建立Map存储,对象名都可以自定义,groovy糖果语法,非常灵活,版本号统一管理
androidId=[
compileSdkVersion:28,
buildToolsVersion:"29.0.0",
minSdkVersion:19,
targetSdkVersion:29,
versionCode:1,
versionName:"1.0"
]
//设置module包
appId=[
app:"com.hengda.gradle",
library: "com.hengda.library"
]
//生成开发环境的URL
url=[
"debug":"https//111,22,33,55/debug",
"release":"https//111,22,33,55/release",
]
//第三方库
supportLibrary="29.0.0"
dependencies =[
"appcompat":"androidx.appcompat:appcompat:1.3.0",
"material":"com.google.android.material:material:1.3.0",
"constraintlayout":"androidx.constraintlayout:constraintlayout:2.0.4",
]
}
2.过程与讲解
1.简单打印
1.println(“欢迎收看苍老湿节目”)
编译后的结果:
2.调用自己创建的gradle文件里的值,来打印
1.创建值:
2.添加到根gradle
3.调用,执行
结果:
3.替换compileSdkVersion等值,调用自己创建的gradle的值
1.创建值
2.调用值
4.替换依赖的值,这里是遍历的形式
1.添加值
2.替换遍历值
5.
1.
2.
3.
4.
5.
6.
6.
1.
2.
3.
4.
5.
6.
3.总结,反思,问题
1.不是这样的:implementation而是testImplementation等其他的呢,那就直接添加为testImplementation到自定义gradle,然后implementation替换为testImplementation,代码如下:
support.each { k, v -> testImplementationv }
2.themes文件报错
解决办法:
- 把依赖复原,对半注释依赖来排除
- 找到了凶手:
implementation 'androidx.appcompat:appcompat:1.2.0'
- 找到幕后主使:
- 妖孽,给我死:修改名字,不重复就行了,其他重复名字的也改改就好了