Gradle初探

一:Project和Module中的gradle脚本的基本格式

  • 一:project的gradle脚本如下:
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

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

allprojects {
    repositories {
        jcenter()
    }
}

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

说明:最重要的是buildScript中的代码,其中指定了jcenter为代码仓库,声明了依赖的gradle插件版本,allprojects 为全局的一些设置


  • 二:Module的gradle脚本
apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.example.testtts"
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

gradle使用的是领域特定语言,因此分析问题的时候只需要定位到特定的位置进行排查即可。

  • apply plugin: ‘com.android.application’:apply plugin表示了改module是一个application,引入了Android项目的工具,若是library的话就表示该module是一个库。

  • android领域:表示在构建过程中所用到的所有参数,默认创建了compileSdkVersion 24
    buildToolsVersion “24.0.1”两个参数,分别表示编译的SDK的版本和Android build tool的版本

  • defaultConfig:默认的一些配置放在此领域中,可覆盖清单文件中已经预先定义好的一些配置。

  • buildTypes:通过构建不同的构建类型,从而生成不同的APK,可以为构建类型实现不同的参数设置

  • dependencies:表示该module在构建过程中依赖的所有的库,可以是jar,也可以是aar,aar的优势在于相当于依赖了整个项目源码,可以有资源文件等。

二:gradle 中task的使用

assemble task :为项目打包,assembleDebug和assembleRelease分别表示打包一个debug的包和一个release的包,命令简写为gradle aD 和gradle aR

clean task:为清理已经构建好的编译结果,作用与IDE本身的clean一样,其他具体的使用方法可进行Google。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值