Android gradle 初探

Android gradle 初探(一)

首先,gradle是什么,gradle是一个构建工具,那么什么是构建工具呢?我们在开发app是只是输入了java代码和图片等资源,只要点击运行就可以在手机上跑起来这个app,在build目录下可以看到,我们的java代码和图片资源被打包成了一个app文件(app文件有点像zip文件,是一系列文件的集合),那么问题来了,这个app是如何产生的呢?google官方告诉了我们这个app是如何产生的。
在这里插入图片描述
这一系列的操作,包括编译java代码,编译资源文件,签名、混淆等等都是通过构建工具来操作的,那么构建工具应该就是一些列操作的集合,而且能够比较方便的设置这些操作。那么gradle作为一种工具,就需要提供一种能力和规范,让用户定义一些行为操作,同时可以通过一些配置来设置这些行为操作,事实上,Gradle也是这么做的,用户可以通过task来定义操作行为,通过extention来配置行为的属性。为了方便开发者,google基于gradle的规范定义了android的插件,就是我们常见的 com.android.tools.build:gradle:3.6.2

buildscript {
    repositories {
        maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven {url 'https://maven.aliyun.com/repository/google' }
        maven {url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
//        google()
//        jcenter()
        maven {
            url 'https://jitpack.io'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        }
}

com.android.tools.build:gradle:3.6.2这个gradle插件提供了android app编译、混淆、签名等一系列打包的行为,并可以通过android{}属性来设置这些行为。

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

至于gradle 的setting.gradle、build.gradle、这些文件都是gradle的规范,gradle的执行需要经过初始化、配置、以及task执行三个阶段。我们平时在andorid 工程中点击file->sync project with gradle files其实只是执行了gradle 的初始化和配置阶段,task的执行还需要手动执行task才可以。例如assemble。
在这里插入图片描述

//setting.gradle
println("gradle init setting")
rootProject.name='My Application'
include ':app'

//build.gradle(My Application)
println("gradle config at root project")
buildscript {
    
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        
    }
}

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

task("helloWorld"){
    println("this is hello world config")
    doLast {
        println("this is hello world execute task")
    }
}

// build.gradle(app)
apply plugin: 'com.android.application'
println("gradle config at app project")
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

工程中点击file->sync project with gradle files,结果如下:

gradle init setting

> Configure project :
gradle config at root project
this is hello world config

> Configure project :app
gradle config at app project

CONFIGURE SUCCESSFUL in 517ms

执行gradlew helloWorld,结果如下

gradle init setting

> Configure project :
gradle config at root project
this is hello world config

> Configure project :app
gradle config at app project

> Task :helloWorld
this is hello world execute task

BUILD SUCCESSFUL in 568ms
1 actionable task: 1 executed

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值