前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android Gradle 插件】Android Module 模块 build.gradle 构建脚本 Groovy 语法分析 ① ( Gradle 二进制插件引入 | Gradle依赖配置 )

【Android Gradle 插件】Android Module 模块 build.gradle 构建脚本 Groovy 语法分析 ① ( Gradle 二进制插件引入 | Gradle依赖配置 )

作者头像
韩曙亮
发布2023-03-30 16:25:56
5080
发布2023-03-30 16:25:56
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

Android Plugin DSL Reference 参考文档 :

一、Module 模块 build.gradle 构建脚本示例


代码语言:javascript
复制
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 插件有两种形式 :

  • 二进制插件
  • 脚本插件

在 Gradle 脚本中引入二进制插件是常见的用法 , 如 Android 中的 build.gradle 插件引入 ;

Gradle 二进制插件一般是在 jar 包中发布 , 引入方式为

代码语言:javascript
复制
apply plugin:'com.android.application'

或者

代码语言:javascript
复制
plugins {
    id 'com.android.application'
}

引入上述插件后 , 才可以调用 com.android.application 插件 中的方法 ;

build.gradle 中可引入的插件有 3 种类型 ,

三、Gradle 依赖配置


在 Gradle 构建脚本中 , 可以直接调用 org.gradle.api.Project 中的方法 , 该类的文档如下 :

org.gradle.api.Project 配置 ( build.gradle 根配置 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html

上述构建脚本中的

代码语言:javascript
复制
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 中的如下方法

代码语言:javascript
复制
// 配置工程的依赖
void dependencies​(Closure configureClosure)

在脚本中调用 dependencies 方法 , 传入一个 Closure 闭包 ;

依赖配置参考 https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html 文档 ;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、Module 模块 build.gradle 构建脚本示例
  • 二、Gradle 二进制插件引入
  • 三、Gradle 依赖配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档