Android Plugin DSL Reference 参考文档 :
在上一篇博客 【Android Gradle 插件】Gradle 自定义 Plugin 插件 ⑦ ( 自定义 Gradle 插件导入方式 | buildSrc 插件导入 | 构建脚本中自定义插件 | 独立文件 ) 中 , 总结了在 Android Studio 工程中 自定义 Gradle 插件 , 并在 build.gradle 构建脚本 中导入插件的代码 ;
Android Gradle 插件 也是一种 自定义的 Gradle 插件 , 该插件被上传到 jcenter / google / mavenCentral 远程仓库 中进行存储 , 在 Android Studio 工程的根目录中的 build.gradle 构建脚本中 , 通过如下方式将 自定义插件 引入 Android 工程 ;
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
然后 , 在 Module 模块中通过
apply plugin:'com.android.application'
或
plugins {
id 'com.android.application'
}
方式 , 在 Module 模块中 , 导入并使用 Android Gradle 插件 ;
这种方式是将 自定义 Gradle 插件 的 jar 包 上传到了 jcenter / google / mavenCentral 远程仓库 中 ;
选择 " 菜单栏 / New / New Module… " 选项 ,
在 " Create New Module " 对话框中 , 选择 创建 " Java or Kotlin Library " 类型的依赖库 ;
在 buildSrc 目录 下 , 会自动引入 Java / Groovy / Gradle 的依赖 , 但是在自己创建的 Java Library 类型的依赖库 中 , 需要将其 手动引入 , 在 该 Module 模块的 build.gradle 构建脚本中引入上述依赖 ;
plugins {
id 'java-library'
id 'kotlin'
id 'groovy'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation gradleApi()
implementation localGroovy()
implementation fileTree(dir: 'libs', includes: ['*.jar'])
}