安卓资源收集

Android开发札记初级(四)Gradle详解

2016-12-07  本文已影响0人  Newamber

Gradle 概述

Gradle 生命周期

gradle 构建一个工程主要分为三部分:

基础

一个 Android Studio 项目中,会存在多个 .gradle 文件。其中 Project 目录下存在一个 工程 build.gradle 文件,而每一个 module 会存在一个 build.gradle 文件。

工程中的 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
      //声明使用jcenter()库,可以理解成一个新的中央远程库,可以把自己的库上传到jecenter()供别人一起使用,兼容maven中心仓库,而且性能更优。
        jcenter()  
    }
    dependencies {
        //依赖android提供的2.2.1的 gradle build
        classpath 'com.android.tools.build:gradle:2.2.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
//为所有的工程的repositories配置为jcenters
allprojects {
    repositories {
        jcenter()
    }
}
//清楚工程的任务
task clean(type: Delete) {
    delete rootProject.buildDir
}

模块中的 build.gradle

//这表示module是一个安卓项目
apply plugin: 'com.android.application'
// 配置了所有android构建的参数
android {
    // 表示是基于哪个Sdk版本编译
    compileSdkVersion 24
    //基于哪个构建工具版本进行构建的
    buildToolsVersion "24.0.2"

//缺省配置主要包括:应用ID、最小SDK版本、目标SDK版本、应用版本号和应用版本名
    defaultConfig {
        applicationId "com.example.newam.helloworld"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    //buildTypes是构建类型,常用的有release和debug两种,可以在这里面启用混淆,启用zipAlign以及配置签名信息等。
    buildTypes {
        release {
            //是否启用代码混淆
            minifyEnabled false 
             // 混淆使用文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
//dependencies它定义了该module需要依赖的jar,aar,jcenter库信息。
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:gridlayout-v7:24.2.1'
}

待补充

上一篇 下一篇

猜你喜欢

热点阅读