Gradle之build.gradle文件

project的build.gradle文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.
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
    }
}

//project中所有module共用的第三方依赖包的仓库
allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

buildscript

    /**
     * <p>Configures the build script classpath for this project.
     *
     * <p>The given closure is executed against this project's {@link ScriptHandler}. The {@link ScriptHandler} is
     * passed to the closure as the closure's delegate.
     *
     * @param configureClosure the closure to use to configure the build script classpath.
     */
    void buildscript(Closure configureClosure);

为此项目配置构建脚本的类路径。
给定的闭包是针对这个项目的{@link ScriptHandler}执行的。{@link ScriptHandler}是作为闭包的委托传递给闭包。

buildscript闭包主要是为了gradle构建脚本自身的执行,获取脚本执行所依赖的插件。

repositories{}闭包

配置远程仓库
该闭包中声明了jcenter()和google()的配置,其中jcenter是一个代码托管仓库,上面托管了很多Android开源项目,在这里配置了jcenter后我们可以在项目中方便引用jcenter上的开源项目。从Android Studio3.0后新增了google()配置,可以引用google上的开源项目。

dependencies{}闭包

配置构建工具
该闭包使用classpath声明了一个Gradle插件,由于Gradle并不只是用来构建Android项目,因此此处引入相关插件来构建Android项目,其中’3.0.0’为该Gradle插件的版本号,可以根据最新的版本号来调整。

allprojects

    /**
     * <p>Configures this project and each of its sub-projects.</p>
     *
     * <p>This method executes the given closure against this project and its sub-projects. The target {@link Project}
     * is passed to the closure as the closure's delegate.</p>
     *
     * @param configureClosure The closure to execute.
     */
    void allprojects(Closure configureClosure);

配置此项目及其每个子项目。
此方法针对此项目及其子项目执行给定的闭包。目标{@link Project}作为闭包的委托传递给闭包。

allprojects闭包下配置的repositories用于多项目构建,为所有项目提供所需要的共同的依赖包。而子项目可以在自己的build.gradle文件中的repositories中配置自己独需的依赖包。

module的build.gradle文件

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

	//动态的在build时配置AndroidManifest.xml里的项目,defaultConfig里的配置可以覆盖manifest里的配置。
    defaultConfig {
        applicationId "com.example.flutterdemo"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

	//配置如何构建和打包你的App,默认有debug和release两个类型。debug类型包含调试时的信息,并且有debug key签名。release默认是不含签名的。
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    
	productFlavors {
 
	}	

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


    //implementation project(':flutter_module')
    implementation project(':flutter')


}

android {}闭包

defaultConfig

动态的在build时配置AndroidManifest.xml里的项目,defaultConfig里的配置可以覆盖AndroidManifest.xml里的配置。

buildTypes

配置如何构建和打包你的App,默认有debug和release两个类型,分别对应测试包和线上release包。debug类型包含调试时的信息,并且有debug key签名。release默认是不含签名的。本例中release版本用了ProGuard。

defaultConfig 与 buildTypes的区别?

productFlavors

用于多渠道打包的相关配置

productFlavors顾名而思义,就是用于定义产品的特性,这是每个产品不同的地方。有了它我们可以用同一套代码创建不同的产品。

lintOptions

lint静态代码检查的相关配置

artifact

gradle 的artifact 是指什么

assembleDebug 和 assembleRelease这些gradle task是定义在哪里

gradle打包整体流程

1.先根据build.gradle脚本里的配置数据生成Configuration
2.然后根据Configuration执行Task(比如:assembleDebug与assembleRelease)

build.gradle的 android {}闭包

参考:

你所应该知道的gradle知识都在这里(1)

关于Android Studio里的Gradle

Gradle的神奇之处

浅谈Gradle(一)

Android Gradle 学习笔记(七):Android Gradle 插件

Gradle 理解:Artifact

Android开发如何使用productFlavors
productFlavors的使用
productFlavors简介

史上最全Android build.gradle配置详解
[Android Gradle] 搞定Groovy闭包这一篇就够了
Android打包之Gradle的打包方式
史上最全的Android build.gradle 配置介绍
build gradle dependencies闭包的详解

gradle在Java项目构建参数详细解释

android studio 查看task的代码:
android studio中的gradle task怎么查看其源码?

Android Gradle defaultConfig详解及实用技巧
defaultConfig——安卓gradle

Android Gradle使用详解(三) 之 Android Gradle插件配置详解
关于Android Studio里的Gradle

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值