Android Plugin DSL Reference 参考文档 :
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 31
buildToolsVersion "31.0.0"
defaultConfig {
applicationId "kim.hsl.paintgradient"
minSdkVersion 18
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Gradle 插件有两种形式 :
在 Gradle 脚本中引入二进制插件是常见的用法 , 如 Android 中的 build.gradle 插件引入 ;
Gradle 二进制插件一般是在 jar 包中发布 , 引入方式为
apply plugin:'com.android.application'
或者
plugins {
id 'com.android.application'
}
引入上述插件后 , 才可以调用 com.android.application 插件 中的方法 ;
build.gradle 中可引入的插件有 3 种类型 ,
在 Gradle 构建脚本中 , 可以直接调用 org.gradle.api.Project 中的方法 , 该类的文档如下 :
org.gradle.api.Project 配置 ( build.gradle 根配置 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html
上述构建脚本中的
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
配置 , 就是定义在 org.gradle.api.Project 中的如下方法
// 配置工程的依赖
void dependencies(Closure configureClosure)
在脚本中调用 dependencies 方法 , 传入一个 Closure 闭包 ;
依赖配置参考 https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html 文档 ;