Article directory
Learn the basics of Gradle build scripts and the basics of Groovy language.
1. What is Gradle
Definition: Gradle It uses a domain-specific language based on Groovy to declare project settings.
1.1 APK Build
Compile resources, compile code, and sign apk.
1.2 Gradle execution command
./gradlew clean
./gradlew projects
./gradlew tasks
1.3 Gradle version changes, upgrades and downgrades
Command line operation: ./gradlew wrapper --gradle-version desired version number
Or directly modify gradle-wrapper.properties in the project directory
2. How to run groovy programs in Android Studio
Refer to this guy’s blog below:
https://blog.csdn.net/Gdeer/article/details/83062523
2.1 Simple syntax
class MyGroovy {
static void main(String[] args) {
//变量
int a = 10
def b = 10
println "a=$a"
println "b=$b"
//字符串
String str = "111aaaaaa1"
String str2 = str.toUpperCase()
println "str=$str"
println "str2=$str2"
//集合
def array = [1, 2, 2, 2]
println "array $array"
array.add(222)
array.each {
println it }
//map
def map = ["name": "1221", "age": 100]
println "$map"
println "${map["age"]}"
//循环
for (i in 0..<10) {
println i
}
hello(1111)
}
//方法
def hello(String name){
println name
}
}
2.2 Advanced syntax
DSL, closures, classes
Closure (function expression): an open anonymous block of code that can accept parameters, have a return value, and can also be assigned to variables.
{[ params -> ] statements }
class MyGroovy {
static void main(String[] args) {
def c = {
println 111
}
def c2 = {
it -> println it
}
c2(123)
def c3 = {
name1, name2 ->
{
println name1
println name2
}
}
c3(111, 222)
def list = [1, 2, 3, 4, 5]
list.each {
it ->
println it
}
}
}
DSL:
class MyGroovy {
static void main(String[] args) {
def android = {
compileSdkVersion 27
defaultConfig {
versionName "1.0"
}
}
Android a = new Android()
//将闭包与具体对象关联起来
android.dehydrate = a
android.call()
println ("$a")
}
}
class DefaultConfig {
private String versionName
def versionName(String versionName) {
this.versionName = versionName
}
@Override
String toString() {
return "DefaultConfig{ versionName = $versionName}"
}
}
class Android {
private int compileSdkVersion
private DefaultConfig defaultConfig
Android() {
this.defaultConfig = new DefaultConfig()
}
def compileSdkVersion(int compileSdkVersion) {
this.compileSdkVersion = compileSdkVersion
}
def defaultConfig(Closure closure) {
closure.setDelegate(defaultConfig)
closure.call()
}
@Override
String toString() {
return "Android{ compileSdkVersion = $compileSdkVersion,defaultConfig = $defaultConfig }"
}
}
2.3 Gradle builds common script files
settings.gradle
build.gradle
gradle.properties
2.4 Life cycle, sequence
initialization phase
configuration phase
Execution phase
Test in settings.gradle
gradle.addBuildListener(new BuildListener() {
@Override
void settingsEvaluated(Settings settings) {
println "Initialization phase completed"
}
@Override
void projectsLoaded(Gradle gradle) {
println "During the configuration loading process"
}
@Override
void projectsEvaluated(Gradle gradle) {
println "configuration complete"
}
@Override
void buildFinished(BuildResult buildResult) {
println "End of construction"
}
})
2.4 Important directories, Gradle tasks
Initialization phase - root project
Configuration phase - project
Execution phase - task